April 2019
Beginner to intermediate
698 pages
15h 15m
English
Kotlin while loops have the simplest syntax. Think back to the if statements for a moment; we could use virtually any combination of operators and variables in the conditional expression of the if statement. If the expression evaluated to true, then the code in the body of the if block is executed. With the while loop, we also use an expression that can evaluate to true or false:
var x = 10
while(x > 0) {
Log.i("x=", "$x")
x--
}Take a look at the preceding code; what happens here is as follows:
while loop, an Int type named x is declared and initialized to 10. while loop begins; its condition is x > 0. So, the while loop will execute the code in its body.Read now
Unlock full access