C++ Conditional Statements | Find output programs | Set 1

This section contains the C++ find output programs with their explanations on conditional statements (set 1).
Submitted by Nidhi, on June 02, 2020

Program 1:

#include <iostream>
using namespace std;

int main()
{
    if (NULL) {
        cout << "Hello World";
    }
    else {
        cout << "Hii World";
    }

    return 0;
}

Output:

Hii World

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.

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;
}

Output:

PQR

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".

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;
}

Output:

222

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.





Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.