While loop

Intermediate R

Filip Schouwenaars

DataCamp Instructor

while loop

while(condition) {
  expr
}
while(ctr <= 7) {
Intermediate R

while loop

while(condition) {
  expr
}
while(ctr <= 7) {
  print(paste("ctr is set to", ctr))
Intermediate R

while loop

while(condition) {
  expr
}
while(ctr <= 7) {
  print(paste("ctr is set to", ctr))
  ctr <- ctr + 1
}
Intermediate R

while loop

while(condition) {
  expr
}
while(ctr <= 7) {
  print(paste("ctr is set to", ctr))
  ctr <- ctr + 1
}
"ctr is set to 1"
Intermediate R

while loop

while(condition) {
  expr
}
while(ctr <= 7) {
  print(paste("ctr is set to", ctr))
  ctr <- ctr + 1
}
"ctr is set to 2"
Intermediate R

while loop

while(condition) {
  expr
}
while(ctr <= 7) {
  print(paste("ctr is set to", ctr))
  ctr <- ctr + 1
}
"ctr is set to 7"
Intermediate R

while loop

while(condition) {
  expr
}
while(ctr <= 7) {
  print(paste("ctr is set to", ctr))
  ctr <- ctr + 1
}
  • No printout
Intermediate R

while loop

ctr <- 1
while(ctr <= 7) {
  print(paste("ctr is set to", ctr))
  ctr <- ctr + 1
}
"ctr is set to 1"
"ctr is set to 2"
...
"ctr is set to 7"
ctr
8
Intermediate R

while loop

ctr <- 1
while(ctr <= 7) {
  print(paste("ctr is set to", ctr))
  ctr <- ctr + 1
}
Intermediate R

Infinite while loop

ctr <- 1
while(ctr <= 7) {
  print(paste("ctr is set to", ctr))

}
"ctr is set to 1"
"ctr is set to 1"
"ctr is set to 1"
"ctr is set to 1"
"ctr is set to 1"
"ctr is set to 1"
"ctr is set to 1"
...
Intermediate R

break statement

ctr <- 1
while(ctr <= 7) {
  if(ctr %% 5 == 0) {
    break
  }
  print(paste("ctr is set to", ctr))
  ctr <- ctr + 1
}
"ctr is set to 1"
"ctr is set to 2"
"ctr is set to 3"
"ctr is set to 4"
Intermediate R

Let's practice!

Intermediate R

Preparing Video For Download...