Learning Java by Building Android  Games
上QQ阅读APP看书,第一时间看更新

While loops

Java while loops have the simplest syntax. With the while loop, we put an expression that can evaluate to true or false. If the expression is true, the loop executes. If it is false it doesn't. Take a look at this code:

int x = 10;

while(x > 0){
   x--;
   // x decreases by one each pass through the loop
}

What happens here is this: Outside of the while loop an int named x is declared and initialized to 10. Then the while loop begins. Its condition is x > 0. So, the while loop will continue looping through the code in its body until the condition evaluates to false. So, the code above will execute 10 times.

On the first pass x = 10 then 9 then 8 and so on. But once x is equal to 0, it is of course no longer greater than 0, the program will exit the loop and continue with the first line of code after the while loop.

It is important to realize that it is possible that the while loop will not execute even once. Take a look at this.

int x = 10;

while(x > 10){
   // more code here.
   // but it will never run 
   // unless x is greater than 10.
}

Moreover, there is no limit to the complexity of the conditional expression or the amount of code that can go in the loop body.

int lives = 3;
int aliens = 12;

while(lives > 0 || aliens > 0){
   // keep playing game
   // etc.
}

// continue here when lives and aliens equal 0

The above while loop would continue to execute until both lives and aliens were equal to or less than zero. As the condition uses logical OR operator || either one of those conditions being true will cause the while loop to continue executing.

It is worth noting that once the body of the loop has been entered it will always complete even if the expression evaluates to false, part way through, as it is not tested again until the code tries to start another pass. For example:

int x = 1;

while(x > 0){
   x--;
   // x is now 0 so the condition is false
   // But this line still runs
   // and this one
   // and me!

}

The above loop body will execute exactly once. We can also set a while loop that will run forever; called an infinite loop. Here is one.

int x = 0;

while(true){
   x++; // I am going to get mighty big!
}

The keyword true unsurprisingly evaluates to true. So, the loop condition is met. We need an exit plan.

Breaking out of a loop

We might use an infinite loop like this next code so that we can decide when to exit the loop from within its body. We would do this by using the break keyword when we are ready to leave the loop body. Like this:

int x = 0;

while(true){
   x++; // I am going to get mighty big
   
   break; // No you're not
   // code doesn't reach here
}

We can combine any of the decision-making tools like if, else, and switch within our while loops and all the rest of the loops we will look at in a minute. Here is a sneak look ahead at the if keyword. For example:

int x = 0;
int tooBig = 10;

while(true){
   x++; // I am going to get mighty big!
   if(x == tooBig){
      break;
   } // No, you're not ha-ha.
   
   // code reaches here only until x = 10
}

You can probably work out for yourself that the if(x == tooBig) line of code causes the break statement to execute when x equals tooBig.

It would be simple to go on for many more pages showing the versatility of while loops but at some point, we want to get back to doing some real programming. So here is one last concept combined with while loops.