Topic 7: Repetition Structures

Repetition structures allow you to execute a block of code multiple times. The primary repetition structures in C++ are:

  • for loop
  • while loop
  • do-while loop

for Loop

The for loop is used to repeat a block of code a known number of times.

Syntax:
for (initialization; condition; update) {
    // code to be executed
}
                    
Example: for Loop
  • #include 
    using namespace std;
    
    int main() {
        for (int i = 1; i <= 5; i++) {
            cout << "Iteration " << i << endl;
        }
    
        return 0;
    }
                                        

    Output:

    Iteration 1
    Iteration 2
    Iteration 3
    Iteration 4
    Iteration 5
                                        

while Loop

The while loop is used to repeat a block of code as long as a specified condition is true.

Syntax:
while (condition) {
    // code to be executed
}
                    
Example: while Loop
  • #include 
    using namespace std;
    
    int main() {
        int i = 1;
        while (i <= 5) {
            cout << "Iteration " << i << endl;
            i++;
        }
    
        return 0;
    }
                                        

    Output:

    Iteration 1
    Iteration 2
    Iteration 3
    Iteration 4
    Iteration 5
                                        

do-while Loop

The do-while loop is similar to the while loop, but it guarantees that the block of code will be executed at least once.

Syntax:
do {
    // code to be executed
} while (condition);
                    
Example: do-while Loop
  • #include 
    using namespace std;
    
    int main() {
        int i = 1;
        do {
            cout << "Iteration " << i << endl;
            i++;
        } while (i <= 5);
    
        return 0;
    }
                                        

    Output:

    Iteration 1
    Iteration 2
    Iteration 3
    Iteration 4
    Iteration 5
                                        

Practice Questions

  1. Write a while statement for each of the following cases:
    • Use a counter named i which has an initial value of 1, a final value of 20, and an increment of 1.
    • Use a counter named xcnt that has an initial value of 20.0, a final value of 10.0, and an increment of -0.5.
  2. Write a for statement for each of the following cases:
    • Use a counter named j that has an initial value of 1, a final value of 100, and an increment of 5.
    • Use a counter named xcnt that has an initial value of 20.0, a final value of 10.0, and an increment of -0.5.
  3. Make the iteration table to determine the value in total after each of the following loops is executed:

    1. total = 0; 
      for(i = 1; i <= 10; i = i + 1) 
          total = total + 1; 

    2. total = 0;
      for (i = 10; i <= 15; i = i + 1)
          total = total + 1;
  1. Write a program that calculates the sum of the first n natural numbers (entered by the user) using a loop.
  2. Write a program using the while loop to find the sum of positive numbers. If the user enters a negative number, the loop ends and the negative number entered is not added to the sum. The sum is then displayed.
  3. Create a program that takes an integer and reverses its digits using a loop. Example n = 12345, the reversed number would be 54321.
  4. Write a program to check if a number is prime using a loop.
  5. Create a program that prints the multiplication table for a number n up to a certain range using a loop.
    E.g. if n = 3 and r = 5 then the output would be:
    3 * 1 = 3
    3 * 2 = 6
    3 * 3 = 9
    3 * 4 = 12
    3 * 5 = 15
  6. Write a program that calculates the factorial of a number using a loop.
    The factorial function (symbol: !) says to multiply all whole numbers from chosen number down to 1.
    Example:
    • 4! = 4 × 3 × 2 × 1 = 24
    • 7! = 7 × 6 × 5 × 4 × 3 × 2 × 1 = 5040
    • 1! = 1
  7. Write a program to print the first n numbers in the Fibonacci sequence using a loop.
    The Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
    The next number is found by adding up the two numbers before it:
    • the 2 is found by adding the two numbers before it (1+1)
    • the 3 is found by adding the two numbers before it (1+2)
    • the 5 is (2+3)
    • and so on
    Example: the next number in the sequence above is 21+34 = 55
  8. Create a C++ program that calculates the sum of the digits of a number using a loop.
    • Example if n = 1234 then
    • The sum of the digits would be: 1 + 2 + 3 + 4 = 10
  9. Write a program to convert kilometers/hr to miles/hr. The program should produce a table of 10 conversions, starting at 60 km/hr and incremented by 5 km/hr. The display should have appropriate headings and list each km/hr and its equivalent miles/hr value.
    Use the relationship that 1 kilometer = 0.6241 miles. Implement this program using the FOR loop. Sample output is given below:
  10. Write a program that prints a pattern of stars using nested loops. Example if the n = 5 then the star pattern would be:
    *
    **
    ***
    ****
  11. Four experiments are performed, and each experiment has six test results. The results for each experiment are:
    • 1st experiment results: 23.2 31 16.9 27 25.4 28.6
    • 2nd experiment results: 34.8 45.2 27.9 36.8 33.4 39.4
    • 3rd experiment results: 19.4 16.8 10.2 20.8 18.9 13.4
    • 4th experiment results: 36.9 39 49.2 45.1 42.7 50.6
    Write a program using a nested loop to compute and display the average of the test results for each experiment.
    Sample output is given below:
Coming Soon...