-
Book Overview & Buying
-
Table Of Contents
Android Programming for Beginners - Fourth Edition
By :
Kotlin while loops have syntax that will be familiar to us after our discussions of the previous 2 chapters. 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 is 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 be evaluated to true or false. Look at this code.
var x = 10
while(x > 0) {
Log.d("Loops Demo", " x= $x")
x--
}
In the preceding code, before the while loop, an Int named x is declared and initialized to 10. Next, the while loop begins. Its condition is x > 0. This means the while loop will repeatedly execute the code in its body as long as this condition is true. This should seem logical at this stage, given all we learned in Chapters 4 and 5. Execution will exit the loop body when a test of the loop condition equals false...