Page 1 of 1

Problems With "if" Statements

Posted: Wed Dec 05, 2007 8:57 pm
by Bobalandi
Alright, I have this code:

Code: Select all

if (shift = 0)
           putch(kbdus[scancode]);
else if (shift = 1)
           putch(upcase[scancode]);
and for some reason only the putch(upcase[scancode]); is executed. It doesn't check whether the first statement is true. I've tried putting it in curly braces too, but it still doesn't work. Any one have an idea what is wrong?

Re: Problems With "if" Statements

Posted: Wed Dec 05, 2007 9:13 pm
by Tyler

Code: Select all

if (shift == 0)
           putch(kbdus[scancode]);
else if (shift == 1)
           putch(upcase[scancode]);

Posted: Wed Dec 05, 2007 10:47 pm
by Alboin
Hey, to explain a bit, '=' is assignment, while '==' is a test for equality.

It's C's way of doing things. For example, in Pascal, the '=:' is used for assignment. (IIRC....)

Re: Problems With "if" Statements

Posted: Wed Dec 05, 2007 11:59 pm
by Solar
Bobalandi wrote:Any one have an idea what is wrong?
Three things:

1) You compiled without warnings enabled (for GCC, -Wall at minimum), or the compiler would have told you that...

2) ...you used a single "=", which means you are making an assignment (which evaluates to the value assigned). Then there is...

3) ...the suspicion that either (if shift can be 0 or 1 only) you'd be better off with a simple if - else (instead of if - else if), or (if shift can have more than three states) a switch - case.

Take care!

Posted: Thu Dec 06, 2007 2:33 pm
by Bobalandi
Thanks for replying, I figured out that I needed to add == instead of = but now it is only executing the first statement even when it is not true., also I made one if statement.

Posted: Thu Dec 06, 2007 8:54 pm
by AndrewAPrice
I had the person sitting next to be doing:

Code: Select all

if(renderme);
   Render();
and he was doing step-by-step debugging, VS reported "renderme" was false, yet it took 10 minutes to figure out why Render() was sill being called :D