PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Oct 03, 2006 4:43 pm |
|
|
| Quote: | | But just for my information would j = 0 to <=40 loop 40 times or 41 times ? |
The following code will loop for 41 times. That's because the count
starts at zero, and it will run as long as the count is less than or equal
to 40. In other words, it will run for a count of 0 through 40, which is
a total of 41 times through the loop. (0, 1, 2, 3, ... 38, 39, 40).
| Code: | for (k=0; k <= 40; k++)
{
// do something
} |
I don't normally use that construct ( <= ), because I want to see
instantly what the total loop count will be. If I do it as shown below,
starting at 0, and then using "less than" as the test, the total loop count
will be the number that I plug in on the right side of the test (40).
| Code: | for (k=0; k < 40; k++)
{
// do something
} |
|
|