If statement

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
}
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.
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.
so /if(z)\ means /if(z==true)\

alright guys, thanks alot.
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.
@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
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.