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 >

* * * * *

C++ Loops: Predict Output Questions (For Beginners)

C++ Looping Statements

Predict Output Questions For Beginners

(while, do-while and for loop)

Part A: While Loop Predict Output Questions


Q1
#include <iostream>
using namespace std;

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

Output:
1 2 3 4 5

Q2
#include <iostream>
using namespace std;

int main() {
    int i = 5;
    while(i > 0) {
        cout << i << " ";
        i -= 2;
    }
    return 0;
}

Output:
5 3 1

Q3
#include <iostream>
using namespace std;

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

Output:
10

Q4
#include <iostream>
using namespace std;

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

Output:
0123

Q5
#include <iostream>
using namespace std;

int main() {
    int i = 1;
    while(i < 10) {
        cout << i << " ";
        i *= 2;
    }
    return 0;
}

Output:
1 2 4 8

Part B: Do-While Loop Predict Output


Q6
#include <iostream>
using namespace std;

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

Output:
1 2 3

Q7
#include <iostream>
using namespace std;

int main() {
    int i = 5;
    do {
        cout << i << " ";
        i--;
    } while(i > 2);
    return 0;
}

Output:
5 4 3

Q8
#include <iostream>
using namespace std;

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

Output:
0

Q9
#include <iostream>
using namespace std;

int main() {
    int i = 1, product = 1;
    do {
        product *= i;
        i++;
    } while(i <= 4);
    cout << product;
    return 0;
}

Output:
24

Q10
#include <iostream>
using namespace std;

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

Output:
2 5 8

Part C: For Loop Predict Output Questions


Q11
#include <iostream>
using namespace std;

int main() {
    for(int i = 1; i <= 4; i++)
        cout << i * 2 << " ";
    return 0;
}

Output:
2 4 6 8

Q12
#include <iostream>
using namespace std;

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

Output:
6

Q13
#include <iostream>
using namespace std;

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

Output:
5 3 1

Q14
#include <iostream>
using namespace std;

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

Output:
0123

Q15
#include <iostream>
using namespace std;

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

Output:
1 3


Back to C++ Home Page >

* * * * *

Saturday, February 14, 2026

C++ Tricky Decision Making: Predict Output Questions (Logical Errors)

C++ Decision-making Statements

Tricky Predict Output Questions (Logical Errors)

(ifif-elsenested ifelse if ladderswitch, etc.)

Answers are given at the bottom of this page.

Q1 - Assignment Instead of Comparison
#include <iostream>
using namespace std;

int main() {
    int x = 5;
    if (x = 0)
        cout << "True";
    else
        cout << "False";
    return 0;
}

Q2 - Missing Braces
#include <iostream>
using namespace std;

int main() {
    int x = 10;
    if (x > 5)
        cout << "A";
        cout << "B";
    return 0;
}

Q3 - Dangling Else Problem
#include <iostream>
using namespace std;

int main() {
    int x = 5;
    if (x > 2)
        if (x < 4)
            cout << "A";
        else
            cout << "B";
    return 0;
}

Q4 - Switch Without Break
#include <iostream>
using namespace std;

int main() {
    int x = 2;
    switch(x) {
        case 1: cout << "One";
        case 2: cout << "Two";
        case 3: cout << "Three";
        default: cout << "Default";
    }
    return 0;
}

Q5 - Logical AND Mistake
#include <iostream>
using namespace std;

int main() {
    int x = 5;
    if (x > 2 && x < 5)
        cout << "Yes";
    else
        cout << "No";
    return 0;
}

Q6 - Semicolon After If
#include <iostream>
using namespace std;

int main() {
    int x = 10;
    if (x > 5);
        cout << "Hello";
    return 0;
}

Q7 - Character Comparison
#include <iostream>
using namespace std;

int main() {
    char ch = 'A';
    if (ch == 65)
        cout << "Match";
    else
        cout << "No Match";
    return 0;
}

Q8 - Multiple If Instead of Else If
#include <iostream>
using namespace std;

int main() {
    int marks = 85;

    if (marks > 50)
        cout << "Pass ";
    if (marks > 75)
        cout << "Distinction";

    return 0;
}

Q9 - Not Equal Confusion
#include <iostream>
using namespace std;

int main() {
    int x = 5;
    if (!x == 5)
        cout << "True";
    else
        cout << "False";
    return 0;
}

Q10 - Boolean Value Check
#include <iostream>
using namespace std;

int main() {
    bool flag = 2;
    if (flag)
        cout << "ON";
    else
        cout << "OFF";
    return 0;
}

* * * * *

Answer Key (Tricky Outputs)

Que.| Output    | Explanation
Q1   False      x = 0 assigns 0 → condition false
Q2   AB         Only first cout inside if; second always executes
Q3   B          else belongs to inner if
Q4   TwoThreeDefault        No break, so fall-through occurs
Q5   No         x < 5 is false (5 is not less than 5)
Q6   Hello      Semicolon ends if; cout always executes
Q7   Match      ASCII value of 'A' is 65
Q8   Pass Distinction   Both if conditions are true
Q9   False      !x → !5 → false (0); 0 == 5 false
Q10  ON         Any non-zero value converts to true

C++ Decision Making Statements: Predict Output Questions (For Beginners)

C++ Decision-making Statements

Predict Output Questions For Beginners 

(if, if-else, nested if, else if ladder, switch, etc.)