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

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.

主站蜘蛛池模板: 东城区| 海口市| 天门市| 玉树县| 乳山市| 石楼县| 铜陵市| 胶南市| 竹北市| 南宫市| 武胜县| 太原市| 栾城县| 繁昌县| 新泰市| 建平县| 新绛县| 阜康市| 晋中市| 宿松县| 遂昌县| 巴南区| 依兰县| 华蓥市| 谷城县| 台州市| 仙居县| 高台县| 南岸区| 枝江市| 台中市| 济南市| 三江| 徐汇区| 万全县| 台江县| 永嘉县| 五常市| 南丰县| 项城市| 红原县|