back
Gettin' Loopy with Loops
You are now going to add a powerful series of tools to your programming skills: loops. Loops are technically known as iteration structures because every run through a program is called an iteration. Loops allow you to keep your program running. Up to now, your programs have run through once and terminated. By adding loops, you can have the user continue to run your program over and over. This is super useful, especially in a situation like a game where you want the user to keep score. Let's start with simplest of all loops: the FOR loop.

For loops have a basic structure: the

Let's break down what this means. Inside the parenthesis there are three parts:
  • the initializing expression
  • the control expression
  • the step expression


Let's take a look at these indiviually.

The Initializing Expression

A loop is a type of counter. It counts how many times a program runs through a certain number of steps. The variable that counters use is customarily "i."

Think of the counter as the start button on a stop watch. Before you use a stop watch, what do you need to do? You set it. When you initialize the "i" variable to 1, you are telling the loop to begin counting at one. If you don't initialize the variable, it doesn't know from where to start counting!

The Control Expression

The control expression sets the limits on how long the loop should last. You can decide whether the program loops through one time or a million times. If you set the control expression to say, i < 3, you will make the loop stop when i reaches a value of three.

The Step Expression

Adding ++ next to a variable as in:

i++;

Takes whatever is in the variable and automatically adds one to it. Similarly:

i--;

Does the reverse. It subtracts one from whatever value is in "i."

The step expression, therefore, tells the loop how much it should increase or decrease in value as it loops. As you will see in the following examples, the step expression is not limited to adding one or subtracting one. You could write a step expression like:

i = i + 10;

which would add ten to the amount each time.

Take a look at this text file [loop one example]. Copy and past it into your compiler and then run it. Save it in your folder and name it "loop_1."

Now, take a look at how you can make the same program go in reverse. Copy this file, [loop two example] run it and save it as "loop_2."

Next, we can make a loop that steps up by more than just one. Run this file, [loop three example]and compare it to the others. Save it as "loop_3."

We can also make loops exectute multiple statements. Run this file and save it as [loop four example]"loop_4."

While Loops

Another type of loop is the while loop.
while

You could, for example, say:

while(number <= 10)
{
cout « number;
}

Copy and paste this example of a while loop [while loop example 1. Run the code and save it in your folder as "while_loop_1." Try entering a variety of numbers to see how the program reacts.

Finally, check out this example of another type of while loop called the "do while" loop:

[while loop example 2

Save it in your folder as "while_loop_2."

A Do While loop tests the control expression at the end of the loop instead of at the top.