Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 292
  • Last Modified:

For loop

Hi,

How is the following loop interpreted?

Thank you.

for (i=56;i>0;){
0
IAJWDDIY
Asked:
IAJWDDIY
  • 5
  • 3
  • 2
  • +1
1 Solution
 
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
 
callrsCommented:
Loop starts with i equal to 56
Continues to loop while i is greater than 0
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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
 
jkrCommented:
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
 
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 5
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now