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:

whileloops2.png

Example:

whileloops1.png

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:

whileloops4.png

Example:

whileloops3.png

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.

  • Twitter
  • LinkedIn
  • discord-logo--v2
  • kisspng-github-pages-logo-repository-fork-github-logo-1-magentys-5b69de71b51265

Dragon Eye Intelligence LLC

bottom of page