Kotlin Control Flow - if else, for loop, while, range With Examples

In this tutorial, we’ll be covering an important aspect of programming, namely Kotlin Control Flow statements.

We’ll look into the if else, range, for, while, when repeat, continue break keywords that form the core of any programming language code.

Let’s get started with each of these operators by creating a Kotlin project in our IntelliJ Idea.

Kotlin if else

In Kotlin, if else operators behave the same way as it does in Java.
if executes a certain section of code if the condition is true. It can have an optional else clause.
Following code is from the Test.kt class in our IntelliJ Project.

In Kotlin, if else can be used as expressions too in order to return a value.

This way Kotlin eliminates the use of ternary operator by using if else as expressions.

In another view, the last statement present in an if-else condition is returned as the value.
The below example demonstrates the same.

Kotlin for loop

The syntax of for loop in Kotlin is different from the one in Java.
For loop is used to iterate over a list of items based on certain conditions.
Following is the implementation of for loops in Kotlin to print numbers 0 to 5.

Few inferences from the above syntax are listed below:

  • Kotlin saves us from declaring the type of i
  • The lower and upper (including) limits are be defined on either side of .. operator.
  • in keyword is used to iterate over the range.

Another case for you, where we iterate over an array using for-in loop.

To print the index of the elements, we invoke the indices method on the arrays.

To access the index and value of the element in the iteration, withIndex() function is used.

Kotlin forEach loop

ForEach loop repeats a set of statements for each iterable as shown below.

it is the default iterable variable that holds the value of the current iterator.
In the second case, we’d use our custom variable.

Kotlin Range

We’ve seen in the above sections that .. operators represents a range.

To check whether an element exists or doesn’t exist in a range we use the in and !in keywords as shown below.

And the following should work the same way. Right?

NO.
The Range .. can’t be used in the reverse order.
This is where we use the downTo keyword.

The below code would work:

To exclude the last element from the range we use the keyword until.

To traverse the range in steps we use the keyword step.

Kotlin while loop

while and do-while loops in Kotlin behave the same way as they do in Java.

Kotlin break and continue

break is used to exit the loop there and then.
continue is used to go to the next iteration of the loop.
Kotlin gives us the luxury to attach a label to the break and continue statements to indicate the loop on which their actions are triggered as shown below.

Kotlin repeat and when

repeat allows us to execute the statements in the loop N number of times(where N is the number specified as the argument).
The following code snippet would print the group of statements 3 times.

when in Kotlin is equivalent to switch in other languages, though with a different syntax and more power!
A basic example of when operator is given below.

The when operator matches the argument with all the branches. If it matches with none, the else statement is printed. The else statement is similar to default in switch.
when operator can be used to return values too, similar to if else.

Going further, we can use Any type to check the branch as shown below.

Kotlin Control Flow Statements Example

Summing up the control flow in our Kotlin project, this is how our Kotlin class file looks:

This brings an end to this tutorial
References: Kotlin Official Doc, Range API Doc

By admin

Leave a Reply

%d bloggers like this: