initialisation of bool - can't get my head around it!

Pages: 12
Hi peeps,

I’m having a hard time getting my head around the initialisation of bool.

Consider the program i wrote below. As it stands, it outputs the correct answer. However, if i change the first "false" to "true", it outputs the opposite answer! Why does it matter how the bool is initialized? Why doesn’t the compiler believe the statement "if (45 == x) what = true;"?

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


int main()
{
	bool what(false);
	int x(2);
	if (45 == x) what = true;
		
	if (what) cout << "The same!" << endl;
	else cout << "Not the same" << endl;
	
}
There is code that might set the boolean to true, but there is no code that could ever set the boolean to false. Thus, the only way the boolean could ever be false is if it started that way and was never set to true.
Last edited on
Thanks for the reply but i simply don't understand it at all! (beginner here!)

Are you speaking generally?....... or specifically about the code i presented?

And chance you could try to explain it in some other way?
I explained it specifically for the code you presented.

Let's say line 8 sets the boolean to true. Tell me where you think the boolean would later be set to false before line 12.
Why does it matter how the bool is initialized?
Because line 10 does not change the value of what since x is not 45.
Just to add on what LB said, an easy way to solve this that will ignore how the bool was initialised would be this:
1
2
3
4
5
6
7
8
9
10
11
12
13
bool what; //Initialise however you want
int x (2)
what = (x == 45); 
//I put x == 45 not 45 == x because it is more natural to say if this number is equal to 45, 
//rather than to say is 45 equal to the number.
//It implies that the term on the left is the one with an unknown value and
//the one on the right is the one that the unknown is being compared to.

if(what) {
    std::cout << "The same!" << std::endl;
} else {
    std::cout << "Not the same" << std::endl;
}

edit: damn all the sniping
Last edited on
@shadowmouse
//I put x == 45 not 45 == x because it is more natural to say if this number is equal to 45,
Get used to 45 == x because, well: what = (x = 45); // Do you spot the problem?
As coder777 says, placing the constant first is a well-known programming habit designed to guard against accidentally using assignment.
Ahhh, ok, i feel like i see a rough outline of understanding in the fog of my mind!

So at line 12 the statement "if(what)" could be better thought of (by me) as "if (what [is true])"

Then all i need to do is check what was the most recent assignment of "what", and that determines what happens at line 12?

So "if(what)" always equates to: "if (what [is true])", is that right? Never: "if (what [is false])"?

Regarding the other matter, are you saying that "what = (x = 45);" could be mistaken as a new assignment of int x?
Last edited on
if(var) is the same as if(var != false)

if(!var) is the same as if(var == false)

Writing if(x = y) is the same as writing x = y; if(x)
ouch, there you again bro! ;)

So "!=" means "is not the same as"?

And unfortunately i don't see any difference between:
if(x = y)
&
if(x == y)

:(



A single equals sign is assignment and changes the value of the object on the left to be the same as the value of the object on the right. The result of the expression is the object on the left.

A double equals sign is equality comparison - it compares the two values and returns true if they are the same and false otherwise.
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

using namespace std;

int main()
{
	bool what = false;
	int x = 2;
	
	
	//Test 1
	if (2 == x) what = true;
		
	if (what) cout << "The same!" << endl;
	else cout << "Not the same" << endl;
	
	// Test 2
	what = true;
	if (2 == x) what = false;	
	if (!what) cout << "The same!" << endl;
	else cout << "Not the same" << endl;
}
closed account (48T7M4Gy)
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 = 2;
	
	//Test 1
	if (x == 2) cout << "The same!" << endl;
	else cout << "Not the same" << endl;
	
	// Test 2
	if ( !(2 == x) ) cout << "Not the same!" << endl;
	else cout << "The same!" << endl;
}
Regarding the other matter, are you saying that "what = (x = 45);" could be mistaken as a new assignment of int x?
It assigns 45 to x and makes what becomes true (actually the value 45 as well!)

Implicitely if does that: if(expression != 0) hence if you write if (what) it is considered if (what != 0)

true is a value != 0
false is value == 0

This works also for non bool:
1
2
	if (x) cout << "x is not 0" << endl;
	else cout << "x is 0" << endl;
closed account (48T7M4Gy)
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 = 2;
	
	//Test 1
	if (x == 2) cout << "The same!" << endl;
	else cout << "Not the same" << endl;
	
	// Test 2
	if ( x = 2  ) cout << "Not the same!" << endl; //***
	else cout << "The same!" << endl;
}
@kemort: why are you spamming this thread with unexplained code?
Thanks guys, it's becoming clearer! The last post by coder777 i don't understand, will go over that again this evening.

Just one more question, I'm messing around a bit more here, could you tell me why this little masterpiece isn't outputting the answer i expect?:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int getNumb(int x)
{
	cout << "Enter a prime number below 8" << endl;
	cin >> x;
	return x;
}

int main()
{
	int b(0);
	getNumb(b);
	bool prime(false);
	if (2 == b) prime = true;
	else if (3 == b) prime = true;
	else if (5 == b) prime = true;
	else if (7 == b) prime = true;

	if (prime) cout << "Yeah baby!" << endl;
	else cout << "Nope, not a prime!" << endl;

}
Last edited on
closed account (48T7M4Gy)
.
Last edited on
On line 11, you discard the return value of getNumb and so the value of b never changes from 0. Though, why does getNumb take a parameter at all?
1
2
3
4
5
6
7
8
9
10
11
int getNumb()
{
	int x;
	cout << "Enter a prime number below 8" << endl;
	cin >> x;
	return x;
}

int main()
{
	int b = getNumb();
Last edited on
Pages: 12