📖 Scenario: You are organizing a small theater event. You want to print the seating arrangement where rows are labeled 1 to 3 and seats in each row are labeled A to C.
🎯 Goal: Build a program that uses nested loops to print each seat label in the format RowNumberSeatLetter, for example, 1A, 1B, etc.
📋 What You'll Learn
Create two loops: an outer loop for rows and an inner loop for seats
Use the outer loop variable row for row numbers 1 to 3
Use the inner loop variable seat for seat letters 'A' to 'C'
Print each seat label on its own line in the format row followed by seat
💡 Why This Matters
🌍 Real World
Nested loops are useful when you need to work with grids, tables, or any two-dimensional data like seating charts or calendars.
💼 Career
Understanding nested loops is essential for programming tasks involving multiple levels of data, such as game development, UI layouts, and data processing.
Progress0 / 4 steps
1
Create the row numbers
Create an integer variable called row and write a for loop that counts from 1 to 3 inclusive.
C++
Hint
Use a for loop starting at 1 and ending at 3 for the variable row.
2
Create the seat letters
Inside the for loop for row, create another for loop with a character variable called seat that counts from 'A' to 'C' inclusive.
C++
Hint
Use a nested for loop inside the row loop with seat from 'A' to 'C'.
3
Print each seat label
Inside the inner for loop, write a std::cout statement to print the row number followed by the seat letter, then a newline.
C++
Hint
Use std::cout to print row and seat together, then end the line.
4
Run and see the seating labels
Run the program and observe the output. It should print all seat labels from 1A to 3C, each on its own line.
C++
Hint
The output should list all seats from 1A to 3C, each on a new line.
Practice
(1/5)
1. What does a nested loop in C++ mean? for (int i = 0; i < 3; i++) { for (int j = 0; j < 2; j++) { // code } }
easy
A. Two loops running one after another
B. A loop inside another loop
C. A loop that runs only once
D. A loop that never ends
Solution
Step 1: Understand the code structure
The code shows one for loop inside another for loop, which means nested loops.
Step 2: Define nested loops
Nested loops mean one loop runs inside another loop's block of code.
Final Answer:
A loop inside another loop -> Option B
Quick Check:
Nested loop = loop inside loop [OK]
Hint: Look for one loop inside another's braces [OK]
Common Mistakes:
Thinking loops run one after another, not inside
Confusing nested loops with single loops
Assuming nested loops run only once
2. Which of the following is the correct syntax for a nested loop in C++?