Log in

View Full Version : is there a problem with...


AlphaOmega870
23-01-2004, 17:55
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.

Mike Soukup
23-01-2004, 18:03
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
for (n = 1; n =< 150; n++)

mtrawls
23-01-2004, 18:28
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

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)

deltacoder1020
23-01-2004, 19:39
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.

Guest
23-01-2004, 20:09
A for loop only takes 4 parameters, you've got an extra ';' at the end
I hope you meant "3" parameters, not 4 :)

echos
23-01-2004, 22:33
just thought I would emphasize on this for the n00bs.


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++;
}

Mike Soukup
24-01-2004, 01:04
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 :ahh:

Yes, I meant '3' instead of '4' and I guess I also missed the '=<'. Sorry, and thanks for noticing my mistakes.

bheller
24-01-2004, 10:30
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

just thought I would emphasize on this for the n00bs.


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++;
}