Loops
Loops can execute a block of code as long as a specified condition is reached.
Loops are handy because they save time, reduce errors, and they make code more readable.
Java While Loop
The while loop loops through a block of code as long as a specified condition is true:
Syntax
while (condition) { // code block to be executed }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:
Note: Do not forget to increase the variable used in the condition, otherwise the loop will never end!
The Do/While Loop
The do/while loop is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true.
Syntax
do { // code block to be executed } while (condition);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
int i = 0;do { System.out.println(i); i++; } while (i < 5);
Try it Yourself »
Do not forget to increase the variable used in the condition, otherwise the loop will never end!
The do...while statement creates a loop that executes a specified statement until the test condition evaluates to false. The condition is evaluated after executing the statement, resulting in the specified statement executing at least once.
Try it
Syntax
do statement while (condition)
statementA statement that is executed at least once and is re-executed each time the condition evaluates to true. To execute multiple statements within the loop, use a block statement ({ /* ... */ }) to group those statements.
conditionAn expression evaluated after each pass through the loop. If condition evaluates to true, the statement is re-executed. When condition evaluates to false, control passes to the statement following the do...while.
Note: Use the break statement to stop a loop before condition evaluates to true.
Examples
Using do...while
In the following example, the do...while loop iterates at least once and reiterates until i is no longer less than 5.
let result = ""; let i = 0; do { i += 1; result += `${i} `; } while (i > 0 && i < 5); // Despite i === 0 this will still loop as it starts off without the test console.log(result);
Using an assignment as a condition
In some cases, it can make sense to use an assignment as a condition — but when you do, there's a right way to do it, and a wrong way; the while documentation has a Using an assignment as a condition section with an example showing a general best-practice syntax you should know about and follow.
Specifications
ECMAScript Language Specification # sec-do-while-statement |
Browser compatibility
BCD tables only load in the browser
See also
An expression can include relational operators (such as < or ==) and logical operators (such as &&, ||, or ~). Use the logical operators and and or to create compound expressions. MATLAB® evaluates compound expressions from left to right, adhering to operator precedence rules.
Within the conditional expression of a while...end block, logical operators & and | behave as short-circuit operators. This behavior is the same as && and ||, respectively. Since && and || consistently short-circuit in conditional expressions and statements, it is good practice to use && and || instead of & and | within the expression. For example,
x = 42; while exist('myfunction.m','file') && (myfunction(x) >= pi) disp('Expressions are true') break end
The first part of the expression evaluates to false. Therefore, MATLAB does not need to evaluate the second part of the expression, which would result in an undefined function error.