Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

Nested Loops

A loop can contain another loop. This is called a nested loop. In each iteration of the outer loop, the inner loop runs all the way through. Let's list every lunch a canteen offers: every meal with every drink.

std::vector<std::string> meals = {"pizza", "pasta"};
std::vector<std::string> drinks = {"water", "juice", "tea"};
for (std::string meal : meals) {
    for (std::string drink : drinks) {
        std::cout << meal << " " << drink << std::endl;
    }
}

This prints:

pizza water
pizza juice
pizza tea
pasta water
pasta juice
pasta tea

The outer loop takes "pizza", and the inner loop goes through all three drinks. Only then does the outer loop take "pasta", and the inner loop goes through all the drinks again. The inner block runs 2 × 3 = 6 times. Give the variables of the two loops different names (here meal and drink), so that you know which is which.

Nested loops are also handy for tables. Here, both loops count with an index. The inner loop prints one row on one line, and std::endl after it ends the row:

for (int row = 1; row <= 3; row++) {
    for (int column = 1; column <= 5; column++) {
        std::cout << row * column << " ";
    }
    std::cout << std::endl;
}
1 2 3 4 5 
2 4 6 8 10 
3 6 9 12 15 

Look at where std::cout << std::endl; is: after the closing brace of the inner loop, but still inside the outer loop. It runs each time the inner loop has printed a whole row. Inside the inner loop, it would put every number on its own line. After the outer loop, it would put all the numbers on one long line.

Instructions

  1. 1

    A travel agency offers a trip to each city in cities with each activity in activities. Write a loop over cities and, nested inside it, a loop over activities. Print every pair on its own line: the city, one space and the activity. The output has 12 such lines and starts with Prague Tennis, Prague Shopping, Prague Sightseeing, Brno Tennis…

  2. 2

    Below that, print a multiplication table from 1 to 5: five rows with five numbers each, separated by spaces. The first row is 1 2 3 4 5, the second one 2 4 6 8 10 and the last one 5 10 15 20 25. Use two nested for loops with an index, and end each row with std::endl in the outer loop.

Back

12/13

  1. 1. The while Loop
  2. 2. Infinite Loops
  3. 3. The for Loop
  4. 4. Looping over a Vector
  5. 5. Loops and Text
  6. 6. Sum and Average
  7. 7. Counting
  8. 8. The Largest Value
  9. 9. Building a New Vector
  10. 10. The break Statement
  11. 11. The continue Statement
  12. 12. Nested Loops
  13. 13. Summary
Next

Try it yourself

Do this exercise right here in your browser. No account needed.