For loop with Break and continue and label
Break
Break statement is used to stop the iteration of its nearest loop
1
2
Continue
Continue statement is used to skip the current iteration
Output:
1
3
4
5
Labeled for loop with break statement
Output:
a is 1 and b is 1
Labeled for loop with continue statement
Break statement is used to stop the iteration of its nearest loop
fun main(args:Array<String>) { for (a in 1..5) { println(a) if (a==2) break } }Output:
1
2
Continue
Continue statement is used to skip the current iteration
fun main(args:Array<String>) { for (a in 1..5) { if (a==2) continue
println(a) } }
Output:
1
3
4
5
Labeled for loop with break statement
fun main(args:Array<String>) { myLoop@ for (a in 1..3) { for (b in 1..3) { if (b==2) break@myLoop
println("a is $a and b is $b") } } }
Output:
a is 1 and b is 1
Labeled for loop with continue statement
fun main(args:Array<String>) { myLoop@ for (a in 1..3) { for (b in 1..3) { if (b==2) continue@myLoop
println("a is $a and b is $b") } } }
Output:a is 1 and b is 1 a is 2 and b is 1 a is 3 and b is 1
Comments
Post a Comment