posted 5 years ago

Hello,

Can someone enlighten me on both statements below and their uses in java? :

x=x+1; //does it exactly mean loop forever?

x=x-1; // does this mean "or we'd loop forever when used?

Can someone enlighten me on both statements below and their uses in java? :

x=x+1; //does it exactly mean loop forever?

x=x-1; // does this mean "or we'd loop forever when used?

posted 5 years ago

Neither of those statements has any inherent relationship to looping, though they may be used in a loop.

The first one just says "add 1 to the current value of x, and replace what's in x with the new value," so if x was 3 before that statement, it will be 4 afterward. The second one similarly subtracts one from x. Note that it's more common to use

What either one of those means in terms of how long your loop will run depends on the loop's condition and on what else is in the loop.

Wilson Oluwasegun wrote:Hello,

Can someone enlighten me on both statements below and their uses in java? :

x=x+1; //does it exactly mean loop forever?

x=x-1; // does this mean "or we'd loop forever when used?

Neither of those statements has any inherent relationship to looping, though they may be used in a loop.

The first one just says "add 1 to the current value of x, and replace what's in x with the new value," so if x was 3 before that statement, it will be 4 afterward. The second one similarly subtracts one from x. Note that it's more common to use

`x++`or

`++x`and

`x--`or

`--x`.

What either one of those means in terms of how long your loop will run depends on the loop's condition and on what else is in the loop.