JavaScript - Loop with condition on the beginning: while
Condition testing is done at the beginning of the loop. before each iteration the condition is tested, if it is was true. before the first iteration is a condition test, the use of this loop is excellent in situations where perhaps we don't need to execute the cycle.while
Description
while (condition) statement
Input
- condition - Logical value The condition can be any expression
- statement - statements
Examples
JavaScript
Loop with condition on the beginning the possible of use:i=0;
while (i<10) ++i;
x =0;
while (i<20)
{
i= i+2;
if (i==16)
{
continue;
}
x++;
}
JavaScript
Other pieces of example codes:while (i>0) if (i==10) break; else { i--; }
while (y>1) { x++; y--; }
if ( x >= 12 ) while ( y >= 0 ) --y;
while ( y <= 32 ) y++;
Loop with condition on the beginning in another programming language:
Differences to: