-
Book Overview & Buying
-
Table Of Contents
Android Programming for Beginners - Fourth Edition
By :
The do-while loop works in the same way as the ordinary while loop, except that the presence of a do block guarantees that the code will execute at least once, even when the condition of the while expression does not evaluate to true. Look at this code.
var y = 10
do {
y++
Log.d("Loops","In the do block and y= $y")
}
while(y < 10)
If you copy and paste this code into one of your apps' onCreate functions and then run it, the output might not be what you expect. We will build an app that practices all our loops shortly, but as a quick heads-up, here is the output:
In the do block and y= 11
This is a less-used but sometimes-perfect solution to a problem. Even though the condition of the while loop is false, the do block executes its code, incrementing the y variable to 11 and printing a message to the logcat. The condition of the while loop is y < 10, so the code in the do block is not executed again. If the expression in the while condition...