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

Prerequisite : Decision Making in C++

Question 1. What is the output of following program?

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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?

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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?

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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?

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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?

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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?

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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?

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output: else part

Explanation : -1 + 1 = 0 = false

Question 8. What is the output of following program?

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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.

This article is contributed by Palak Jain. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.