• Post Reply Bookmark Topic Watch Topic
  • New Topic

Loops  RSS feed

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!