Ask Question
8 July, 17:46

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;

+4
Answers (1)
  1. 8 July, 17:56
    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 and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers