For, While, and Nested Loops

For Loops

We often want to repeat (or iterate, or loop) either one command or a sequence of commands. For example, if we wanted a dog named “spot” to roll on the floor five times, we could write:


spot.rollOnFloor();

spot.rollOnFloor();

spot.rollOnFloor();

spot.rollOnFloor();

spot.rollOnFloor(); //this line of code was written out 5 times. It tells our dog spot to roll on the floor


However, writing out commands multiple times can be inconvenient and waste time, especially if you want to repeat a command for hundreds, or even thousands of times! If we know beforehand how many times to iterate (definite iteration), we can use a for-loop:

int k; //this line defines the variable k as an integer

for (k = 1; k <= 5; k++) //this line contains the for-loop

{

spot.rollOnFloor(); //this line is the command that we want to repeat

}

Each for-loop has three parts: where the variable k begins (what number it first equals; k = 1 is the initialization of the variable), where it ends (its final value), and how k’s value increases with each loop. The command k++ causes the value of k to increase by one each time the loop repeats. If k was 1, it becomes 2 after the first loop; after the second loop, k becomes 3, and so on, until k becomes 5. Because the second part of the “directions” for the for-loop (k <= 5) says that k must be less than or equal to 5 for the loop to continue, the for-loop stops repeating after k = 5. Therefore, spot only rolls on the floor 5 times. Together, the actions and conditions of the variable k inside the for-loop parentheses are called the counter, because they keep track of how many times the loop repeats.

If we want to repeat multiple commands in a for-loop, they must be enclosed by curly brackets {} for the for-loop to repeat all the commands we want it to every time it loops. However, if you only want to repeat one command, like in the example above (rollOnFloor), the curly brackets are optional.

While Loops

What should you do if you don’t know how many times you need to loop a command? For example, what if you need to practice the piano a certain number of times until you can play music perfectly? In computer science, these situations are called indefinite iterations because we don’t know how many times we should repeat a command. Luckily, we can use a while-loop to repeat a command (practice piano) for as long as we want it to, until a certain condition (play music perfectly) is satisfied. Here’s how to write it out:


while (musician.playsImperfectly())

{

musician.practice(); // this command will repeat for as long as the musician plays imperfectly

}


Since we want the musician to play perfectly before exiting the practice loop, the condition that the while loop assesses each time it repeats is whether or not the musician plays imperfectly. So long as the musician is playing imperfectly, the loop will repeat, and the musician will practice once again. When the while-loop’s condition (musician.playsImperfectly()) is no longer true--that is, the musician is now playing perfectly--the loop terminates, and the musician will no longer repeat the commands inside the while-loop.

Just like with for-loops, commands that are repeated using while-loops must be enclosed with curly brackets to let the while-loop know that you want to repeat all of the enclosed commands every time it loops. If you just want to repeat one command, the brackets are optional.

Nested Loop

A nested loop is simply a loop within a loop. If you were to put a for-loop inside a for-loop, for example, the inner for-loop would repeat a command a certain number of times. Then, once it’s finished the outer for-loop repeats itself, causing the inner for loop to initiate once again!


int k;

for (k = 1; k <= 5; k++) // this is the outer for-loop

{

int a;

for (a = 1; a <= 3; a++) // this is the inner, or nested for-loop

{

spot.rollOnFloor();

}

}


Every time the outer for-loop repeats, the inner for-loop must initiate itself once again. For example, the first time the outer for-loop runs its course (when k = 1), the inner for-loop initiates one time. However, because the inner for-loop repeats 3 times, spot rolls on the floor 3 times before we can end both the inner and outer for-loop. This means that, each time the outer for-loop repeats, spot rolls on the floor 3 times. The outer for-loop repeats 5 times, so spot rolls on the floor for a total of 5 x 3 = 15 times.

Nested loops can be made up of for-loops, while-loops, or even a mixture of the two.