Ask Question

What is the output of the following code segment? int x = 5; if (x = 2) cout << "This is true!" << endl; else cout << "This is false!" << endl; cout << "That's all, folks!" << endl;

+2
Answers (1)
  1. 8 August, 08:46
    0
    The output of the given program is:

    This is true!

    That's all, folks!

    Explanation:

    In this code the variable x is initialize to 5 after that the control moves to the if block if (x = 2) this statement set the value of variable x to 2 also it not check the condition i. e x = =2 because it is assignment operator inside the condition of if block not the equal to operator. so it executed the statement inside the if block which print "This is true!" after executing the if block the control moves and executed the last statement i. e "That's all, folks!".

    Following are the complete code in c++

    #include / / header file

    using namespace std;

    int main () / / main function

    {

    int x = 5;

    if (x = 2)

    cout << "This is true!" << endl;

    else

    cout << "This is false!" << endl;

    cout << "That's all, folks!" << endl;

    return 0;

    }

    Output:

    This is true!

    That's all, folks!
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “What is the output of the following code segment? int x = 5; if (x = 2) cout ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers