Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Putting a for loop inside an if statement

Putting a for loop inside an if statement
Thread Tools
Senior User
Join Date: Sep 2000
Location: UK
Status: Offline
Reply With Quote
Jun 26, 2004, 06:42 AM
 
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
Reply With Quote
Jun 26, 2004, 07:50 AM
 
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'."
     
iMacfan  (op)
Senior User
Join Date: Sep 2000
Location: UK
Status: Offline
Reply With Quote
Jun 26, 2004, 08:29 AM
 
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
Reply With Quote
Jun 26, 2004, 03:19 PM
 
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'."
     
iMacfan  (op)
Senior User
Join Date: Sep 2000
Location: UK
Status: Offline
Reply With Quote
Jun 27, 2004, 09:44 AM
 
Excellent idea - I was wondering why the compiler did not pick up my stupid mistake.

David
     
Dedicated MacNNer
Join Date: Sep 2002
Status: Offline
Reply With Quote
Jun 27, 2004, 10:23 AM
 
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
Reply With Quote
Jun 27, 2004, 03:56 PM
 
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'."
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 06:37 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2