• C

# For loop

Hi,

How is the following loop interpreted?

Thank you.

for (i=56;i>0;){
###### Who is Participating?

Commented:
Well, if they were like

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

Yes, they would, e.g.

for (i=56;i>0;){

a=z[i--];
}
0

Commented:
Set i to 56.

Then, as long as i is greater than 0
execute the following compound statement.
0

Commented:
'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.
0

Commented:
Loop starts with i equal to 56
Continues to loop while i is greater than 0
0

Commented:
0

Commented:
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--){
0

Commented:
>>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."?
0

Author Commented:
So if there is a statement like:

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

inside the loop would this reduce?

Thanks
0

Author Commented:
Thank you.
0

Commented:
>>What was unclear

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

Commented:

IAJWDDIY, it's almost hidden away, but there's a "Split points" link in open questions at the bottom ;)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.