I think I have declared and implemented these booleans wrong

closed account (367kGNh0)
Simply, what are the flaws here? I was never good with bool, so I have no idea. The following code is extracted.


This code is from the header file
1
2
3
  
	bool iseven = false;
	bool issix = false;


And this is in the cpp. I will specify if it is from a method

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
29
30
31
32
33
34
35
36
37
//from, lets say method A
const char *activie[] = {
		"AC",
		"BC",
	};
	int PencilTextl = rand() % 2;
	
	if(activie[PencilTextl] == "AC") {
		iseven = true;
		sevenlettersSketch();
	}

	else if (activie[PencilTextl] == "BC") {
		issix = true;
		sixlettersSketch();
	}

//from method B
if (issix = true)
	{
		issix = false;
	}

	if (iseven = true)
	{
		iseven = false;
	}

	
	if(issix = true){
	clipper->removeAllChildren();
	removeChild(stencil);}

	if (iseven = true) {
		clipper7->removeAllChildren();
		removeChild(stencil7);
	}


for some reason the code does not work
Last edited on
closed account (367kGNh0)
I found the solution. Hope it helps, i need two equal signs like this

if(iseven == false)
You don't really need to use == here because the variable is already a bool.

 
if (iseven)
closed account (367kGNh0)
You don't really need to use == here because the variable is already a bool.


I saw this while researching. so how on earth do you specify if true or false?
If you want to check if the variable is false you just use the logical not operator (!).

 
if (!iseven)
Please note that if you state that “iseven” and “issix” must be turned to false when they are true
1
2
if ( issix  ) { issix = false; }
if ( iseven ) { iseven = false; }

the following conditions will always evaluate to false:
1
2
3
4
5
6
7
8
if ( issix ) {
    clipper->removeAllChildren();
    removeChild(stencil);
}
if ( iseven ) {
    clipper7->removeAllChildren();
    removeChild(stencil7);
}

closed account (367kGNh0)
Please note that if you state that “iseven” and “issix” must be turned to false when they are true

if ( issix ) { issix = false; }
if ( iseven ) { iseven = false; }


the following conditions will always evaluate to false:

if ( issix ) {
clipper->removeAllChildren();
removeChild(stencil);
}
if ( iseven ) {
clipper7->removeAllChildren();
removeChild(stencil7);
}



I know, for that reason I re-arranged it
1
2
3
4
if (issix = true)
	{
		issix = false;
	}


this is just:
issix = issix^issix ; //true xor true = false. false xor false is false. excessive if-statements slow down code execution, and xor is built into the cpu chip its very quick. assigning false over already being false costs less than compare/jump/etc of an if to avoid it. and its more concise. can use issix ^= issix; of course.



Last edited on
@Jonnin

I found your post above very interesting!

could you give an example of how to use this? let me elaborate..

lets say you have the value issix and that equals true

1
2
3
4
5
6
7

if(isSix)
 isSix = false; // we need an if statement here to test if isSix == true if so set it to false

if(isSix)
 isSix = isSix ^ isSix // true xor true == false BUT we still need an if statement to test if isSix is true


so my question is how do you avoid using the if statement? lets say isSix could equal true or false depending if the user inputs 6 or not, wouldn't we still need an if statement to check for this? if not how would we get rid of the if statement checking if isSix == true then setting it to false once the check is done

thanks! =)
it fully replaces the code.

your code says:
if issix is true, set it to false.

which, now that I think about it, is identical to
issix = false; //if it was true, it is false now. if it was false, it is false now.

my code was overthinking it.
my code says
if it is true, make it false, if it was false, make it false. //same thing
and it just works differently.
the truth table for xor is
in in out
T T F
T F T //not possible when xor something with itself
F T T // not possible for with itself
F F F

The point is you do not need to check it. you want it to be false if it was true.
the only other option was, it was already false, and you want that unchanged. Unchanged may mean you assign it = to false, same result. Not all conditional statements are avoidable, but when dealing with Booleans, you get a lot of freebies like this one where you can.


Last edited on
ah ok I think I misread the original post, that makes sense,

what I meant was trying to avoid an if statement altogether, but I don't think that would be possible

1
2
3
4
5
6
7
8
9

if(isSix){
 
   isSix = isSix ^ isSix

   // do certain actions such as call specific functions if isSix == true
}



but yes now I understand what you meant, the if statement if below

1
2
3
4

if(isSix)
 isSix = false


could be simplified and avoid using the if statement by just

isSix = isSix ^ isSix
Last edited on
Just write isSix = false; without the if.
Last edited on
closed account (367kGNh0)
Just write isSix = false; without the if.
You don't even really know the context :)
I don't, and that is why I didn't mention it earlier. My last post was mainly a response to what adam2016 said about avoiding the if statement by using xor. If there is nothing else inside the if statement there is no reason checking the variable. Just set it to whatever value you want it to have.
Last edited on
Topic archived. No new replies allowed.