don't understand

Can you please help me understand why this code returns the one statement from the if and another from the else?

This is true!
That's all folks"


#include <iomanip>
using namespace std;

int main()
{

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;
 
x = 2


This is an assignment - not a test. x is set to 2 with the result tested for true/false - and 2 is always true. So the 'This is true' statement is executed. Then the final 'That's all. folks' statement is executed.

You probably mean something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

int main()
{
    int x = 5;

    if (x == 2)
        cout << "This is true!\n";
    else
        cout << "This is false!\n";

    cout << "That's all, folks!\n";

    return 0;
}


Note the indenting etc.

Some consider that when testing against a constant, the constant should come first:

 
if (2 == x)


so that if == is wrongly written as =, then there will be a compile error as 2 is a constant so can't have it's value set.
Last edited on
@seeplus,

Good point. I never thought of putting the constant first, but I see the truth in it.


Hello renpo,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



Andy

Edit:
Last edited on
I'm kind of surprised your compiler didn't even give you a warning when you tried to compile it. Mine always gives me a warning:
clang::warning: using the result of an assignment as a condition without parentheses [-Wparentheses]
        if (x = 2)
            ~~^~~

Also, note that you forgot a closing bracket after your return 0 on line 19. This will affect your code, because then the program will never actually end (unless, like I do, you use brackets on your if statements, but then it will still do weird stuff).
Also, just a tip, to make it easier to read, indent like @seeplus said, but you can also do this:
1
2
3
4
5
6
7
8
9
10
11
12
13
int main (int argc, char const* argv[])
{
    int x = 2;
    if (2 == x) 
    { // add brackets to separate the contents of the if statement from the rest
        std::cout << "This is true!" << std::endl;
    }
    else
    {
    	std::cout << "This is false!" << std::endl;
    }
    return EXIT_SUCCESS;
}

Edit:
Something surprising, I forgot to #include <ostream>, but the code still compiled and ran ok. Kind of odd.
Last edited on
Topic archived. No new replies allowed.