Kotlin Control Statements : If When For
Kotlin Control Statements : Let’s explore the use of If, When (Switch alternative), For, While, Do-While, Break and Continue statements.
In Kotlin, a modern and versatile programming language, developers can leverage a range of control statements to write efficient and readable code.
We will unfold Kotlin’s control statements and discover how they can simplify your code and improve control flow.
Kotlin If Expression
Kotlin’s “if” expression is a powerful control statement that allows you to execute a block of code conditionally. Unlike many other languages, Kotlin’s “if” expression is an expression itself, meaning it returns a value.
This feature enables you to assign the result of an “if” expression directly to a variable. It can be used in both simple and advanced forms.
If example in Kotlin
Write the following code on Kotlin playground or IntelliJ IDEA.
fun main() {
val data = false
val result = if (data) { true } else { false }
println(result)
}
false
Find Odd or Even using Kotlin If Statement
fun main()
{
println("Enter a number :")
val number = readln().toInt()
if(number%2==0)
{
println("$number is even")
}else {
println("$number is odd")
}
}
Enter a number :
121
121 is odd
The code prompts the user to enter a number and reads the input as an integer number. It then checks whether the number is divisible by 2 (even) or not.
If the number is even, it prints a message stating that the number is even. Otherwise, It prints a message stating that the number is odd.
Kotlin : When expression
The “when” expression in Kotlin is an enhanced version of the traditional “switch” statement. It enables pattern matching and the execution of code blocks based on different conditions.
The “when” expression is highly versatile and supports various matching options, including value checking, ranges, types, and custom conditions.
When statement example in Kotlin
package control_statements
fun main()
{
println("Enter any number:")
val value = readln().toInt()
when(value)
{
1 -> println("value is 1")
2 -> println("value is 2")
in 3..10 -> println("value is between 3 and 10")
is Int -> println("value is an integer")
else -> println("value is something else")
}
}
Sample Input 1
Enter any number:
10
value is between 3 and 10
Sample Input 2
Enter any number:
2
value is 2
Sample Input 3
Enter any number:
89
value is an integer
Kotlin: For Loop
Kotlin’s “for” loop simplifies iteration over collections, ranges, or any other iterable object. It automatically handles indexing and provides a clean syntax for looping.
You can iterate over arrays, lists, maps, and custom objects with the “for” loop.
For loop example in Kotlin
fun main()
{
val list = listOf<Int>(4,5,6,677,77)
for (item in list)
{
println(item)
}
}
Program output
4
5
6
677
77
Kotlin : While and Do-While Loops
Kotlin supports both “while” and “do-while” loops for iterative execution. The “while” loop continues executing a block of code as long as a condition is true, while the “do-while” loop executes the block at least once and then continues as long as the condition remains true.
Kotlin While and Do-While Example
fun main()
{
var i = 0
while (i < 5) {
println(i)
i++
}
println("___")
var j = 0
do {
println(j)
j++
} while (j < 5)
}
Output
0
1
2
3
4
___
0
1
2
3
4
Kotlin : Break and Continue Statements
To modify the control flow within loops, Kotlin provides the “break” and “continue” statements. The “break” statement allows you to exit a loop prematurely, while the “continue” statement skips the current iteration and moves to the next one.
Kotlin Break and Continue example
fun main()
{
for (i in 1..10) {
if (i == 5) {
break
}
println(i)
}
println("---")
for (i in 1..10) {
if (i % 2 == 0) {
continue
}
println(i)
}
}
Output
1
2
3
4
---
1
3
5
7
9
Overview
Kotlin’s control statements offer powerful tools for managing the flow of your code. The “if” and “when” expressions enable conditional execution, while the “for” loop simplifies iteration. Additionally, the “while” and “do-while” loops provide flexible options for iterative execution. By utilizing the “break” and “continue” statements, you can customize the control flow within loops.