While Loop

The while loop is used to execute code repeatedly as long as a condition is true. The syntax for a while loop is as follows:

while (condition) {
  // code to be executed repeatedly
}

The condition statement is evaluated before each iteration of the loop. If the condition is true, the code inside the loop body is executed. If the condition is false, the loop terminates.

Here is an example of a while loop:


				

In this example, the condition statement is i < 10. This statement checks if the value of i is less than 10. If it is, the code inside the loop body is executed. The code inside the loop body increments the value of i by 1.

Task

Edit the about while loop to print 0 1 2 3 4. Run the above code and experiment with the code to get used to it.

ADVERTISEMENT
ADVERTISEMENT