While

While

A while statement or loop has three parts. The first one is the initialization of the variable tested in the condition, before the actual loop. The second part is testing the variable before the loop starts so if the condition is not true the the body is not executed and it is possible that it will not be executed at all. While the last part is change the variable which means that somewhere the variable being tested must change at some point in the lop so that it will to continue on endlessly and crash your program.

The format for a while loop is;

while(condition)

{

<body>

}

Like with other loops and conditional statements if there is only one statement you don’t need the braces. Also, don’t put a semicolon after the while condition because then it will be an empty statement so the statements that were suppose to be in the loop would no longer be there.

One thought on “While

Comments are closed.