Loops

Loops


Loops are very easy and efficient way for a program to perform repeated tasks. There are three types of loops: while loops, do while loops and for loops. Each loop has its form based on when it performs its execution and continuation “check”, but any loop can be structured into any form as they are all essentially equal. Let's look at the different types of loops now.


While Loops


A while loop has the form of:





  //program code



    while(continuation check) {



	//loop code



}

The continuation check is a boolean expression, and if it is evaluated to true (refer to the Introduction to Boolean Logic for a refresher of true/false //link if you need to) the program will then enter the loop and execute the code within the block. Once the code in the block has completed execution, the program checks the continuation condition again. And as long as that continuation check remains true, it will perform the code over and over until the check becomes false.

Important note. You must take care when choosing the boolean expression to be the continuation condition and make sure that it will, at some point, become false otherwise you will end up with an infinite loop. Infinite loops will never end and often times this is an undesirable result and will cause your program to crash.


do while loop


A do while loop has the form of:



  

  //program code



  do {



//loop code



}



while(continuation check);



The difference between a while loop and a do while loop is that the do while loop will always perform at least one iteration of the loop while the while loop may never perform an iteration. This is a subtle, but significant difference to keep in mind when designing your loops.


for loops


A for loop has the structure of:




//program code



for( i = starting value; i < ending value; i++) {



//loop code



}



For loops are best for tasks that are going to be repeated a known number of times. Example: Say we wanted to perform an action 10 times, we would write the for loop as:





//program code



for(i = 0; i < 10; i++) {



//loop code



}



Obviously loops can become quite complex fairly quickly, however, their structure always looks like the previous examples. One extremely useful technique you will see is a nested loop, that is a loop within a loop.


Nested Loops


While a nested loop looks very complicated, if we step back and look at it piece by piece we can see that the code that is executed in the first loop is simply another loop:





//program code



        for(int i = starting value; (i < ending value); i++){

            

            //loop code

            

        }



Now lets add a second loop nested within the first loop.




//program code



    for(int i = starting value; (i < ending value); i++){

            

 	for(int j = starting value; (j < ending value); j++){



	//loop code



	}

            

      }



While it is possible to keep putting loops inside of loops as many times as you wanted, each time you do it significantly increases the amount of time it takes for the computer to perform one iteration. It is best to try and avoid going into a nested loop with 3 or more loops - there is likely a more efficient way to write your code.

Some important keywords you will encounter when working with loops are “break” and “continue”. “break” immediately terminates the loop and causes the program to exit the loop and the program will keep running. They keyword “continue” ends the current iteration of the loop which often sends the program back to check a continuation condition.