Archived

This topic is now archived and is closed to further replies.

ifc69

"if" statements

Recommended Posts

I have been going through the book "Game programming all in one" and I''ve come to a bit of code that basically says... (Using Visual C++ 6.0) if (m_LastAction) { code } m_LastAction is just an int, and earlier there was a code block that has m_LastAction equal to a function (If that helps any.) So I guess I''m confused because I thought "if" statements were supposed to evaluate an expression, giving a result of true or false. Can anyone tell me what the "if" statement is doing if only a variable is being evaluated? Thanks, Ian

Share this post


Link to post
Share on other sites
It''s the same thing as saying if(m_LastAction == true). If m_LastAction is not 0 then it will be evaluated as true, if it is 0 then it will be evaluated as false.

Share this post


Link to post
Share on other sites
A variable is a valid expression in C and C++; it evaluates to itself (naturally). An important point is that, in these languages, logical 0 (false) is equivalent to numerical 0, and logical 1 (true) is equivalent to any non-zero value. Thus,
if(somevar) 

is perfectly equivalent to
if(somevar != 0) 

Share this post


Link to post
Share on other sites
Earlier I wrote that the variable was initialized to a function, so does the function get called or looked at?

The function checks to see whether there was a "key-press" and if so it returns what key was pressed.

Share this post


Link to post
Share on other sites
No, it is probably initialized to the value returned by the function. You can''t assign a function to a integer (without converting to a pointer, but that has nothing to do with this). And to answer your original question:
You are right, if statements evaluate Boolean expressions (true and false, 1 and 0). An int can be converted to true or false for an if statement. If it is 0, the expression is false, if the expression is anything but 0, it evaluates to true.

Share this post


Link to post
Share on other sites
If statements evaluate the expression they''re given. If the evaluated result is true, it executes the code afterwards. Otherwise it''ll either jump to the else or go straight to the end of the if block if there''s no else.

The reason why, say, if(valueOne == valueTwo) works is because "==" is an operator that returns true if both values are equal or false otherwise. You could do this, in fact...

bCharDead = Char.Health == 0 || Char.Status == ST_PETRIFIED;

When evaluating a single value, you already have either a true or false result to begin with so code like if(bAlive == 1) is redundant and unecessary.

Share this post


Link to post
Share on other sites
I suggest using the debugger to figure these things out.

If you haven''t yet learned how to set a breakpoint (F9 in MSVC editor), and step through/into functions (F10/F11 in MSVC debugger) then you need to learn this right now.

Running your program line by line and watching how the variables change values is very, very useful and will teach you a lot. It''s also one of the better debugging methods there is.

Share this post


Link to post
Share on other sites