Kotlin is a statically typed programming language that runs on the Java Virtual Machine (JVM). It was developed by JetBrains and is now an official language for Android development. Kotlin is designed to be concise, expressive, and safe. It is also interoperable with Java, which means that you can use Kotlin and Java code together in the same project.
Kotlin When & While Loop, Break & Continue (Lesson 8 PDF)
Overview about the Lesson
This lesson is part of a series of lessons on Kotlin programming. In this lesson, we will cover the use of When & While Loop, Break & Continue statements in Kotlin. We will start by discussing what these statements are and how they work.
We will then explore the different types of When & While Loop, Break & Continue statements in Kotlin. Finally, we will look at some examples of how to use these statements in Kotlin.




The Content Of The Lesson
The content of this lesson is divided into the following sections:
- What are When & While Loop, Break & Continue statements?
- When statement
- While Loop statement
- Break statement
- Continue statement
- Examples
Why This Lesson
This lesson is important because When & While Loop, Break & Continue statements are essential parts of any programming language. They allow us to make decisions based on conditions, which is essential for building complex applications. By understanding When & While Loop, Break & Continue statements in Kotlin, you will be able to write more efficient and effective code.
when, while, break and continue: Examples
This lesson’s PDF covers Kotlin’s when expression and while loops. Here are clear examples with the output each one prints.
The when expression
when replaces long if…else if chains and Java’s switch statement.
fun main() {
val day = 5
val name = when (day) {
1 -> "Monday"
2 -> "Tuesday"
3 -> "Wednesday"
4 -> "Thursday"
5 -> "Friday"
6, 7 -> "Weekend"
else -> "Invalid day"
}
println(name) // Friday
}You can match several values (6, 7), ranges and types:
fun grade(score: Int) = when (score) {
in 90..100 -> "A"
in 80..89 -> "B"
in 70..79 -> "C"
else -> "Needs work"
}
fun describe(x: Any) = when (x) {
is Int -> "A number"
is String -> "Text of length ${x.length}"
else -> "Something else"
}when without an argument works like a cleaner if-else chain: when { temp > 30 -> "Hot"; else -> "OK" }.
while and do-while loops
fun main() {
var count = 1
while (count <= 3) {
println("Count: $count")
count++
}
var input: Int
do {
input = (1..6).random()
println("Rolled $input")
} while (input != 6)
}while checks the condition first, so the body may never run. do…while always runs the body at least once, which suits “try until success” logic like the dice roll above.
break and continue
fun main() {
var i = 0
while (i < 10) {
i++
if (i % 2 == 0) continue // skip even numbers
if (i > 7) break // stop the loop completely
println(i)
}
}
// Output: 1 3 5 7 (each on its own line)continueskips the rest of the current iteration and moves to the next one.breakexits the loop immediately.
Avoid infinite loops
Make sure something inside a while loop changes the condition. Forgetting count++ above would print “Count: 1” forever. In IntelliJ, stop a runaway program with the red Stop button.
Practice exercise
Keep doubling a number starting from 1 and print each value until it exceeds 100.
fun main() {
var n = 1
while (n <= 100) {
println(n)
n *= 2
}
}
// Output: 1 2 4 8 16 32 64Frequently Asked Questions
Is when better than if-else in Kotlin?
For three or more branches, usually yes: it is easier to read, and when it is used as an expression on an enum or sealed class, the compiler checks that every case is covered.
Does when need an else branch?
When used as an expression, yes, unless the compiler can prove all cases are covered (for example all values of an enum).
Continue the course
← Previous: Lesson 7: Booleans & If…Else | Next: Lesson 9: Arrays →
- Lesson 1: Getting Started
- Lesson 2: Syntax & Output
- Lesson 3: Comments & Variables
- Lesson 4: Data Types
- Lesson 5: Operators
- Lesson 6: Set Operations & Grouping
- Lesson 7: Booleans & If…Else
- Lesson 8: When, While, Break & Continue (you are here)
- Lesson 9: Arrays
- Lesson 10: For Loops & Ranges
- Lesson 11: Functions & Classes
- Lesson 12: Classes, Objects & Constructors
- Lesson 13: Class Functions & Inheritance
Conclusion
In conclusion, When & While Loop, Break & Continue statements are essential parts of any programming language. In this lesson, we covered the different types of When & While Loop, Break & Continue statements in Kotlin. We also looked at some examples of how to use these statements in Kotlin.
By understanding When & While Loop, Break & Continue statements in Kotlin, you will be able to write more efficient and effective code.
Download From Link
You can download the lesson from the following link:













