Nick Woods

Greenhorn

Posts: 3

posted 2 years ago

Here's my program:

public class JavaProgram {

public static void main (String[] args) {

int x = 8;

while (x > 5) {

System.out.println("Carrots and potatoes");

x = x-1;

}

System.out.println("Bananas);

}

}

The output i get is:

Carrots and potatoes

Carrots and potatoes

Carrots and potatoes

Bananas

Shouldn't i have Carrots and potatoes only twice since first time (x = 8-1 so first "x" is 7 and then x = 7-1 so next "x" is 6. So why do i get 3 outputs since the 3rd x would be equal and would not satisfy the (bigger than) condition?

public class JavaProgram {

public static void main (String[] args) {

int x = 8;

while (x > 5) {

System.out.println("Carrots and potatoes");

x = x-1;

}

System.out.println("Bananas);

}

}

The output i get is:

Carrots and potatoes

Carrots and potatoes

Carrots and potatoes

Bananas

Shouldn't i have Carrots and potatoes only twice since first time (x = 8-1 so first "x" is 7 and then x = 7-1 so next "x" is 6. So why do i get 3 outputs since the 3rd x would be equal and would not satisfy the (bigger than) condition?

posted 2 years ago

- 1

You set x to 8.

You check to see if x is greater than 5. it is.

You print "Carrots and potatoes".

You reduce the value of x by 1, so x is 7.

You go back to the top of the loop.

You check to see if x is greater than 5. it is.

You print "Carrots and potatoes".

You reduce the value of x by 1, so x is 6.

You go back to the top of the loop.

You check to see if x is greater than 5. it is.

You print "Carrots and potatoes".

You reduce the value of x by 1, so x is 5.

You go back to the top of the loop.

You check to see if x is greater than 5. it is not.

You skip the loop body

You print Bananas.

You check to see if x is greater than 5. it is.

You print "Carrots and potatoes".

You reduce the value of x by 1, so x is 7.

You go back to the top of the loop.

You check to see if x is greater than 5. it is.

You print "Carrots and potatoes".

You reduce the value of x by 1, so x is 6.

You go back to the top of the loop.

You check to see if x is greater than 5. it is.

You print "Carrots and potatoes".

You reduce the value of x by 1, so x is 5.

You go back to the top of the loop.

You check to see if x is greater than 5. it is not.

You skip the loop body

You print Bananas.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

Fix World

Greenhorn

Posts: 2

posted 2 years ago

I modified your program to print values of x at every step, hope the output makes the result clear.

And the output is

Nick Woods wrote:Here's my program:

public class JavaProgram {

public static void main (String[] args) {

int x = 8;

while (x > 5) {

System.out.println("Carrots and potatoes");

x = x-1;

}

System.out.println("Bananas);

}

}

The output i get is:

Carrots and potatoes

Carrots and potatoes

Carrots and potatoes

Bananas

Shouldn't i have Carrots and potatoes only twice since first time (x = 8-1 so first "x" is 7 and then x = 7-1 so next "x" is 6. So why do i get 3 outputs since the 3rd x would be equal and would not satisfy the (bigger than) condition?

I modified your program to print values of x at every step, hope the output makes the result clear.

And the output is

With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |