IGDTUW 2026 Computer PYQ — What will be the output of the following C++ code if a is assigne… | Mathem Solvex | Mathem Solvex
Tip:A–D to answerE for explanationV for videoS to reveal answer
IGDTUW 2026 — Computer PYQ
IGDTUW | Computer | 2026
What will be the output of the following C++ code if a is assigned a positive non-zero value?
#include <iostream>
using namespace std;
int main() {
int a;
if (a = 10) {
cout << "Good";
} else {
cout << "Bad";
}
return 0;
}
Choose the correct answer:
A.
Good
B.
Bad
(Correct Answer)
C.
Compilation Error
D.
Depends on the compiler
Correct Answer:
Bad
Explanation
Understanding the Expression in if:
Inside the condition of the if statement, we have the expression:
if(a=10)
Notice that a single equal sign (=) is used here instead of a double equal sign (==). In C++, = is the assignment operator, not the relational equality operator.
Evaluation of Assignment:
The expression a=10 assigns the integer value 10 to the variable a. The value of an assignment expression as a whole is the value that gets assigned. Therefore, the expression evaluates directly to:
10
Boolean Conversion:
In C++, any non-zero integer value is implicitly converted to a boolean value of true, while 0 is considered false.
Since 10=0, the condition evaluates to true:
Condition: 10→true
Execution of Code Blocks:
Because the condition is true, the code inside the if block executes, and the else block is completely skipped.
\text{cout << "Good";}
Thus, the program outputs Good.
Explanation
Understanding the Expression in if:
Inside the condition of the if statement, we have the expression:
if(a=10)
Notice that a single equal sign (=) is used here instead of a double equal sign (==). In C++, = is the assignment operator, not the relational equality operator.
Evaluation of Assignment:
The expression a=10 assigns the integer value 10 to the variable a. The value of an assignment expression as a whole is the value that gets assigned. Therefore, the expression evaluates directly to:
10
Boolean Conversion:
In C++, any non-zero integer value is implicitly converted to a boolean value of true, while 0 is considered false.
Since 10=0, the condition evaluates to true:
Condition: 10→true
Execution of Code Blocks:
Because the condition is true, the code inside the if block executes, and the else block is completely skipped.