 |
 |
Putting a for loop inside an if statement
|
 |
|
 |
|
Senior User
Join Date: Sep 2000
Location: UK
Status:
Offline
|
|
Hi,
I'm trying to program my palm in C, but it doesn't want to do a for loop inside an if statement:
int x = 0;
if(x = 0)
{
for(i=1 ; i<=30000 ; i++)
{
SndDoCmd(NULL, &snd, 0);
SysTaskDelay( delay );
if(EvtSysEventAvail(true))
{i=30000;}
}
}
Without the if(x=0){}, everything works fine.
Thanks for any help,
David
|
|
|
| |
|
|
|
 |
|
 |
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
The problem isn't putting the for-statement inside. The problem is you miswrote your if-statement.
x = 0 means "assign the value 0 to the variable x"
x == 0 means "check if x is equal to zero, and if so, yield a non-zero result"
You should be using the second one in your if-statement, but you are using the first. The expression "x = 0" evaluates to 0, so whatever is inside it will never get executed.
|
|
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Sep 2000
Location: UK
Status:
Offline
|
|
Thank you so much!
I guess it's the sort of thing that you just don't see if you wrote it yourself.
I can assure you that there is much slapping of foreheads here!
David
|
|
|
| |
|
|
|
 |
|
 |
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
Yep. It's an easy mistake to miss. That's the reason why many programmers make it a point to put constants first:
if (0 == x)
if (nil == passedObject)
Because you can't assign to constants, so if you accidentally miss an equals sign, the compiler will recognize that you've made a mistake.
|
|
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Sep 2000
Location: UK
Status:
Offline
|
|
Excellent idea - I was wondering why the compiler did not pick up my stupid mistake.
David
|
|
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Sep 2002
Status:
Offline
|
|
I was wondering why the compiler did not pick up my stupid mistake
Because it's not a mistake. In C it is perfectly legal to do something like
Code:
if( x = 0 )
{
...
}
and a variety of similar things. However, I think most compilers will have an option somewhere to issue warnings when such things are tried. Check the documentation.
|
|
|
| |
|
|
|
 |
|
 |
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
Like I said, if() just checks whether the expression in the parentheses evaluates to a non-zero value, and if so executes the associated block of code. So if you write if (x = 0), you have entered a valid C statement that always evaluates to 0.
|
|
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|