Do-While Loop

A variation of the while loop. Executes the loop's block code at least once, before considering the loop's condition.

Syntax

do{
    // the code here will execute at least once
    // before the boolean condition is being considered.
} while(boolean condition);

What is the Difference with a While loop

The do-while evaluates its expression at the bottom of the loop instead of the top. Thus, the statements within the do block are always executed at least once.

Example

The same code at the while loop, print all the numbers between 1 and 10;

int count = 1;
do {
    //print the number here
    count = count + 1;
} while (count <= 10);

results matching ""

    No results matching ""