Exam Question. about how booleans are handled in cout

I am studying upcoming exams and came across this exam question, which for me does not make sense.

Consider the following main function:

int main()
{
    int x = 0;
    cout << "x = " << x << ", (0 < x < 10) = " << (0 < x < 10) << endl;
    int x = 5;
    cout << "x = " << x << ", (0 < x < 10) = " << (0 < x < 10) << endl;
    int x = 10;
    cout << "x = " << x << ", (0 < x < 10) = " << (0 < x < 10) << endl;

    return 0;
}

When the program is executed, the following is printed:

x = 0, (0 < x < 10) = 1
x = 5, (0 < x < 10) = 1
x = 10, (0 < x < 10) = 1

Explain what exactly happened.


This is a question. As far as I know, the last line of output should be "x = 10, (0 <x <10) = 0". What am I missing?

+4
source share
2 answers

What do you expect from 0 < x < 10?

It does not check if xbetween 0and 10if that is what you were thinking about.

< is a binary operator that follows the rules for evaluating the operator (priority and associativity).

, 0 < x < 10 (0 < x) < 10. , ( ).

+10

0 < x < 10 , . ( 0 < x ) < 10, , . 0 < x ( true false) < 10, true ( 1) false ( 0).

( 0 < x ) && ( x < 10 )

, x .

, cout x=0 , ( 0 < 0 ), false, 0 < 10 (false 0), , , 1.

, cout x=5 false, 0 < 10, true.

, , cout at x=10 true, true, 1.

+3

Source: https://habr.com/ru/post/1584917/


All Articles