View Single Post
  #3   Spotlight this post!  
Unread 26-01-2005, 17:41
Dave Scheck's Avatar
Dave Scheck Dave Scheck is offline
Registered User
FRC #0111 (WildStang)
Team Role: Engineer
 
Join Date: Feb 2003
Rookie Year: 2002
Location: Arlington Heights, IL
Posts: 574
Dave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond repute
Re: Weird problem with if statements

Quote:
Originally Posted by Don Reid
The compilers don't complain because it is legal and sometimes done on purpose.
To add on to what Don said, if you want to get the compiler to complain, you can try to get in the habit of doing the following.

if(1 == x)
{
}
instead of
if(x == 1)
{
}

With the correct comparison syntax (as written), you should see no difference when you compile, but if you were to mistakenly put a single '=' instead of a double, you would see the a syntax error. This is because it tries to assign the value of x to 1. Since you don't have permission to write to 1, you get the error.

Note that if you were to be comparing two variables, instead of a variable to a constant number, you wouldn't see the syntax error because you would be allowed to write one variable into another (in most cases).

There are some people that use this as their coding standard. I personally don't use it, but it may be something that helps you get in the habit of remembering the == when comparing.