1

This might be a silly question but I'm confused.

I have a for loop that excludes a given iteration (a random number). It works for any random number chosen that's greater than 0. However, if it's zero, it never does a single iteration:

int x;
for (x = 0; ((x < 2) && (x != r)); x++) {
    // do something if (x != r)

}
System.out.println("X : " + x);

For the example that it's not working, r = 0. Shouldn't that mean it should skip the first iteration but does the second?

The above println yields "X : 0".

Any help? Thanks!

5
  • It never does a single iteration because you require x != r while your x is first set to 0, like your r, causing your condition to return false and not to enter the loop Commented Nov 8, 2015 at 8:12
  • Because of the condition(x < 2) && (x != r). As r =0, the condition fails and will not enter the loop. Commented Nov 8, 2015 at 8:14
  • No, the second expression is used as a way to end the loop. If the expression evaluates to false, exit the loop: docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html Commented Nov 8, 2015 at 8:14
  • Oh, I get it now. Silly mistake! Thanks all for the help. Commented Nov 8, 2015 at 8:15
  • @tj56 When the first condition (second expression) becomes false, it exists the loop and continues execution after the loop. Commented Nov 8, 2015 at 8:16

3 Answers 3

5

If you only want to skip the first iteration, you must move the x != r condition inside the loop. Having the x != r condition in the loop's condition means the loop will never be entered if both r and x are initialized to 0, since the loop terminates when the loop's condition becomes false.

for (x = 0; x < 2; x++) {
    if (x != r) {
    // do something if (x != r)
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

The variable r is chosen randomly so it might not be always the first iteration. But assuming it's always 0, why wouldn't it work the way I posted above?
@tj56 I added an explanation. If the loop's condition is false, the loop is terminated. If it's false before the first iteration, the loop is never entered.
0
for (initialization; termination condition; increment) {
    statement(s)
}

Above code will run:

    initialization
    termination condition
    statement
    increment

    termination condition
    statement
    increment

    termination condition
    statement
    increment

    ....

So you should remove condition x != r because before it runs statement for first time, it check that condition and return false.

You should note that. without condition x != r, your loop is run only twice: x = 0 and x = 1. Maybe in your code, those value (0 and 1) doesn't output any value.

Comments

0

I think it would be great if you placed (r >= 0) the random number generation would then be picked directly from 0.

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.