Ibteda Sabur
Ibteda Sabur

Reputation: 31

Why does this while loop run after the condition is met?

When I do a regular while loop like this

int x = 0;

while (x < 2) {
      System.out.println(x);
      x = x + 1;
}

it prints

0
1

but this second while loop runs one more time after the condition is met.

class Hobbits {
String name;

public static void main(String [] args) {

    Hobbits [] which = new Hobbits[3];

    int number = -1;

    while (number < 2) {

        number = number + 1;

        which[number] = new Hobbits();

        which[number].name = "bilbo";

        if (number == 1) {
            which[number].name = "frodo";
        }

        if (number == 2) {
            which[number].name = "sam";
        }

        System.out.print(which[number].name + " is a ");
        System.out.println("good Hobbit name");

    }
}
}

so the result is

bilbo is a good Hobbit name
frodo is a good Hobbit name
sam is a good Hobbit name

shouldn't it stop printing at "frodo is a good Hobbit name"

I set the condition for x < 2 so how does "sam" print if the while loop was supposed to stop at x == 1?

Edit: ohhh I get it now lol I was thinking like the increment was at the end of the code and the start was 0

Upvotes: 0

Views: 1064

Answers (5)

Vicente Olivert Riera
Vicente Olivert Riera

Reputation: 1220

There's nothing wrong in that behaviour. You set number = -1 and then you do a loop that will iterate while number < 2. So, -1, 0 and 1. Three iterations. What's the problem? Let's do a simple trace:

number = -1
(-1 < 2)? Yes. Execute code inside while.
number = number + 1 (0)
(0 < 2)? Yes. Execute code inside while.
number = number + 1 (1)
(1 < 2)? Yes. Execute code inside while.
number = number + 1 (2)
(2 < 2)? No. Continue with the next instruction after the while block.

Upvotes: 0

jonathangersam
jonathangersam

Reputation: 1147

Because you have 3 iterations that are true:

  • -1 < 2 == true
  • 0 < 2 == true
  • 1 < 2 == true
  • 2 < 2 == false

Cheers

Upvotes: 0

Dimitry Rakhlei
Dimitry Rakhlei

Reputation: 191

I will try to trace through it for you.

First case this is what happens.

x = 0
is 0 < 2? yes
print 0
0 <- 0 + 1   // here it becomes 1

is 1 < 2? yes
print 1
1 <- 1 + 1  // here it becomes 2

is 2 < 2? no

exit program

second loop works a bit more like this

number = -1
is number < 2? yes
number <- -1 + 1    // here it becomes 0
make hobbit "bilbo" at index 0
0 does not equal either 1 or 2
print hobbit at index 0 who is "bilbo"

is 0 < 2? yes
0 <- 0 + 1    // here it becomes 1
number equals 1 so make "frodo" at index 1
print hobbit at index 1 who is "frodo"

is 1 < 2? yes
1 <- 1 + 1  // becomes 2
number equals 2 so make "sam" at index 2
print hobbit at index 2 who is "sam"

is 2 < 2? no
end program

Upvotes: 1

Rajpal Singh
Rajpal Singh

Reputation: 311

you can solve this easily with replace

int number = -1;

to

int number = 0;

Because! -1 + 1 = 0;

Upvotes: 0

that other guy
that other guy

Reputation: 123470

Here's your test case more closely matching your code:

class Test {
  public static void main(String[] args) {
    int x = -1;

    while (x < 2) {
      x = x + 1;
      System.out.println(x);
    }
  }
}

It does indeed print:

$ java Test
0
1
2

Your loop is not designed to stop when number is 2, it's designed to stop before the next iteration if the number is 2. Since you increment number early on in the loop, it will continue with that value until it's time to choose whether to iterate again.

  • Before the first iteration, number = -1 which is <2 so it should iterate.

  • Before the second iteration, number = 0, which is <2 so iterate.

  • Before the third iteration, number = 1, which is <2 so iterate.

  • Before the fourh iteration, number = 2, which is NOT <2 so stop.

You therefore get 3 iterations.

Your original test case had the right idea -- it's better to increment as the last step in the loop. That way, you start at 0 instead of -1, and you stop the loop based on the new value rather than the previous value.

Upvotes: 1

Related Questions