2 more for loops

• 05-04-2006
newbie101
2 more for loops
here is what i have:

Code:

char Let = 'J';
int Bo = -3;

for (char Ind=Let; Ind<= 'J'; ++Ind) <statement>;

for (int Boun=1; Boun>=Bo;  --Boun) <statement>;

for the 1st one i dont think it will run at all since Ind=Let and Let is 'J' that makes Ind 'J' so in the next part where Ind<= 'J' he is already less the or = to 'J' since his is 'J' so the loop should run at all correct?

the 2nd one the statement should run 4 times until Boun is >=Bo correct?

thanks
• 05-04-2006
Dave_Sinkula
Once and five times, respectively. Think about what equality means.
• 05-04-2006
newbie101
i can see why the 1st one is run once but why the second 5 times and not 4? if i have 1 and want to get to -3 i would just run it 4 time wouldnt i?
• 05-04-2006
Dave_Sinkula
• 05-04-2006
indigo0086
Quote:

Originally Posted by newbie101
here is what i have:

Code:

int Bo = -3;

for (int Boun=1; Boun>=Bo;  --Boun) <statement>;

Initial Pass: Bound = 1 (> -3)
2nd pass: Bound = 0 (> -3)
3rd pass: Bound = -1(> -3)
4th: Bound =-2 (>-3)
5th: Bound = -3, bo = -3 (still bound >= Bo, so we do it one more time
finally: Bound = -4
• 05-04-2006
newbie101
thanks got it
• 05-04-2006
indigo0086
But your approach to for loops is kind of counter intuitive, generally if you want to execute a loop a certain (n) number of times, you would start at 0 and end at less than, or not equal to n, for example

Code:

for(int i = 0; i < n; i++)
<statement>;

or

for(int i = 0; i != n; ++i)
<statement>;

Why start at 0? I guess goes back to basic computre fundamentals and ho memory is referenced.