In our previous tutorial, we learned about C++ While Loops. Today, we’ll learn about for loops.
For loops are different than while loops due to the way they function. This allows one to choose what type of loop to use depending on the problem. For example, while loops are useful for executing statements until a condition is met and a for loop is useful when you know how many iterations you must complete before you get your result.
A for loop works as follows:
- The variable(s) is initialized
- The condition is checked. If true, execute statements, else terminate for loop
- Execute statements within for loop
- Update variables and return to step B
The syntax is below:
for ( initialization; condition; update) { // statements }
For Loop Example
In our previous tutorial, we learned how to count forwards and backwards. In this tutorial, we’ll do something more interesting. We’ll compute the summation from 0 to n, n being user input.
To compute the summation, we simply add up all numbers starting from 0 to n. For example, the sum of 5 would be 0 + 1 + 2 + 3 + 4 + 5 = 15. Taking programming aside, the way we’d solve this step by step would be to have a running sum which we add to until we reach our desired n. Try programming this before you see the solution below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | int n = 0; // User input int sum = 0; // Our running sum cout << "Sum: "; cin >> n; // For loop declaration // We use the variable i to denote an iteration // In this example, we execute the following loop // if i is less than or equal to the user input for ( int i = 0; i <= n; i++ ) { sum += i; } |
For example, if n was 3, the following loop would execute in the following manner.
i = 0; 0 <= 3 sum += 0; sum = 0 i = 1; 1 <= 3 sum += 1; sum = 1; i = 2; 2 <= 3 sum += 2 sum = 3 i = 3; 3 <= 3 sum += 3 sum = 5 i = 4; 4 <= 3 // End of For Loop
For loops advanced
Some things to know about for loops:
- You don’t have to declare a variable within the initialization block of a for loop.
int i; for ( i = 0; i < n; i++ )
- You may have multiple initializations, conditions, and updates if you use a comma in between them.
for ( i = 0, j = 2; i < j; i++ )
- You can break out of a for loop using the following statement:
break;
This is useful for when you encounter a special case in your loop using if statements.
- Likewise you may use continue; statement.
Going further with For Loops
Now that you understand for loops, why not try the following?
- Write the previous for loop using a while loop. This will make you understand when to use a for loop
- Write a for loop that computes the factorial of n.
For example, if n is 3, then the factorial would be fac(3) = 1 x 2 x 3 = 6;
- Using two for loops (not nested), create a program that draws a triangle.
For example, if n is 3, then the appropriate triangle would be
*
**
***
**
* - Using a nested for loop, create a program that will draw a right triangle.
For example, if n is 3, then the appropriate triangle would be
*
**
***

