Every time a forever loop is made, the statement in the parentheses is

(i = 0; i < n(or some variable); i++)

Why doesn't this work?

(n > 0; n--)

1 Answer 1


What you have displayed are loops, but not forever loops. The first loop that you describe,

for(i = 0; i < n(or some variable); i++) 
    //do something 

is the standard construction of a loop that will start with i = 0 and increment on each pass until i is no longer less than n, at which time the loop ends and processing continues with the next line of code after the curly braces. You could do exactly the same thing with this:

for(i = n; i > 0; i--) 
    //do something 

The difference here is that i is decrementing from n to 1. At 0, the loop stops. There are many variations that you could use, some more suitable than others for a given use, but all just as valid. None of these are forever loops.

A forever loop would be such that the test condition is always true. It can also be accomplished by not having a test condition. Examples:

for(i=0;  ; i++) ...
for(   ; true; ) ...
for( ;  ; i++) ...

and so on. A more common construction of a forever loop is this:

while (true)
    // do something

This will run forever, or until some code inside the loop causes it to stop, such as a break or return statement.

If this answers your question, please accept this (or another) answer to close the question. Let's keep up on forum housekeeping. ;-)

  • interesting, I learned a lot more loops than I thought I would
    – BooFluff
    Commented Jun 2, 2015 at 2:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .