Introduction
The do-while loop in Kotlin is used to repeatedly execute a block of code as long as a specified condition is true. Unlike the while loop, the do-while loop guarantees that the loop body will be executed at least once because the condition is evaluated after the loop body. This chapter will cover the syntax and usage of the do-while loop in Kotlin with examples.
Basic do-while Loop
The do-while loop evaluates a condition after executing the loop body. If the condition is true, the loop body is executed again. This process repeats until the condition becomes false.
Syntax
do {
// Code to execute
} while (condition)
- condition: A Boolean expression that is evaluated after each iteration of the loop.
- block of code: The set of instructions that will be executed repeatedly as long as the condition is true.
Example
fun main() {
var i = 1
do {
println("Iteration $i")
i++
} while (i <= 5)
}
Explanation:
var i = 1: Initializes a variableiwith the value 1.do { ... } while (i <= 5): Executes the loop body first, then checks the conditioni <= 5. Sinceiis 1, the loop body executes.println("Iteration $i"): Prints the current value ofi.i++: Increments the value ofiby 1.- The loop continues until
ibecomes greater than 5.
Example Program with do-while Loop
Here is an example program that demonstrates the use of the do-while loop in Kotlin:
fun main() {
// Basic do-while loop
println("Basic do-while loop:")
var i = 1
do {
println("Iteration $i")
i++
} while (i <= 5)
}
Explanation:
var i = 1: Initializes a variableiwith the value 1.do { ... } while (i <= 5): Executes the loop body first, then checks the conditioni <= 5. Sinceiis 1, the loop body executes.println("Iteration $i"): Prints the current value ofi.i++: Increments the value ofiby 1.- The loop continues until
ibecomes greater than 5.
Using break and continue in do-while Loop
break Statement
The break statement is used to terminate the loop immediately, regardless of the loop condition.
Syntax
break
Example
fun main() {
var i = 1
do {
if (i == 3) {
break
}
println("Iteration $i")
i++
} while (i <= 5)
}
Explanation:
- When
iequals 3, thebreakstatement is executed, and the loop terminates. - The output will be:
Iteration 1 Iteration 2
continue Statement
The continue statement skips the current iteration of the loop and proceeds to the next iteration.
Syntax
continue
Example
fun main() {
var i = 1
do {
if (i == 3) {
i++
continue
}
println("Iteration $i")
i++
} while (i <= 5)
}
Explanation:
- When
iequals 3, thecontinuestatement is executed, skipping the rest of the loop body for that iteration. - The output will be:
Iteration 1 Iteration 2 Iteration 4 Iteration 5
Example Program with do-while Loop, break, and continue
Here is an example program that demonstrates the use of the do-while loop along with the break and continue statements in Kotlin:
fun main() {
// Basic do-while loop
println("Basic do-while loop:")
var i = 1
do {
println("Iteration $i")
i++
} while (i <= 5)
// do-while loop with break
println("\ndo-while loop with break:")
var j = 1
do {
if (j == 3) {
break
}
println("Iteration $j")
j++
} while (j <= 5)
// do-while loop with continue
println("\ndo-while loop with continue:")
var k = 1
do {
if (k == 3) {
k++
continue
}
println("Iteration $k")
k++
} while (k <= 5)
}
Explanation:
- The first
do-whileloop demonstrates a basic iteration from 1 to 5. - The second
do-whileloop includes abreakstatement that terminates the loop whenjequals 3. - The third
do-whileloop includes acontinuestatement that skips the iteration whenkequals 3.
Conclusion
In this chapter, you learned about the do-while loop in Kotlin, including its syntax and usage for repeated execution of code blocks based on conditions. You also saw how to use the break and continue statements to control the flow within do-while loops. Understanding how to use these loops is essential for performing repetitive tasks and managing control flow in your Kotlin programs.