top of page
Loops!
Loops are coding mechanisms which will repeat based on given parameters, once the parameters are met the loop will stop and the program may continue normal execution. The two most common loop functions are while loops and for loops. Each loop carries their own syntax and uses the while and for functions appropriately.
While Loops
While loops follow the syntax: while(condition){ execute }
While the condition is met, the loop will continue to loop. The loop will not leave until it either no longer meets the condition or is told to break manually. In our example we will ask a user to type the letter 5 and keep them in the loop until they comply.
Output:

Example:

Our loop will continue until x is not equal to 5 as our condition states. We use != for not equal to, this is one example of a rational operator.
The break; function can be called to manually break out of a loop at any time.
Output:

Example:

As predicted the loop only executed once until it hit the break at which point it left the loop and continued on with the program.
bottom of page