Kotlin while and do while Loop

Before we discuss loops, let’s go back in time and remember those days when we used to be punished and teachers told us to write: “I will not repeat the same mistake” a 100 times. Understanding that pain and easing that effort programming languages came up with repeated conditional statements which ease the programming effort by simply reducing effort.

So a typical console output of “I will not repeat the same mistake” statement written 10 times in Java would be like below:

System.out.println ("I will not repeat the same mistake");
System.out.println ("I will not repeat the same mistake");
System.out.println ("I will not repeat the same mistake");
System.out.println ("I will not repeat the same mistake");
System.out.println ("I will not repeat the same mistake");
System.out.println ("I will not repeat the same mistake");
System.out.println ("I will not repeat the same mistake");
System.out.println ("I will not repeat the same mistake");
System.out.println ("I will not repeat the same mistake");
System.out.println ("I will not repeat the same mistake");

This whole effort would have reduced to below, thanks to the wonderful gift of loops:

fun main(){
	var i = 0;
	
	while(i<10){
		println("I will not make the same mistake again.");
		i++;
	}
}

The above code prints the output as below:

I will not make the same mistake again.
I will not make the same mistake again.
I will not make the same mistake again.
I will not make the same mistake again.
I will not make the same mistake again.
I will not make the same mistake again.
I will not make the same mistake again.
I will not make the same mistake again.
I will not make the same mistake again.
I will not make the same mistake again.

Now, lets have a look at the while loop.

Kotlin while Loop

While loop is Entry Controlled loop, before moving ahead let’s find out what entry controlled means:

Entry Controlled means a loop in which the required condition needs to be matched before the code is processed.

Lets have a look at the syntax of the while loop for Kotlin, though it is very similar to any traditional loops.

Syntax:

while (<Test Condition>) {
    // Body of the loop: To be Executed on Repetition
}

So as shown in the syntax, Test Condition is the one which we need to be tested every time before the loop continues execution. Like in example above we used condition as (i<10).

while loop

Now let’s take an example to print first 10 even numbers :

fun main(){
	var count = 0;

	while (count<20){
		println("Even Number is : $count");
		count=count+2;
	}
}

The output of the above code is something like:

Even Number is : 0
Even Number is : 2
Even Number is : 4
Even Number is : 6
Even Number is : 8
Even Number is : 10
Even Number is : 12
Even Number is : 14
Even Number is : 16
Even Number is : 18

Based on the example above let’s have a look at the working of while loop.

  • The core or underlining logic of while loop is based on the entry condition, the entry condition should return a Boolean for the loop to execute successfully.
  • If the Conditional Expression evaluated returns ‘TRUE’:
    • Body of the loop is executed.
    • Logic for increment or decrement of loop variable is defined.
    • The conditional expression is evaluated again and the cycle continues.
  • If the Conditional Expression evaluated returns ‘FALSE’:
    • Loop execution is terminated.

Now, let’s take an example to find sum of first 100 natural numbers:

So our logic in this case would be as start from 1 and traverse it to 100 and compute the sum of the numbers and after termination of loop print the sum variable.

fun main(){
	var sum = 0
	var count = 1
	
	while (count <= 100){
		sum = count+sum;
		count++;
	}

	println("Sum of first 100 Natural Numbers is = $sum");
}

Output of the code is as below:

Sum of first 100 Natural Numbers is = 5050

while loop gives a bundle of options in which you many conditional and logical operators in the Conditional Expressions, you can have a look at these tutorials to get a brief about them.

To end up, we found here that entry condition is matched but in the case we used in example above even if we would not have used the condition validation at the beginning it would have executed, since loop starting value was pre-defined and clear.

When the entry condition is defined and we can avoid it in those cases we use EXIT CONTROLLED LOOP. In simple words, checking the condition or Boolean expression at ending rather than before start of loop execution.

Kotlin do while Loop

Syntax of do while is very similar to the while, it is just the condition evaluation is done after body execution. Let’s have a look at the typical syntax of the do-while loop:

Syntax:

do{
	// Body of the loop: To be Executed on Repetition Even Before Condition
	//checking
} while (<Test Condition>);

do while loop

Let’s see the example we did for printing sum of first 10 natural numbers using the do while loop:

fun main() {
	var sum = 0
	var count = 1

	do {
		sum = count+sum;
		println("Sum after $count natural numbers is : $sum");
		count++;
	} while (count <= 10);

	println("Sum of first 100 Natural Numbers is = $sum");
}

The output of the above code is as below:

Sum after 1 natural numbers is : 1
Sum after 2 natural numbers is : 3
Sum after 3 natural numbers is : 6
Sum after 4 natural numbers is : 10
Sum after 5 natural numbers is : 15
Sum after 6 natural numbers is : 21
Sum after 7 natural numbers is : 28
Sum after 8 natural numbers is : 36
Sum after 9 natural numbers is : 45
Sum after 10 natural numbers is : 55
Sum of first 100 Natural Numbers is = 55

As shown in the case above, it is evident that the loop executes itself first before even checking the condition.

Thats all about loops which was to be covered, stay tuned for next tutorials.

Image Source: http://python.cs.southern.edu/csharpbook/html/ch08s03.html

Leave a Comment