Ask Question
12 July, 09:09

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;

+5
Answers (1)
  1. 12 July, 11:54
    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?
Get 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 there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers