官术网_书友最值得收藏!

Loop control statements

There are control statements that can change the normal sequence of execution. break and next are loop control statements, and we will briefly discuss these control statements here.

break

break terminates the loop and gives control to the next following statement of the loop; for example:

>Vec <- c("Hello") 
>counter <- 5 
>repeat { 
>+   print(Vec) 
>+   counter <- counter + 1 
>+   if(counter > 8) { 
>+      break 
>+   } 
>+} 

As a result of the break statement, when the preceding statement gets executed, it prints Hello four times and then leaves the loop. repeat is another loop construct that keeps executing unless a stop condition is specified.

next

next does not terminate the loop, but skips the current iteration of the flow and goes to the next iteration. See the following example:

>Vec <- c(2,3,4,5,6) 
>for ( i in Vec) { 
>+   if (i == 4) { 
>+      next 
>+   } 
>+   print(i) 
>+} 

In the preceding example, when the iteration goes to the third element of vector Vec, then the control skips the current iteration and goes back to the next iteration. So, when the preceding statement gets executed, it prints vector elements 2, 3, 5, and 6, and skips 4.

主站蜘蛛池模板: 奇台县| 双牌县| 兴仁县| 安康市| 大荔县| 九龙坡区| 绵竹市| 吴旗县| 格尔木市| 崇左市| 鸡泽县| 龙里县| 伊春市| 碌曲县| 定兴县| 新乡县| 江永县| 长沙市| 西华县| 石渠县| 墨竹工卡县| 綦江县| 富民县| 台前县| 偃师市| 新密市| 兰考县| 大姚县| 江油市| 青龙| 峨边| 宣汉县| 安多县| 万盛区| 民和| 鄂伦春自治旗| 蒙城县| 日土县| 响水县| 郎溪县| 绥阳县|