There is a number of approaches to repeat a block of code while a certain condition is true
. In this lesson, we will learn how to do it using two kinds of loops: while
and do..while
. The difference between these loops is the order of the repetition of a block and the condition evaluation.
The while loop
The while
loop consists of a block of code and a condition that is presented by a boolean expression. If the condition is true
, statements within the block is executed. This repeats until the condition becomes false
. Because this loop checks the condition before the block is executed, the control structure is often also known as a pre-test loop. In some sense, this loop can be thought of as a repeating conditional statement.
The base syntax of the while
loop is the following:
while (condition) {
// body: do something repetitive
}
The loop's body can contain any correct statements: declaring variables, reading data from the standard input, conditional expressions, and even nested loops.
It is also possible to write an infinite loop if the condition is invariably true
while (true) {
// body: do something indefinitely
}
The usage of infinite loops will be considered in the following topics.
Example 1. The following program uses while loop to print integer numbers while a variable is less than 5.
fun main(args: Array<String>) {
var i = 0
while (i < 5) {
println(i)
i++
}
println("Completed")
}
i
. Before the first execution of the loop's body, the program checks i < 5
that is true
(because i
is 0), so the body of the loop starts executing. The body has two statements: displaying the current value of i
and increment it by 1. After this, the expression i < 5
is evaluated again for i
equal 1, which is also true
and the loop's body is repeated again. This is repeated until i
has taken the value 5, after which the expression i < 5
ceases be true
and the execution of this loop terminates. The program proceeds to the next statement followed the loop to print "Completed".0
1
2
3
4
Completed
Loops are also used to process characters, strings and any other data types, not only numbers.
Example 2. The following program displays English letters in a single line.
fun main(args: Array<String>) {
var letter = 'A'
while (letter <= 'Z') {
print(letter)
letter++
}
}
The program takes the first letter 'A'
and then repeats:
- if the letter is less or equal to
'Z'
the program executes to the loop's body, otherwise, it stops the loop. - inside the body, it prints the current character and gets the next letter.
The program prints:
ABCDEFGHIJKLMNOPQRSTUVWXYZ
We remember, that it is possible to get the next character (according to the Unicode table) using the increment operator.
Example 3. This program reads any number of words from the standard input and prints them. It uses hasNext
function of Scanner
to check whether input has a value.
fun main(args: Array<String>) {
val scanner = Scanner(System.`in`)
while (scanner.hasNext()) {
val next = scanner.next()
println(next)
}
}
Kotlin is a modern language
Kotlin
is
a
modern
language
scanner.hasNext()
for strings and scanner.hasNextInt()
for integers when you do not know the size of input data.The do..while loop
do..while
loop, the body is executed first and after the condition is tested. If the condition is true
, statements within the block is executed again. This repeats unill the condition becomes false
. Because do..while
loops check the condition after the block is executed, the control structure is often also known as a post-test loop. In contrast with the while
loop, which tests the condition before the code within the block is executed, the do..while
loop is an exit-condition loop. So, the body is always executed at least once.do
keyword, a body and while(condition)
:do {
// body: do something
} while (condition);
The following program reads an integer number from the standard input and displays the number. If the number 0 is entered the program prints it and then stops. It demonstrates the do..while
loop.
fun main(args: Array<String>) {
val scanner = Scanner(System.`in`)
do {
val n = scanner.nextInt()
println(n)
} while (n > 0)
}
Note, you can use a variable declared in the body of do..while
loop in its condition.
Input numbers:
1 2 4 0 3
The program prints:
1
2
4
0
The do..while
loop can be infinite like the while
loop.do..while
loop is less used than while
. A good example of using this loop is a program that reads data from the standard input until a user entered a certain number or a string. It is assumed, the program will be executed at least once, and repeated execution is optional.