The for loop can be used similarly to the while loop. The for loop works best when you are using a counter variable to loop a set number of times. The following is an example for loop that outputs 12345678910
Variable |
|
|
|
for | (int X = 1; | X <= 10; | X++) |
{ cout << X; } |
After the
for statement, there are
three items within the parentheses:
(1) The variable declaration - this executes one time at the beginning of the
loop
(2) The condition – this condition is tested before each loop. If its
false, then the loop halts.
(3) The increment – this assignment statement is executed after each loop. Example increments are:
X++, X--,
X=X+2, X=X*2.
The next program demonstrates a basic
for loop.
Program 13.1 – Basic For Loop | |
// Using a For Loop to count from 10 to 1
int main()
return 0; |
// Same program with a While Loop
int main()
return 0; |
Output |
Using a for loop may be helpful in certain mathematical calculations. One example is to write a program that calculates the factorial of a number. As you recall, 6! means to multiply 6*5*4*3*2*1. The next program prompts the user for a number, and then calculates the factorial of that number.
Program 13.2 – For Loop for Calculating Factorials |
#include <iostream>
int main() |
Output |
In the preceding program, we first declare two integers – Num to store the number the user inputs, and Answer to store the store the answer (factorial of Num). We then prompt the user to enter an integer for calculating a factorial. In the for loop, the variable X goes from 1 to 6 (the value of Num). Therefore, the loop does the following:
Answer = Answer * 1;
Answer = Answer * 2;
Answer = Answer * 3;
Answer = Answer * 4;
Answer = Answer * 5;
Answer = Answer * 6;
// This gives us Answer = 1 * 2 * 3 * 4 * 5 * 6
There is a third loop not covered in this book – the do...while loop. It is very similar to the while loop except that the condition is located at the end of the loop. The condition is not tested until the loop executes once.
Remember the order in which a for and while loop executes: The condition is tested first. If it is false, then the program will bypass the loop and go to the next line of code after the loop (after the braces). Otherwise, all statements within the braces are executed. After each loop iteration, the condition is tested again to determine if the loop should continue executing.
Take note of the ending value of variables used as increments in loops. Look at program 12.1. What is the value of the variable Count at the end of the program? There may be a tendency to say 1, but the correct answer is 0. Remember that Count must be decremented (Count--) down to 0 before the condition (Count >=1) evaluates to false.
There are two statements that may be used in loops: break and continue. Many instructors will advise against using these because they can cause a program to be more difficult to understand. The break statement will cause the loop to terminate immediately. The following code will print: 123456.
for (int Counter=1; Counter<10; Counter++)
{
cout << Counter;
if (Counter > 5)
break;
}
The continue statement will cause a loop to immediately begin again (next iteration). The following code will print: 5566778910.
for (int A=5; A<=10; A++)
{
cout << A;
if (A > 7)
continue;
cout << A;
}
The goto statement is also often advised against using since it causes such a dramatic change in the flow of your program. It can be a useful debugging tool, however. The goto statement will cause the program to skip immediately to the specified label. The following code will print: 1 4.
cout << "1 ";
goto Here;
cout << "2 ";
cout << "3 ";
Here:
cout << "4 ";
What will each of the following for loops output?
1.
for (int A=1; A<=5; A++)
{
cout << A << " ";
}
2.
for (int A=10; A>0; A--)
{
cout << A << " ";
}
3.
for (int Z=2; Z<=20; Z=Z+2)
{
cout << Z;
}
4.
for (float X=100; X>=0; X = X / 2 - 2)
{
cout << X << " ";
}
5.
int A = 1;
for (int X=1; X<5; X++)
{
A = A * 2 – X;
}
cout << A;
6.
for (int I=1; I<=5; I++)
{
for (int J=1; J<=I; J++)
{
cout << "*";
}
cout << endl;
}
7. Write a for loop that will print the following series of numbers:
3 6 9 12 15 18 21
8. Write a for loop that will print the following series of numbers:
4 2 0 -2 -4
9. Write a for loop that will print the following series of numbers:
1 3 7 15 31 63 127
10. Write a nested for loops (similar to exercise 6) that print the following:
*****
****
***
**
*