While-loop

R för medelnivå

Filip Schouwenaars

DataCamp Instructor

while-loop

while(condition) {
  expr
}
while(ctr <= 7) {
R för medelnivå

while-loop

while(condition) {
  expr
}
while(ctr <= 7) {
  print(paste("ctr is set to", ctr))
R för medelnivå

while-loop

while(condition) {
  expr
}
while(ctr <= 7) {
  print(paste("ctr is set to", ctr))
  ctr <- ctr + 1
}
R för medelnivå

while-loop

while(condition) {
  expr
}
while(ctr <= 7) {
  print(paste("ctr is set to", ctr))
  ctr <- ctr + 1
}
"ctr is set to 1"
R för medelnivå

while-loop

while(condition) {
  expr
}
while(ctr <= 7) {
  print(paste("ctr is set to", ctr))
  ctr <- ctr + 1
}
"ctr is set to 2"
R för medelnivå

while-loop

while(condition) {
  expr
}
while(ctr <= 7) {
  print(paste("ctr is set to", ctr))
  ctr <- ctr + 1
}
"ctr is set to 7"
R för medelnivå

while-loop

while(condition) {
  expr
}
while(ctr <= 7) {
  print(paste("ctr is set to", ctr))
  ctr <- ctr + 1
}
  • Ingen utskrift
R för medelnivå

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
R för medelnivå

while-loop

ctr <- 1
while(ctr <= 7) {
  print(paste("ctr is set to", ctr))
  ctr <- ctr + 1
}
R för medelnivå

Oändlig 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"
...
R för medelnivå

break-satsen

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"
R för medelnivå

Nu kör vi en övning!

R för medelnivå

Preparing Video For Download...