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

11 Sep, 2017
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 partExplanation : 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.
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.
CPP-Output
Program Output
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
unsigned specifier (%u) in C with Examples
How to show full column content in a PySpark Dataframe ?
error: call of overloaded ‘function(x)’ is ambiguous | Ambiguity in Function overloading in C++
Output of Java Programs | Set 12
Discrete Fourier Transform and its Inverse using MATLAB
Output of Java Programs | Set 14 (Constructors)
Output of Python program | Set 6 (Lists)
Output of Java Programs | Set 34 (Collections)
Output of C programs | Set 31 (Pointers)
Output of Java Program | Set 8