Open In App

Output of C++ programs | Set 46 (If-else statements)

Last Updated : 11 Sep, 2017
Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite : Decision Making in C++

Question 1. What is the output of following program?




#include <iostream>
#include <stdio.h>
int main()
{
    if (!(std::cout << "hello"))
        std::cout << "world";
    else
        std::cout << " else part";
  
    return 0;
}


Output: hello else part

Explanation : Since if-else works on the principle that if the condition provided in the if statement is true, if block is executed otherwise, else block will be executed. Since, std::cout<<"hello" returns a reference to std::cout, therefore, if condition becomes true and if block is executed.

Question 2. What is the output of following program?




#include <iostream>
using namespace std;
int main()
{
    int a = 014;
    std::cout << a << std::endl;
    ;
    return 0;
}


Output: 12

Explanation : Reason for the output: A 0 in the beginning of an integer makes it octal. Therefore, 12 is getting printed instead of 14.

Question 3. What is the output of following program?




#include <stdio.h>
#include <iostream>
int main()
{
    if (int q = 0)
        std::cout << "if part";
    else
        std::cout << "else part";
    return 0;
}


Output: else part

Explanation : Since if-else works on the principle that if the condition provided in the if statement is true, if block is executed otherwise, else block will be executed.
Since, int q = 0 results in initializing the variable q as 0, therefore, the condition becomes false, and hence else block is executed.

Question 4. What is the output of following program?




#include <iostream>
using namespace std;
int main()
{
    int a = 0xC;
    std::cout << a << std::endl;
    ;
    return 0;
}


Output: 12

Explanation : A 0x or 0X in the beginning of an integer makes it hexadecimal. Therefore, 12 which is hexadecimal equivalent of C.

Question 5. What is the output of following program?




#include <stdio.h>
#include <iostream>
  
int main()
{
    if (float q = 10.0)
        std::cout << "if part";
    else
        std::cout << "else part";
    return 0;
}


Output: if part
Explanation : Since if-else works on the principle that if the condition provided in the if statement is true, if block is executed otherwise, else block will be executed.
Since, float q = 10.0 results in initializing the variable q as 10.0, therefore, the condition becomes true, and hence if block is executed.

Question 6. What is the output of following program?




#include <stdio.h>
#include <iostream>
  
int main()
{
    int a, b;
    if ((a = 5) || (b = 0))
        std::cout << "if part";
    else
        std::cout << "else part";
    return 0;
}


Output: if part

Explanation : Since if-else works on the principle that if the condition provided in the if statement is true, if block is executed otherwise, else block will be executed.
Since, ((a = 5) || (b = 0)) evaluates to be true (because 5 OR 0 is 5 i.e., true), therefore, the if block is executed.

Question 7. What is the output of following program?




#include <iostream>
#include <stdio.h>
int main()
{
    int n = -1;
    if (n + 1)
        std::cout << "if part";
    else
        std::cout << " else part";
  
    return 0;
}


Output: else part

Explanation : -1 + 1 = 0 = false

Question 8. What is the output of following program?




#include <iostream>
using namespace std;
int main()
{
    int i;
  
    for (std::cout << "hello" << std::endl; i < 5; i++) {
        std::cout << "hi" << std::endl;
    }
    return 0;
}


Output: hello
hi
hi
hi
hi
hi

Explanation : Since if-else works on the principle that if the condition provided in the if statement is true, if block is executed otherwise, else block will be executed.
Since, (n+1) evaluates to be false (because 0 is false), therefore, the else block is executed.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads