This section contains the C++ find output programs with their explanations on conditional statements (set 1).
Program 1:
#include <iostream>
using namespace std;
int main()
{
if (NULL) {
cout << "Hello World";
}
else {
cout << "Hii World";
}
return 0;
}
Program 2:
#include <iostream>
using namespace std;
int main()
{
int A = 5, B = 10, C = 20;
if (A && B > C) {
cout << "ABC";
}
else if (A = B || C == 20) {
cout << "PQR";
}
else {
cout << "XYZ";
}
return 0;
}
Program 3:
#include <iostream>
using namespace std;
int main()
{
int A = 15, B = 10, C = 20;
if (A > B) {
if (A >= 15) {
if (C > 10 ? A > 5 ? NULL : 1 : 2) {
cout << "111";
}
else {
cout << "222";
}
}
else {
cout << "333";
}
}
else {
cout << "444";
}
return 0;
}
Answer Program 1:
Output:
Explanation:
The program will print "Hii World" on the console screen. Because the value of NULL is 0, then the if statement will be false and the else part will be executed.
Answer Program 2:
Output:
Explanation:
Execute the program step by step:
int A=5, B=10, C=20; if(A&&B>C) if(5&&10>20) According to operator priority table, greater than (>) operator will evaluate before the logical and (&&) then if(5&&0) if(0) Then, program's execution will jump to, else if(A=B || C==20) According to the operator priority table, equal to (==) operator will be evaluated before the assignment and logical or operator else if(10 || 20) else if(1) Here, condition is true and its block will be executed, Then the final output will be "PQR".
Answer Program 3:
Output:
Explanation:
Execute the program step by step:
int A=15, B=10, C=20; if( A>B) that is if(15>10) condition is true then if(A>=15) that is if(15>=15) condition is true then Now checking, if(C>10 ? A>5 ? NULL : 1 : 2) Here, C>10 that is 20>10 condition is true then check A>5 that is 15>5 condition is again true then it returns NULL, and the value of NULL is 0 and then if statement will be false. Then the else part will be executed and will print "222" on the console screen.
need an explanation for this answer? contact us directly to get an explanation for this answer