IT Questions and Answers :)

Tuesday, August 13, 2019

In Javascript, which of the following is the correct syntax for a "for" loop?

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++) {...} 

In Javascript, which of the following is the correct syntax for a "for" loop?

EXPLANATION

The For Loop

The for 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>";
}

 

Share:

0 comments:

Post a Comment

Popular Posts