# for loop

• 02-26-2002
phantom
for loop
I have the following line->
for(j=30; j>0; j--)
Can this be re-written as ->
for(j=30; j; j--) ?

I'm not game enough to try it because it is in the middle of a complex algorithm and I already have enough problems without trying something new :) But as I wrote it I just wondered:confused:
• 02-26-2002
Unregistered
no - at least I dont think so. The middle part of the for loop tells the loop when to end, without a value I dont know what it would do
• 02-26-2002
>>>
I'm not game enough to try it because it is in the middle of a complex algorithm and I already have enough problems without trying something new But as I wrote it I just wondered
<<<

So write a simple test program and try it! You'll find you'll be doing that a lot.

As a hint to the answer, the "middle" term is evaluated as a boolean expression, when an integer has the value zero it is FALSE...

As a style point though, which is less likely to confuse? Moral, just because something is legal, doesn't mean it should be used.
• 02-26-2002
Dummies102
well, depends what you want the program to do, the first one is:
j=30, take away 1 from j as long as j is greater than 0.
the second one would be:
j=30, take away 1 from j as long as j is j.
• 02-26-2002
phantom
Thanks for the replies.

Quote:

So write a simple test program and try it! You'll find you'll be doing that a lot.
The reason I didn't do this is because (as I said before) I am right in the middle of a complex algorithm and didn't want to break my 'concentration', it will be something I will be testing when things slow down a bit around here :)