Bird
Raised Fist0
C++programming~20 mins

Nested loops in C++ - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Nested Loops Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of nested loops with break
What is the output of this C++ code snippet?
C++
#include <iostream>
int main() {
    for (int i = 1; i <= 3; ++i) {
        for (int j = 1; j <= 3; ++j) {
            if (j == 2) break;
            std::cout << i << j << " ";
        }
    }
    return 0;
}
A12 22 32
B11 12 13 21 22 23 31 32 33
C11 21 31
D11 12 21 22 31 32
Attempts:
2 left
💡 Hint
Look at where the break statement stops the inner loop.
❓ Predict Output
intermediate
2:00remaining
Counting iterations in nested loops
How many times will the inner statement execute in this code?
C++
#include <iostream>
int main() {
    int count = 0;
    for (int i = 0; i < 4; ++i) {
        for (int j = i; j < 4; ++j) {
            ++count;
        }
    }
    std::cout << count;
    return 0;
}
A10
B16
C6
D12
Attempts:
2 left
💡 Hint
Count how many times j runs for each i.
🔧 Debug
advanced
2:00remaining
Identify the error in nested loops
What error will this code produce when compiled?
C++
#include <iostream>
int main() {
    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
            std::cout << i << j << std::endl;
    std::cout << "Done" << std::endl;
    return 0;
}
ACompilation error: undeclared variable
BCompilation error: missing braces
CRuntime error: segmentation fault
DRuns and prints pairs then 'Done'
Attempts:
2 left
💡 Hint
Check how indentation affects loops in C++.
❓ Predict Output
advanced
2:00remaining
Output of nested loops with continue
What is the output of this code?
C++
#include <iostream>
int main() {
    for (int i = 1; i <= 3; ++i) {
        for (int j = 1; j <= 3; ++j) {
            if (i == j) continue;
            std::cout << i << j << " ";
        }
    }
    return 0;
}
A11 22 33
B12 13 21 23 31 32
C12 13 23 31 32
D21 23 31 32
Attempts:
2 left
💡 Hint
The continue skips printing when i equals j.
🧠 Conceptual
expert
3:00remaining
Number of iterations in nested loops with variable limits
Consider this nested loop code. How many times does the innermost statement execute?
C++
#include <iostream>
int main() {
    int count = 0;
    for (int i = 1; i <= 5; ++i) {
        for (int j = 1; j <= i; ++j) {
            for (int k = 1; k <= j; ++k) {
                ++count;
            }
        }
    }
    std::cout << count;
    return 0;
}
A35
B55
C50
D45
Attempts:
2 left
💡 Hint
Sum the counts: for each i, sum j=1 to i, then sum k=1 to j.

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

  1. Step 1: Understand the code structure

    The code shows one for loop inside another for loop, which means nested loops.
  2. Step 2: Define nested loops

    Nested loops mean one loop runs inside another loop's block of code.
  3. Final Answer:

    A loop inside another loop -> Option B
  4. 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++?
easy
A. for(int i=0; i<3; i++) { for(int j=0; j<2; j++) { /* code */ } }
B. for i in range(3): for j in range(2): /* code */
C. while(i<3) { while(j<2) /* code */ }
D. loop(i=0; i<3; i++) { loop(j=0; j<2; j++) { /* code */ } }

Solution

  1. Step 1: Identify C++ loop syntax

    C++ uses for(initialization; condition; increment) syntax for loops.
  2. Step 2: Check each option

    for i in range(3): for j in range(2): /* code */ uses Python syntax, invalid in C++.
    for(int i=0; i<3; i++) { for(int j=0; j<2; j++) { /* code */ } } uses correct C++ syntax with nested for loops.
    while(i<3) { while(j<2) /* code */ } uses while loops missing initializations and increments.
    loop(i=0; i<3; i++) { loop(j=0; j<2; j++) { /* code */ } } uses invalid loop keyword.
  3. Final Answer:

    for(int i=0; i<3; i++) { for(int j=0; j<2; j++) { /* code */ } } -> Option A
  4. Quick Check:

    C++ nested loops use for(init; cond; inc) [OK]
Hint: Remember C++ loops use for(init; cond; inc) syntax [OK]
Common Mistakes:
  • Using Python or other language syntax in C++
  • Missing semicolons in for loop header
  • Using wrong keywords like 'loop'
3. What is the output of this C++ code?
for(int i = 1; i <= 2; i++) {
for(int j = 1; j <= 3; j++) {
std::cout << i*j << " ";
}
std::cout << std::endl;
}
medium
A. 3 6 9 \n 6 12 18
B. 1 2 3 4 5 6
C. 1 2 3 \n 2 4 6
D. 1 1 1 \n 2 2 2

Solution

  1. Step 1: Trace outer loop values

    Outer loop runs i=1 and i=2.
  2. Step 2: Calculate inner loop outputs

    For i=1, j=1 to 3: outputs 1*1=1, 1*2=2, 1*3=3.
    For i=2, j=1 to 3: outputs 2*1=2, 2*2=4, 2*3=6.
  3. Final Answer:

    1 2 3 2 4 6 -> Option C
  4. Quick Check:

    Multiply i and j in nested loops [OK]
Hint: Multiply outer and inner loop counters for output [OK]
Common Mistakes:
  • Ignoring the newline after inner loop
  • Adding all numbers instead of printing products
  • Confusing loop ranges
4. Find the error in this nested loop code:
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; i++) {
std::cout << i+j << " ";
}
std::cout << std::endl;
}
medium
A. Inner loop increments i instead of j
B. Outer loop condition is wrong
C. Missing semicolon after std::cout
D. No error, code runs fine

Solution

  1. Step 1: Check inner loop increment

    Inner loop header uses i++ instead of j++, causing infinite loop or wrong behavior.
  2. Step 2: Identify correct increment variable

    Inner loop should increment j to progress through its range.
  3. Final Answer:

    Inner loop increments i instead of j -> Option A
  4. Quick Check:

    Inner loop must increment its own variable [OK]
Hint: Check loop increments match loop variables [OK]
Common Mistakes:
  • Incrementing wrong variable in inner loop
  • Assuming code runs without checking increments
  • Confusing outer and inner loop variables
5. You want to print a 4x4 multiplication table using nested loops in C++. Which code snippet correctly achieves this?
hard
A. for(int i=1; i<=4; i++) { for(int j=1; j<=4; j++) { std::cout << i-j << " "; } std::cout << std::endl; }
B. for(int i=0; i<4; i++) { for(int j=0; j<4; j++) { std::cout << i+j << " "; } std::cout << std::endl; }
C. for(int i=1; i<=4; i++) { for(int j=1; j<=4; j++) { std::cout << i+j << " "; } std::cout << std::endl; }
D. for(int i=1; i<=4; i++) { for(int j=1; j<=4; j++) { std::cout << i*j << " "; } std::cout << std::endl; }

Solution

  1. Step 1: Understand multiplication table logic

    Each cell should print the product of row and column numbers (i*j).
  2. Step 2: Check each option's output logic

    for(int i=1; i<=4; i++) { for(int j=1; j<=4; j++) { std::cout << i*j << " "; } std::cout << std::endl; } prints i*j correctly.
    for(int i=0; i<4; i++) { for(int j=0; j<4; j++) { std::cout << i+j << " "; } std::cout << std::endl; } prints sum with zero-based indices.
    for(int i=1; i<=4; i++) { for(int j=1; j<=4; j++) { std::cout << i+j << " "; } std::cout << std::endl; } prints sum with one-based indices.
    for(int i=1; i<=4; i++) { for(int j=1; j<=4; j++) { std::cout << i-j << " "; } std::cout << std::endl; } prints difference.
  3. Final Answer:

    for(int i=1; i<=4; i++) { for(int j=1; j<=4; j++) { std::cout << i*j << " "; } std::cout << std::endl; } -> Option D
  4. Quick Check:

    Multiplication table = print i*j in nested loops [OK]
Hint: Multiply loop counters for multiplication table [OK]
Common Mistakes:
  • Using addition or subtraction instead of multiplication
  • Starting loops at 0 instead of 1 for table
  • Not printing newline after each row
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard