Problems With "if" Statements

Programming, for all ages and all languages.
Post Reply
Bobalandi
Member
Member
Posts: 107
Joined: Mon Dec 03, 2007 7:26 am
Location: Near Boston, MA
Contact:

Problems With "if" Statements

Post 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?
Tyler
Member
Member
Posts: 514
Joined: Tue Nov 07, 2006 7:37 am
Location: York, England

Re: Problems With "if" Statements

Post by Tyler »

Code: Select all

if (shift == 0)
           putch(kbdus[scancode]);
else if (shift == 1)
           putch(upcase[scancode]);
User avatar
Alboin
Member
Member
Posts: 1466
Joined: Thu Jan 04, 2007 3:29 pm
Location: Noricum and Pannonia

Post 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....)
C8H10N4O2 | #446691 | Trust the nodes.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Problems With "if" Statements

Post 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!
Every good solution is obvious once you've found it.
Bobalandi
Member
Member
Posts: 107
Joined: Mon Dec 03, 2007 7:26 am
Location: Near Boston, MA
Contact:

Post 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.
User avatar
AndrewAPrice
Member
Member
Posts: 2299
Joined: Mon Jun 05, 2006 11:00 pm
Location: USA (and Australia)

Post 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
My OS is Perception.
Post Reply