Link to home
Start Free TrialLog in
Avatar of IAJWDDIY
IAJWDDIY

asked on

For loop

Hi,

How is the following loop interpreted?

Thank you.

for (i=56;i>0;){
Avatar of imladris
imladris
Flag of Canada image

Set i to 56.

Then, as long as i is greater than 0
execute the following compound statement.
Avatar of jkr
'i' is initialized to 56 and the loop is executed as long as 'i' does not drop below '1'. However, since the 'loop expression' (as in "for( [init-expr]; [cond-expr]; [loop-expr] )") does not change 'i', it has to be altered inside the loop body to meet that criteria.
Avatar of callrs
callrs

Loop starts with i equal to 56
Continues to loop while i is greater than 0
Need to reduce i, else you have infinite loop. This will reduce it by one on each iteration of the loop:

for (i=56;i>0;i--){
>>Need to reduce i

What was unclear about "However, since the 'loop expression' [...] does not change 'i', it has to be altered inside the loop body to meet that criteria."?
Avatar of IAJWDDIY

ASKER

So if there is a statement like:

 a=z[i-] ; b=z[i-].....

inside the loop would this reduce?

Thanks
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you.
>>What was unclear

Nothing wrong with more clarity, adding the words "infinite loop', and giving an i--  option.   : )

IAJWDDIY, it's almost hidden away, but there's a "Split points" link in open questions at the bottom ;)