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