• C

For loop

Hi,

How is the following loop interpreted?

Thank you.

for (i=56;i>0;){
IAJWDDIYAsked:
Who is Participating?
 
jkrConnect With a Mentor 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
 
imladrisCommented:
Set i to 56.

Then, as long as i is greater than 0
execute the following compound statement.
0
 
jkrCommented:
'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
Worried about phishing attacks?

90% of attacks start with a phish. It’s critical that IT admins and MSSPs have the right security in place to protect their end users from these phishing attacks. Check out our latest feature brief for tips and tricks to keep your employees off a hackers line!

 
callrsCommented:
Loop starts with i equal to 56
Continues to loop while i is greater than 0
0
 
callrsCommented:
0
 
callrsCommented:
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
 
jkrCommented:
>>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
 
IAJWDDIYAuthor Commented:
So if there is a statement like:

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

inside the loop would this reduce?

Thanks
0
 
IAJWDDIYAuthor Commented:
Thank you.
0
 
callrsCommented:
>>What was unclear

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

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.