• ### Announcements

#### Archived

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

# If..Else?

## Recommended Posts

Why does this if..else statement always execute the first block of code only every single time?

if(playerRace = HUMANS)
{
human1.humanSetData();
human1.main_human_story();
}
else
{
alien1.alienSetData();
alien1.main_alien_story();
}

(HUMANS is #define''d for the value of 1 and there''s an ALIENS #define for 2)
What does it take to be a good game programmer? --- Good insight, good knowledge, and of course, big money deposits!

##### Share on other sites
it needs to be:

==

NOT

=

a single = is an assignment operator the expression x = y always evaluates to false

== is the are these things equal check

-me

##### Share on other sites
It should be:

if(playerRace == HUMANS)

Note the double equals sign. If you only use one (as in variable = 1) it sets the variable to equal 1. If you use two (variable == 1) then it compares the two (which is what you want).

-----------------
"Who the hell wants to hear actors talk?" - H. M. Warner

"Everything that can be invented has been invented." - Charles H. Duell

"I think there is a world market for maybe five computers." - Thomas Watson

[edited by - StarFire on May 3, 2002 7:11:36 PM]

##### Share on other sites
If HUMANS != 0, and you set playerRace = HUMANS, then it will always be true! You want to use the ''==''.

Billy - BillyB@mrsnj.com
(Hey, I''''m no longer anonymous!)

##### Share on other sites
Doh! I gotta remember that! I ALWAYS *repeat* - ALWAYS forget to do == ''equals to'' rather than = ''is now...". Thanks guys!

What does it take to be a good game programmer?
---
Good insight,
good knowledge,
and of course,
big money deposits!

##### Share on other sites
quote:
Original post by Wachar
I ALWAYS *repeat* - ALWAYS forget to do == 'equals to' rather than = 'is now...".

If you have this problem, you can try to get into the habit of placing any constant values on the left-hand side of the comparison expression:

if(HUMANS == playerRace) {    human1.humanSetData();    human1.main_human_story();}

That way, your compiler will complain if you accidentally use '=' instead of '=='.

[edited by - spock on May 4, 2002 5:44:55 AM]

• ### Forum Statistics

• Total Topics
627721
• Total Posts
2978803

• 10
• 9
• 21
• 14
• 12