R break statement

R break statement is used inside a loop to stop iteration. Flow the control outside of the loop

Syntax:

break

Break in for loop

x<-1:10 # Define variable x

for (i in x) { if(i==5) { break } print(i) }

Output:

[1] 1
[1] 2
[1] 3
[1] 4

techs<-c("R","Python","SAS","MySQL")

for (i in techs) { if(i==’MySQL’) { break } print(i) }

Ouput:

[1] “R”
[1] “Python”
[1] “SAS”

Break in while loop

i<-1 # Define variable i

while(i<10) { print(i) i=i+1 if(i==5) break }

Ouput:
[1] 1
[1] 2
[1] 3
[1] 4

Powered by WordPress