Kotlin Control Flow

9. Kotlin Control Flow 9.1 Kotlin IF Statement 9.2 Kotlin IF Else Statement 9.3 Kotlin Multiple Conditions 9.4 Kotlin When Expression 9.5 Kotlin For Loop 9.6 Kotlin While Loop 9.7 Kotlin Do While Loop 9.8 Kotlin Range


Kotlin IF Statement

The if statement is the most commonly used in conditional control statements. It returns a boolean value. If the condition in the 'if statement' is true, then the statements enclosed within it are executed.

Example

fun main(args : Array<String>)  { 
	var a = 10
	if(a < 50) println("a is less than 50")
}
Output
a is less than 50

We can also place the statements within curly braces.

fun main(args : Array<String>)  { 
	var a = 10
	if(a < 50) {
		println("a is less than 50")
	}
}
Output
43
12
22

Kotlin IF Else Statement

In this, if the expression within the if statement is true, then the statements within the if are executed otherwise the statements within the else are executed.

Example

fun main(args : Array<String>)  { 
	var a = 10
	if(a < 50) {
		println("a is less than 50")
	} else {
		print("a is greater than 50")
	}
}
Output
a is less than 50




Kotlin Multiple Conditions

If ...elseif.... else statements are used for more than one condition.

Example

fun main(args : Array<String>)  { 
	var a = 10
	if(a < 50) {
		println("a is less than 50")
	} else if (a == 50) {
		println("a is equal to 50")
	} else {
		println("a is greater than 50")
	}
}
Output
a is less than 50

Kotlin When Expression

In kotlin, the when expression is used if there is need to match the value from many different cases and execute the statements within the matched case. When statement executes line by line and read all the cases. This is replacement of switch statement from other language like PHP, C, C++ and more powerful then switch statement.

Example

fun main(args : Array<String>)  { 
	var msg = 4
	when(msg) {
		1 -> println("Hello")
		2 -> println("Welcome")
		3 -> println("Good Morning")
		4 -> println("Good Evening")
		5 -> println("Good Night")
		else -> println("Have a great day")
	}
}
Output
Good Evening




Kotlin For Loop

Kotlin provides loop constructs - for loop and while loop like other programming languages.

The for loop is a most popular iterator. It can iterates through a range, lists, any object. This is equivalent to the foreach loop in languages like C#, PHP.

Example

fun main(args : Array<String>)  { 
	val list = listOf(10, 11, 12, 13)
	for (k in list) {
		println(k)
	}
}
Output
10
11
12
13

The above example contains in operator which is always used with for loops.

This is also used to iterate over an array or list with an index.

fun main(args : Array<String>)  { 
    val arr = arrayOf(22,12,43)
    for (x in arr.indices) {
        println("array[$x]")
    }
}
Output
array[0]
array[1]
array[2]

Kotlin While Loop

While loop is used for iteration. In each iteration, it evaluates the truth expression just like the if statement. If the condition evaluates to true, then the statement inside the loop is executed and control goes to the next iteration. If the condition evaluates to false, then the loop ends and control goes out of the loop.

Example

fun main(args : Array<String>)  { 
	var a = 1
	while(a <= 10) {
		println("$a ")
		a++
	}
}
Output
1 
2 
3 
4 
5 
6 
7 
8 
9 
10 

Kotlin Do While Loop

The do while loop is almost similar to the while loop except that the loop statement is executed at least once. In this, the conditional check is written at the end of the loop iteration. So the statement is executed first before the condition is tested.

Example

fun main(args : Array<String>)  { 
    var n = 0;
    do{
        println("Number is $n")
        n++
    }
    while(n <= 5)
}
Output
Number is 0
Number is 1
Number is 2
Number is 3
Number is 4
Number is 5

Range

Kotlin range is a sequence between the given start value and end value. The (..) operator is used to create a range expression which is complemented by in and !in.

Example

fun main(args : Array<String>)  { 
	for(i in 1..10) {
		println(i)
	}
}
Output
1
2
3
4
5
6
7
8
9
10







Read more articles