If statement

Aug 22, 2016 at 7:00pm
What does this mean, it defines a new boolean and gives it false value, but what does if(z) mean, when is it gonna be true and enter the if statement?

Thank you.

1
2
3
4
5
bool z =false 
If (z)
{ 
 //code here
}
Aug 22, 2016 at 7:11pm
The code inside the if statement will run when the condition is true. In this case the condition is always false so it will never run.
Aug 22, 2016 at 7:11pm
If there is no code between lines 1 and 2 that sets z to true, the if statement will never be true and line 4 will never be executed.
Aug 23, 2016 at 2:44pm
so /if(z)\ means /if(z==true)\

alright guys, thanks alot.
Aug 23, 2016 at 9:17pm
Saying if (z) is equivalent to testing if whatever z is evaluates to true.

If z of type bool then if (z == true) is equivalent to if (z).

So, in this case, since z is of type bool, OP's assumption is correct.
Aug 23, 2016 at 11:01pm
@gentleguy the rule you mention holds for pretend Booleans like the WinSDK BOOL type, which is typedef of int, and hence can store more than just TRUE (1) and FALSE (0). It does not hold for the bool type.

Andy
Aug 24, 2016 at 2:28am
closed account (48T7M4Gy)
Seems this is a question of style.

Both are correct. The advantage gained by explicitly referring to if(z == true) rather than if(z) is there is little or no opportunity for ambiguity or errors to creep in, especially in maintaining the software later on.

Maintainable, self-documenting and above all readable code is far more important than writing 'shorthand' stuff.

And giving 'z' a more meaningful name isn't a bad move.
Topic archived. No new replies allowed.