Please explain why these are the same

Can someone explain why these work the same?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>

using namespace std;

int main()
{
int userInput;
int greater_than_five = true;

cout << "Enter a number: ";
cin >> userInput;

if(userInput < five)
{
greater_than_five = false;
}

//print result, confused on this part 
if(greater_than_five)
{
cout << "Number is greater than five." << endl;
}
else
{
cout << "Number is less than five." << endl;
}
return 0;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>

using namespace std;

int main()
{
int userInput;
int greater_than_five = true;

cout << "Enter a number: ";
cin >> userInput;

if(userInput < five)
{
greater_than_five = false;
}

//print result, confused on this part 
if(greater_than_five == true)
{
cout << "Number is greater than five." << endl;
}
else
{
cout << "Number is less than five." << endl;
}
return 0;
}
closed account (EwCjE3v7)
Look at line 15, there is no such thing as five, you should have written the number 5: if(userInput < 5)

And why they both work the same is because when you pass an int to a if statement, it converts to a bool. A bool can be true or false. The number 0 is true and any other is false. So the == true is optional.

Btw if you want a true or false type, you are better of going with a bool. (Line 8 bellow)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>

using namespace std;

int main()
{
int userInput;
bool greater_than_five = true; // changed to bool

cout << "Enter a number: ";
cin >> userInput;

if(userInput < 5) // error fixed
{
greater_than_five = false;
}

//print result, confused on this part 
if(greater_than_five)
{
cout << "Number is greater than five." << endl;
}
else
{
cout << "Number is less than five." << endl;
}
return 0;
}


And to shorten the code, I presume you already know this but you can do this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

using namespace std;

int main()
{
int userInput;

cout << "Enter a number: ";
cin >> userInput;

if(userInput < 5)
{
cout << "Number is greater than five." << endl;
}
else
{
cout << "Number is less than five." << endl;
}
return 0;
}
Last edited on
Thanks! I understand now how that concept works... and wow that 'five' error was stupid! I just wrote up a simple program to explain my confusion. And yes i know i could have done that. Thanks again.
The number 0 is true and any other is false
0 is false, and anything else is true. Generally 1 is used for true though. IIRC functions only check if they are false (equal to 0) and don't even check if they are true, if they are not false they must be true. So (-∞, 0)∪(0,∞) are true.

Last edited on
closed account (EwCjE3v7)
@giblit Oh my mistake there. Sorry for that, just got confused with the return statement. Thank you giblit for pointing that out
Topic archived. No new replies allowed.