Kotlin while Loop: Loop is used in programming to repeat a particular block of code. In this tutorial, you will learn to create while and do…while loops in Kotlin programming.
Loop is used in programming to repeat a particular block of code until certain condition is met (test articulation is false).
Loops are what make computers fascinating machines. Imagine you need to print a sentence 50 times on your screen. All things considered, you can do it by using print articulation 50 times (without using loops). What about you need to print a sentence one million times? You need to use loops.
You will find out around two loops while and do..while in this tutorial with the assistance of examples.
In the event that you know about while and do…while loops in Java, you are as of now acquainted with these loops in Kotlin too.
In this article, you will learn-
Kotlin while Loop
In Kotlin, the while works very much like in Java. In this way, on the off chance that your experience is Java, you definitely know how it works.
The syntax of while loop is:
while (testExpression) { // codes inside body of while loop }
How while loop works?
The test articulation inside the parenthesis is a Boolean expression.
On the off chance that the test articulation is assessed to true,
- statements inside the while loop are executed.
- at that point, the test articulation is assessed once more.
This interaction goes on until the test articulation is assessed to false.
On the off chance that the test articulation is assessed to false,
while loop is ended.
Example: Kotlin while Loop
// Program to print line 5 times fun main(args: Array<String>) { var i = 1 while (i <= 5) { println("Line $i") ++i } }
At the point when you run the program, the output will be:
Line 1 Line 2 Line 3 Line 4 Line 5
Notice, ++i statement inside the while loop. After 5 emphasess, variable I will be augmented to 6. At that point, the test articulation I <= 5 is assessed to false and the loop ends.
On the off chance that the body of the loop has just a single statement, it’s not important to use curly braces { }.
Example: Compute the sum of Natural Numbers
// Program to compute the sum of natural numbers from 1 to 100. fun main(args: Array<String>) { var sum = 0 var i = 100 while (i != 0) { sum += i // sum = sum + i; --i } println("sum = $sum") }
At the point when you run the program, the output will be:
sum = 5050
Here, the variable sum is introduced to 0 and I is instated to 100. In every iteration of the while loop, the variable sum is assigned sum + I, and the value of I is diminished by 1 until I is equivalent to 0. For better perception,
1st iteration: sum = 0+100 = 100, i = 99 2nd iteration: sum = 100+99 = 199, i = 98 3rd iteration: sum = 199+98 = 297, i = 97 ... .. ... ... .. ... 99th iteration: sum = 5047+2 = 5049, i = 1 100th iteration: sum = 5049+1 = 5050, i = 0 (then loop terminates)
To learn more about test expression and how it is evaluated, visit comparison and logical operators.
Kotlin do…while Loop
The do…while loop is like while loop with one key difference. The body of do…while loop is executed once before the test articulation is checked.
Its syntax is:
do { // codes inside body of do while loop } while (testExpression);
How do…while loop functions?
The codes inside the body of do construct is executed once (without checking the testExpression). At that point, the test expression is checked.
In the event that the test expression is assessed to true, codes inside the body of the loop are executed, and test expression is assessed once more. This interaction goes on until the test expression is assessed to false.
At the point when the test expression is assessed to false, do..while loop ends.
Example: Kotlin do…while Loop
The program beneath calculates the sum of numbers entered by the user until user enters 0.
To take input from the user, readline() work is used. Suggested Reading: Kotlin Basic Input
fun main(args: Array<String>) { var sum: Int = 0 var input: String do { print("Enter an integer: ") input = readLine()!! sum += input.toInt() } while (input != "0") println("sum = $sum") }
At the point when you run the program, the output will be a like thing:
Enter an integer: 4 Enter an integer: 3 Enter an integer: 2 Enter an integer: -6 Enter an integer: 0 sum = 3
Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.