June 2018
Beginner
722 pages
18h 47m
English
Similarly to the while statement, the do...while statement executes the Boolean expression repeatedly and the statement or block until the value of the Boolean expression evaluates as false:
do { //statement or block} while (Boolean expression)
But it executes the statement or the block first before the expression is evaluated, which means that the statement or block is executed at least once.
Let's look at some examples. The following code executes the printing statement six times (one more than the similar while statement):
int i = 0;do { System.out.print(i + " "); //prints: 0 1 2 3 4 5} while(i++ < 5);
While the following code behaves the same way as a while statement:
double result = 0d;do { result += tryAndGetValue(); System. ...
Read now
Unlock full access