Aug 23, 2017

while loop (1)

> ctr <- 1
> while(ctr <= 5){
+  print(paste("ctr is set to", ctr))
+  ctr <- ctr + 1  # Increment ctr
+ }
[1] "ctr is set to 1"
[1] "ctr is set to 2"
[1] "ctr is set to 3"
[1] "ctr is set to 4"
[1] "ctr is set to 5"
> # TEMPLATE for while loop
> while(condition){
+   expression
+ }

infinite while loop

> ctr <- 1
> while(ctr <= 5){
+  print(paste("ctr is set to", ctr))
+  ctr <- ctr  # ctr not incremented
+ }
[1] "ctr is set to 1"
[1] "ctr is set to 1"
[1] "ctr is set to 1"
[1] "ctr is set to 1"

break statement

> ctr <- 1
> while(ctr <= 25){
+  if(ctr %% 5 == 0){
+    break
+  }
+  print(paste("ctr is set to", ctr))
+  ctr <- ctr +1
+ }
[1] "ctr is set to 1"
[1] "ctr is set to 2"
[1] "ctr is set to 3"
[1] "ctr is set to 4"

Note: loop stops when ctr is 5!

for loop (1)

> cities <- c("Boone", "Charlotte", 
+             "Durham", "Raleigh")
> for(city in cities){
+   print(city)
+ }
[1] "Boone"
[1] "Charlotte"
[1] "Durham"
[1] "Raleigh"
> # for loop TEMPLATE
> for(var in seq){
+   expression
+ }

for loop over list

> cities <- list("Boone", "Charlotte", 
+             "Durham", "Raleigh")
> for(city in cities){
+   print(city)
+ }
[1] "Boone"
[1] "Charlotte"
[1] "Durham"
[1] "Raleigh"
> # for loop TEMPLATE
> for(var in seq){
+   expression
+ }

break statement

> cities <- list("Boone", "Charlotte", 
+             "Durham", "Raleigh")
> for(city in cities){
+   if(nchar(city) == 6){
+     break
+   }
+   print(city)
+ }
[1] "Boone"
[1] "Charlotte"
> # for loop TEMPLATE
> for(var in seq){
+   expression
+ }

next statement

> cities <- list("Boone", "Charlotte", 
+             "Durham", "Raleigh")
> for(city in cities){
+   if(nchar(city) == 6){
+     next  # Durham not printed
+   }
+   print(city)
+ }
[1] "Boone"
[1] "Charlotte"
[1] "Raleigh"
> # for loop TEMPLATE
> for(var in seq){
+   expression
+ }

for loop (v2)

> cities <- c("Boone", "Charlotte", 
+             "Durham", "Raleigh")
> for(i in 1:length(cities)){
+   print(cities[i])
+ }
[1] "Boone"
[1] "Charlotte"
[1] "Durham"
[1] "Raleigh"
> # for loop TEMPLATE
> for(var in seq){
+   expression
+ }

for loop (v2) more

> cities <- c("Boone", "Charlotte", 
+             "Durham", "Raleigh")
> for(i in 1:length(cities)){
+   print(paste(cities[i], 
+         "is position", i , 
+         "in cities."))
+ }
[1] "Boone is position 1 in cities."
[1] "Charlotte is position 2 in cities."
[1] "Durham is position 3 in cities."
[1] "Raleigh is position 4 in cities."
> # for loop TEMPLATE
> for(var in seq){
+   expression
+ }

for loop: comparison

  • Are you awake?
  • Harder to read and write
  • More versatile
> cities <- c("Boone", 
+             "Charlotte", 
+             "Durham", 
+             "Raleigh")
> for(i in 1:length(cities)){
+   print(cities[i])
+ }
  • Concise
  • Easy to read
  • No access to looping index
> cities <- c("Boone", 
+             "Charlotte", 
+             "Durham", 
+             "Raleigh")
> for(city in cities){
+   print(city)
+ }