1

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...

5 Answers 5

6

another way is this one:

Integer i = 10;
while(--i>0) {
    System.out.println(i);
}

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

Integer i = 10;
while(i-->0) {
    System.out.println(i);
}

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

Sign up to request clarification or add additional context in comments.

Comments

4

The latter way is reasonable. An alternative - if you don't have any break/continue statements - would be:

while (space > 0)
{
    // Code

    space--;
}

Comments

0

In my opinion, this is a proper way to go. You might work with iterators that are able to move both forward and backward, but this doesn't necessarily all value to your code.

Comments

0

Use a while loop?

while (space > 0) { /* code */ space--; }

If you don't need the value of space in the body of the loop:

while (space-- > 0) { /* code */ }

7 Comments

The post-decrement here will happen before the first body, so the loop will never see it in its "original" value, if you see what I mean.
Yep, fixed accordingly. It can be useful though in the original form if you just want to make sure the condition is satisfied. Thanks for the correction.
@JonSkeet But a pre-decrement would fix that. Right?
No Jon is saying you'll never see the first value of space (if you need it).
@AbdullahJibaly: The way you've got it, the "..." would still never see the original value of space. The "..." needs to be before space--.
|
0

When you're writing code, you should always remember that you're not just writing it so that you can understand it now... it should be understandable to any future reader (which might be you after you've forgotten why you did it the way you did) as long as they have some coding knowledge. It seems to me that you might be trying to re-use your space variable just because you already have one. There's nothing wrong with starting a new one if it increases readability. So you might want to consider using int i = space; inside your for loop - I'm sure your computer will manage this without the stack overflowing, if you'll forgive my lousy pun ;-)

In fact, consider refactoring your loop to a separate private method, appropriately named for readability of course, passing in space as an argument and setting the variable to the result you return. Happy to give you a code example if you explain what you're trying to achieve in your loop.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.