while (condition) {
// code block to be executed
}
The while loop loops through a block of code as long as a specified condition is true:
In the example below, the code in the loop will run, over and over again, as long as a variable (i) is less than 5:.
The example below uses a do/while loop. The loop will always be executed at least once, even if the condition is false, because the code block is executed before the condition is tested:.
Example:In the example below, the code in the loop will run, over and over again, as long as a variable (i) is less than 5:
int i = 0;
while (i < 5) {
printf("%d\n", i);
i++;
}