O'Reilly logo

Learning Java Through Games by Lubomir Stanchev

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 3
Loops
3.1 The while Statement ................................................................... 41
3.2 The do-while Construct ................................................................ 43
3.3 The for Loop ........................................................................... 45
3.4 Nested for Loops ....................................................................... 49
3.5 The Modulus Operation ................................................................ 52
3.6 Summary ................................................................................ 54
3.7 Syntax .................................................................................. 54
3.8 Important Points ....................................................................... 55
3.9 Exercises ................................................................................ 56
3.10 Lab ...................................................................................... 57
3.11 Project .................................................................................. 58
In the previous chapter, we showed how to use the if and switch statements in order to
choose from several possible program execution paths. In this chapter, we present different
types of flow control structures that are called loop structures. These structures allow the
same code segment to be executed multiple times. There are three loop structures in Java:
while, do,andfor.Thewhile structure allows us to repeatedly execute the same block of
code until a condition is met. The do structure is similar, but it guarantees that the code
will be executed at least once. The for structure is best suited when we know how many
times we want to execute a particular code segment. The chapter also covers the modulus
operator.
3.1 The while Statement
First, we will improve on the multiplication game from the last chapter. If the user
entered the wrong number, then we will give the user the option to enter the correct result.
The program should continue asking the user for an answer until the correct answer is
entered. The code from last chapter is shown below.
1 import java . ut il . ;
2 public c la ss Arithmetic {
3 public s ta ti c void main( String [ ] args ) {
4 int x=(int ) (Math . r and om ( ) 10) ;
5 int y=(int ) (Math . r and om ( ) 10) ;
6 Scanner keyboard = new Scanner ( System . in ) ;
7System.out.print(x+"*" +y+"=");
8 int z = keyboard . nextInt () ;
9 if (z == x y) {
10 System . out . p r i n t l n ( "Congratulations !");
11 } else {
12 System . out . p r i n t l n ( " You need more practice" );
13 }
41
42 Learning Java through Games
}
while (condition) {
true
false
FIGURE 3.1: The while construct.
14 }
15 }
In the new code, we will eliminate Lines 9–13. We will execute Lines 7–8 multiple times
until the correct answer is given by the user. Lines that need to be executed multiple times
can be surrounded by a while block. The new code for Lines 7–8 is shown next.
int z;
while (...){
System . out . p ri n t ( x + "*" +y+"=");
z = keyboard . nextInt () ;
}
If the condition in the while statement parentheses is true, then the while statement
block is executed. Otherwise, the block is skipped and the program continues executing
with the first line after the while block; see Figure 3.1.
A while statement allows the same block that is surrounded by braces to be
repeatedly executed. The block can be executed zero or more times. The program will
stop repeatedly executing the block and move to the line immediately after the block
when the condition inside the parentheses of the while statement becomes false.
In other words, we need to have a condition that should be true when we want to
execute the block and should become false when we want to move on. Maybe in our case
we can rewrite the code as follows.
int z;
while(z!=x y){
System . out . p ri n t ( x + "+" +y+"=");
z = keyboard . nextInt () ;
}
If z is not equal to x*y, then this means that the user did not answer the question
correctly. In this case, we will go ahead and ask them for input again. The only remaining
caveat is that we need to insure that the while statement is executed at least once. We
can assure that by setting the variable z initially to a value that is different from x*y.The
rewritten program follows.
import java . uti l . ;
public c la ss Arithmetic {
public s ta tic void main( String [ ] args ) {

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required