In Javascript, which of the following is the correct syntax for a "for" loop?
- for (i = 0; i++; i < 5) {...}
- for (i = 0; i < 5) {...}
- for (i < 5; i++) {...}
- for (i = 0; i < 5; i++) {...}
EXPLANATION
The For Loop
Thefor
loop has the following syntax:
for (statement 1; statement 2; statement 3) {
// code block to be executed
}
Statement 1 is executed (one time) before the execution of the code block.Statement 2 defines the condition for executing the code block.
Statement 3 is executed (every time) after the code block has been executed.
Example
for (i = 0; i < 5; i++) {text += "The number is " + i + "<br>";
}
0 comments:
Post a Comment