Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

kotlin Break and Continue


Certainly! In Kotlin, break and continue are control flow statements used within loops to alter the flow of execution.

break Statement

The break statement is used to terminate the loop immediately when a certain condition is met, regardless of whether the loop's condition is still true or not.

Example

fun main() {
for (i in 1..5) {
if (i == 3) {
break // Exit the loop when i is equal to 3
}
println("Number: $i")
}
}

OutPut

Number: 1
Number: 2

continue Statement

The continue statement is used to skip the current iteration of the loop and continue with the next iteration.

Example

fun main() {
for (i in 1..5) {
if (i == 3) {
continue // Skip iteration when i is equal to 3
}
println("Number: $i")
}
}

OutPut

Number: 1
Number: 2
Number: 4
Number: 5

Conclusion

break and continue statements are useful for controlling the flow of loops in Kotlin. break terminates the loop prematurely, while continue skips the current iteration and proceeds to the next one. These statements help you customize the behavior of loops based on specific conditions.



Advertisement





it pathshaala : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java