Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




KOTLIN - WHILE LOOP


A loop is a concept where something continues endlessly until it is asked to stop.


Just like the sun rises in the east and sets in the west is a loop that continues endlessly.


Let us take the below example to understand loops.


Say, you are asked to print the String "Hello World", five times on the screen.


A simple solution would be,


Example :



fun main() {

    println("Hello World")
    println("Hello World")
    println("Hello World")
    println("Hello World")
    println("Hello World")
}


Output :



 Hello World
 Hello World
 Hello World
 Hello World
 Hello World

So, we have printed "Hello World" five times on the screen.


Simple! Right?


But, what if you are asked to print "Hello World" 100 times or even 1000 times on the screen.


You can't just write 100 or 1000 'print(...)' statements.


And this is where 'loops' comes into picture.


And 'while loop' is one kind loop used by Kotlin.


While Loop


Let us solve the above problem of printing "Hello World" 100 or 1000 times using 'while loop'.


For now, let us rewrite the same program to print "Hello World" 5 times using 'while loop'


Example :



fun main(){
    var x = 1
    while (x <= 5) {
        println("Hello World")
        x = x + 1
    }
}


Output :



 Hello World
 Hello World
 Hello World
 Hello World
 Hello World

So, in the above code, what we have done is, created a variable named 'x' and initialised it with '1'.


var x = 1

java_Collections

Then we have used the while loop to print "Hello World" 5 times.


    
while (x <= 5) {
    println("Hello World")
    x = x + 1
}

So, the first line,


while (x <= 5)

Of the 'while loop' says, continue to be in the loop until the value of 'x' is less than or equal to 5 (i.e. 'x <= 5').


And starts the Iteration(You will understand it eventually).


1st Iteration


So, the control goes inside the 'while loop' and encounters the 'println(...)' statement.


println("Hello World")

And it prints "Hello World" for the first time.


Output :



 Hello World

Then it finds the statement,


x = x + 1

Where the value of 'x' is incremented by 1. So, if you look above, the value of 'x' is '1'.After increasing the value by '1', the new value of 'x' becomes '2'.


java_Collections

After executing both the statements in the 'while loop', the control comes back to the 'while' condition again.


while (x <= 5)

Which says, Continue to be in the loop until the value of 'x' is less than or equal to 5.


And right now the value of 'x' is '2'. So, the control cannot exit the loop and gets into the 'while loop' for second iteration.


2nd Iteration


Once again, the control goes inside the 'while loop' and encounters the 'println(...)' statement.


println("Hello World")

And it prints "Hello World" for the second time.


Output :



 Hello World
 Hello World

Again it finds the statement,


x = x + 1

Where the value of 'x' is incremented by 1. So, if you look above, the value of 'x' is '2'.After increasing the value by '1', the new value of 'x' becomes '3'.


java_Collections

After executing both the statements in the 'while loop', the control comes back to the 'while' condition again.


while (x <= 5)

Which says, Continue to be in the loop until the value of 'x' is less than or equal to 5.


And right now the value of 'x' is '3'. So, the control cannot exit the loop and gets into the 'while loop' for third iteration.


3rd Iteration


Similarly, the control goes inside the 'while loop' and encounters the 'print(...)' statement.


print("Hello World")

And it prints "Hello World" for the third time.


Output :



 Hello World
 Hello World
 Hello World

Again it finds the statement,


x = x + 1

Where the value of 'x' is incremented by 1. So, if you look above, the value of 'x' is '3'.After increasing the value by '1', the new value of 'x' becomes '4'.


java_Collections

After executing both the statements in the 'while loop', the control comes back to the 'while' condition again.


while (x <= 5)

Which says, Continue to be in the loop until the value of 'x' is less than or equal to 5.


And right now the value of 'x' is '4'. So, the control cannot exit the loop and gets into the 'while loop' for fourth iteration.


4th Iteration


Similarly, the control goes inside the 'while loop' and encounters the 'print(...)' statement.


print("Hello World")

And it prints "Hello World" for the fourth time.


Output :



 Hello World
 Hello World
 Hello World
 Hello World

Again it finds the statement,


x = x + 1

Where the value of 'x' is incremented by 1.


java_Collections

After executing both the statements in the 'while loop', the control comes back to the 'while' condition again.


while (x <= 5)

Which says, Continue to be in the loop until the value of 'x' is less than or equal to 5.


And right now the value of 'x' is '5'. So, the control cannot exit the loop and gets into the 'while loop' for fourth iteration.


5th Iteration


Similarly, the control goes inside the 'while loop' and encounters the 'print(...)' statement.


print("Hello World")

And it prints "Hello World" for the fifth time.


Output :



 Hello World
 Hello World
 Hello World
 Hello World
 Hello World

Again it finds the statement,


x = x + 1

Where the value of 'x' is incremented by 1.


java_Collections

After executing both the statements in the 'while loop', the control comes back to the 'while' condition again.


while (x <= 5)

Which says, Continue to be in the loop until the value of 'x' is less than or equal to 5.


And right now the value of 'x' is '6'. And the condition 'x <= 5' fails and the control gets out of the 'while loop' exiting it.


And if you see the final output, "Hello World" is printed 5 times.


Even if you want to print "Hello World" 100 times or 1000 times. Just change the conditional part of 'while loop' and you are done.


Example :



fun main(){
    var x = 1
    while (x <= 1000) {
        println("Hello World")
        x = x + 1
    }
}

So, the above program prints "Hello World" 1000 times.


Now, let us look at the syntax of 'while loop'.


    
while (x <= 1000) {
    println("Hello World")
    x = x + 1
}

The 'while loop' starts with 'while' statement, followed by the conditional part 'x <= 1000',inside brackets '()'.


while (x <= 1000)

And all the statements inside the 'while loop' should be inside braces/flower brackets '{}'.


    
while (x <= 1000) {
    println("Hello World")
    x = x + 1
}

Next, let us look at the 'do - while' loop.