Sunday, February 15, 2026

Tricky C++ Loops: Predict Output Questions

 C++ Loops - Tricky Predict Output Questions

1. Post-Increment in Condition
#include <iostream>
using namespace std;

int main() {
    int i = 0;
    while(i++ < 3)
        cout << i << " ";
    return 0;
}

Output:
1 2 3

Explanation:
  • i++ uses old value for comparison, then increments.
  • Values checked: 0,1,2 ; hence prints incremented values 1,2,3.

2. Pre-Increment in Condition
int main() {
    int i = 0;
    while(++i < 3)
        cout << i << " ";
}

Output:
1 2

Explanation:
  • ++i increments first.
  • Loop runs for i = 1, 2 only.

3. Missing Braces Trap
int main() {
    int i = 1;
    while(i <= 3)
        cout << i++ << " ";
        cout << "Done";
}

Output:
1 2 3 Done

Explanation:
  • Only first statement belongs to while.
  • cout << "Done" executes once after loop.

4. Decrement in Condition
int main() {
    int i = 3;
    while(i--)
        cout << i << " ";
}

Output:
2 1 0

Explanation:
  • Condition checks old value.
  • After decrement, printed values: 2,1,0.

5. Continue Before Print
int main() {
    for(int i = 1; i <= 5; i++) {
        if(i % 2 == 0)
            continue;
        cout << i << " ";
    }
}

Output:
1 3 5

Explanation: Even numbers skipped using continue.

6. Break Before Increment
int main() {
    int i = 1;
    while(i <= 5) {
        if(i == 4)
            break;
        cout << i << " ";
        i++;
    }
}

Output:
1 2 3

Explanation: Loop stops when i == 4.

7. Infinite Loop Trap
int main() {
    for(int i = 1; i < 3; )
        cout << i << " ";
}

Output:
1 1 1 1 ... (Infinite)

Explanation: i never increments.

8. Printing in For Update Section
int main() {
    for(int i = 0; i < 3; cout << i++ << " ");
}

Output:
0 1 2

Explanation: Printing happens in update section of for loop.

9. Double Increment Trap
int main() {
    int i = 1;
    do {
        cout << i++ << " ";
    } while(i++ < 4);
}

Output:
1 3

Explanation: i increments twice each iteration.

10. Continue in Do-While
int main() {
    int i = 0;
    do {
        i++;
        if(i == 2)
            continue;
        cout << i << " ";
    } while(i < 3);
}

Output:
1 3

Explanation: When i=2, continue skips printing.

11. Nested Loop with Break
int main() {
    for(int i = 1; i <= 3; i++) {
        for(int j = 1; j <= 3; j++) {
            if(j == 2)
                break;
            cout << i << j << " ";
        }
    }
}

Output:
11 21 31

Explanation: Inner loop stops when j==2.

12. Logical Trap with Semicolon
int main() {
    int i = 0;
    while(i < 3);
    {
        cout << i;
    }
}

Output:

Infinite Loop

Explanation: Semicolon ends while loop → empty infinite loop.

13. Scope of Variable
int main() {
    int i;
    for(i = 0; i < 3; i++)
        cout << i;
    cout << i;
}

Output:
0123

Explanation: After loop, i becomes 3.

14. Decrement by 2
int main() {
    for(int i = 5; i > 0; i -= 2)
        cout << i << " ";
}

Output:
5 3 1

Explanation: i decreases by 2 each iteration.

15. Nested Continue Trap
int main() {
    for(int i = 1; i <= 2; i++) {
        for(int j = 1; j <= 3; j++) {
            if(j == 2)
                continue;
            cout << i << j << " ";
        }
    }
}

Output:
11 13 21 23

Explanation: j=2 skipped for each i.

16. Logical Trap
#include <iostream>
using namespace std;

int main() {
    int i = 1;
    while(i < 4)
        cout << i++;
        cout << "Done";
    return 0;
}

Output:
123Done

Explanation: Only first cout is inside loop (no braces!).

17. For loop body
#include <iostream>
using namespace std;

int main() {
    for(int i = 0; i < 3; cout << i++ << " ");
    return 0;
}

Output:
0 1 2

Explanation: Printing happens in update section!

18. No Braces Trap
#include <iostream>
using namespace std;

int main() {
    for(int i = 1; i <= 3; i++)
        cout << i << " ";
        cout << "End";
    return 0;
}

Output:
1 2 3 End

Explanation: Only first statement is inside loop.

19. Infinite Loop Trap
#include <iostream>
using namespace std;

int main() {
    for(int i = 1; i < 5; ) {
        cout << i << " ";
    }
    return 0;
}

Output:
1 1 1 1 1 ... (Infinite Loop)

Explanation: i never changes!

Back to C++ Home Page >

* * * * *

No comments:

Post a Comment