zetologos zetologos - 1 year ago 115
Java Question

for loop without index declaration

So I declare a variable some where and initialize it. Now later on I need to use it to loop while its still positive so I need to decrement it. To me looping using a condition and a decrement calls for a for but for it we are missing the first part the initialization. But I don't need to initialize anything. So how do I go about that in a nice way.

for (space = space; space > 0; space--)//my first way to do it but ide doesnt like it

Second way:

for (; space > 0; space--)//my friend recommended me this way but looks kind weird

Are there more ways for me to have a loop with only condition and increment/decrement?

P.S spell check doesn't know that "decrement" is a word. I'm pretty sure it is...

Answer Source

another way is this one:

Integer i = 10;
while(--i>0) {

When i is 0 while condition is false... so.. it will print from 9 to 1 (9 items)

Integer i = 10;
while(i-->0) {

Will print from 9 to 0... (10 items);

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download