Go to Post The real world does not go together like an Erector set. FIRST robots should not either. - dlavery [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 23-01-2004, 17:55
AlphaOmega870 AlphaOmega870 is offline
Registered User
AKA: Mike
#0870 (Southold RICE)
Team Role: Programmer
 
Join Date: Oct 2002
Location: Southold, NY
Posts: 18
AlphaOmega870 is an unknown quantity at this point
Send a message via AIM to AlphaOmega870
is there a problem with...

for ( n = 1; n =< 150; n++; )
this line of code?
I continuosly get a syntax error with this line and can find no problem with it. Thank you in advance.
  #2   Spotlight this post!  
Unread 23-01-2004, 18:03
Mike Soukup's Avatar
Mike Soukup Mike Soukup is offline
Software guy
FRC #0111 (Wildstang)
Team Role: Engineer
 
Join Date: May 2001
Rookie Year: 1996
Location: Schaumburg, IL
Posts: 797
Mike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond repute
Re: is there a problem with...

Quote:
Originally Posted by AlphaOmega870
for ( n = 1; n =< 150; n++; )
this line of code?
I continuosly get a syntax error with this line and can find no problem with it. Thank you in advance.
A for loop only takes 4 parameters, you've got an extra ';' at the end. It should be
Code:
for (n = 1; n =< 150; n++)
  #3   Spotlight this post!  
Unread 23-01-2004, 18:28
mtrawls's Avatar
mtrawls mtrawls is offline
I am JVN! (John von Neumann)
#0122 (NASA Knights)
Team Role: Programmer
 
Join Date: Mar 2003
Location: Hampton, VA
Posts: 295
mtrawls is a splendid one to beholdmtrawls is a splendid one to beholdmtrawls is a splendid one to beholdmtrawls is a splendid one to beholdmtrawls is a splendid one to beholdmtrawls is a splendid one to beholdmtrawls is a splendid one to behold
Send a message via AIM to mtrawls
Re: is there a problem with...

Quote:
Originally Posted by AlphaOmega870
for ( n = 1; n =< 150; n++; )
this line of code?
I continuosly get a syntax error with this line and can find no problem with it. Thank you in advance.
Err ... aside from the mentioned extra semicolon at the end, unless my memory has become severly skewed, I do believe you mean "<=" for less than or equal to, as opposed to "=<". But why such wird code? I mean, really, who starts counting at 1? (Besides normal people, of course

Traditionally, though, you'd say
Code:
for (n=0; n < 150; n++)
This is because C starts indexing at 0, like most sane languages. You can also avoid having an equal sign in the for loop. I suppose it is a matter of taste, though ... (and what you do with n within the loop)
  #4   Spotlight this post!  
Unread 23-01-2004, 19:39
deltacoder1020's Avatar
deltacoder1020 deltacoder1020 is offline
Computer Guy
AKA: Dav
#1020 (The Indiana Prank Monkeys)
Team Role: Programmer
 
Join Date: Jan 2004
Location: Muncie, Indiana
Posts: 340
deltacoder1020 has a spectacular aura aboutdeltacoder1020 has a spectacular aura about
Send a message via AIM to deltacoder1020
Re: is there a problem with...

and do make sure that you've declared 'n' as well but the main problems are probably those already pointed out, i.e. it should be <=, and there should only be 2 semicolons.
__________________
Team 1020, the Indiana Prank Monkeys (www.team1020.org)
  #5   Spotlight this post!  
Unread 23-01-2004, 20:09
Guest
 
Posts: n/a
Re: is there a problem with...

Quote:
Originally Posted by Mike Soukup
A for loop only takes 4 parameters, you've got an extra ';' at the end
I hope you meant "3" parameters, not 4
  #6   Spotlight this post!  
Unread 23-01-2004, 22:33
echos's Avatar
echos echos is offline
Jack of all trades
#1125
Team Role: Programmer
 
Join Date: Dec 2003
Location: San Diego, CA
Posts: 61
echos will become famous soon enough
Send a message via ICQ to echos Send a message via AIM to echos Send a message via MSN to echos Send a message via Yahoo to echos
Re: is there a problem with...

just thought I would emphasize on this for the n00bs.

Code:
 for (n = 0; n <= 150; n++)
{
 // For statements are a type of loop for exuting a snipet of until 
 // it evalueates to false. In the above statement the structure goes 
 // as follows. The first part is the intial variable settings before 
 // looping, the secound part is the testing part, and the third part is 
 // what to do after every loop. 

// The following example will loop 150 times before evaluating to false 
// or 0 ending the loop. 

// code
}

another variation
n = 0

while(n <= 150)
{
// Simular to the above statement except that this uses a "global" 
// variable which can be called other places in the program. 

// code

n++;
}
  #7   Spotlight this post!  
Unread 24-01-2004, 01:04
Mike Soukup's Avatar
Mike Soukup Mike Soukup is offline
Software guy
FRC #0111 (Wildstang)
Team Role: Engineer
 
Join Date: May 2001
Rookie Year: 1996
Location: Schaumburg, IL
Posts: 797
Mike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond repute
Re: is there a problem with...

Quote:
Originally Posted by SilverStar
I hope you meant "3" parameters, not 4
Oh boy, that's what I get for trying to do work and read CD at the same time. Hopefully I didn't make such stupid mistakes on my real code

Yes, I meant '3' instead of '4' and I guess I also missed the '=<'. Sorry, and thanks for noticing my mistakes.
  #8   Spotlight this post!  
Unread 24-01-2004, 10:30
bheller bheller is offline
Registered User
AKA: Brandon Heller
#0931
Team Role: Mentor
 
Join Date: Jun 2002
Location: Saint Louis, MO
Posts: 13
bheller is on a distinguished road
Re: is there a problem with...

One of your comments is off. Your for loop in C:

for ( n = 0; n <= 150; n++)

will do this:
-set n to zero, run code once
-go back to the for statement, add 1, is n <= 150? yes, so run code twice
-...
-(n is now 150) go back to the for statement, add 1, is n <= 150? no, so stop.

The loop will run 151 times, NOT 150 times. Think of a simpler for loop:

for ( n = 0; n <= 2; n++)

n = 0, execute loop.
n = 1, execute loop,
n = 2, execute loop,
n = 3, stop.

If you start at zero and go to 2, the loop runs 3 times - the final valid number, plus one.

Which is why you typically see for (n = 0; n < 150; n++) instead.

The same reasoning applies to the lower for statement.

-Brandon Heller

Quote:
Originally Posted by echos
just thought I would emphasize on this for the n00bs.

Code:
 for (n = 0; n <= 150; n++)
{
 // For statements are a type of loop for exuting a snipet of until 
 // it evalueates to false. In the above statement the structure goes 
 // as follows. The first part is the intial variable settings before 
 // looping, the secound part is the testing part, and the third part is 
 // what to do after every loop. 

// The following example will loop 150 times before evaluating to false 
// or 0 ending the loop. 

// code
}

another variation
n = 0

while(n <= 150)
{
// Simular to the above statement except that this uses a "global" 
// variable which can be called other places in the program. 

// code

n++;
}
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Victor problem Andy Baker Electrical 15 24-06-2003 09:49
Ok one problem cantwell03 Programming 3 13-02-2003 07:28
The problem with scouting... archiver 2001 10 23-06-2002 23:49
joystick problem archiver 2000 12 23-06-2002 23:08
Major problem with chipphua motors aka Scott White Motors 18 19-03-2002 19:44


All times are GMT -5. The time now is 19:52.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi