




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





In this exercise you will:
The while statement has the form
while ( condition ) {
blockOfStatements ;
}
and directs that the cycle of testing the condition and executing the blockOfStatements be continued as long as the test confirms that the condition is true. This iterative statement is called a looping structure and the while statement is called a while loop.
A slight variation of the while loop is the do-while loop. It has the form
do {
blockOfStatements ;
} while ( condition );
and directs that the process of executing the blockOfStatements and then testing the condition be continued until the condition becomes false.
Note that the while loop tests the condition before executing blockOfStatements , and the do- while loop tests the condition after executing blockOfStatements. Therefore, the body of the do- while loop is always executed at least once.
Step 1. Predict the output of this program.
class J03E { public static void main(String[] args) { int numChips = 1; System.out.println("I open the bag and ..."); while (numChips < 4) { System.out.println(" eat chip no. " + numChips); numChips = numChips + 1; } System.out.println("I JUST CAN'T STOP!"); } }
Step 3. Predict the output of this program, which is similar to the program in Step 1, but uses a do-while loop instead of a while loop.
class J03E05B { public static void main(String[] args) { int x = 4; do { System.out.println(x); x++; } while (x < 4); System.out.println("That's all folks!"); } }
Step 4. Compile and execute program J03E05B.java. Record the results.
Step 5. Explain the differences in the output between the two programs in Steps 1 and 3.
Step 1. Rewrite the following program segment using a do-while loop rather than a while loop. Your segment should produce the same output as the original while loop.
countdown = 10; while (countdown >= 0) { System.out.print (countdown + " "); if (countdown == 6) { System.out.print("Ignition! "); } countdown--; // the -- operator subtracts 1 from variable countdown } System.out.print("Liftoff! ");
Step 2. Expand the given segment and the segment you wrote in Step 1 into two programs, and test them to confirm that your version is correct. Summarize your work below.
The logical operators AND (represented by &&) and OR (represented by 2 vertical lines, ||) can be used to construct more complicated boolean expressions. For example, the blockOfStatements of this while loop will be executed as long as the value of x lies between 5 and 10, inclusive.
while (x >= 5 && x <= 10) {
blockOfStatements ;
}
If expression1 and expression2 are boolean expressions, then
expression1 && expression
has value true if both expression1 and expression2 are true. Otherwise, it is false.
In contrast, the body of the following while loop will be executed as long as the value of x is either less than 5 or greater than 10.
Step 3. Modify the program by changing the condition. Replace (num < 1 || num > 10) with (num < 1 && num > 10). Execute the modified program, testing different input values. For what values of num is the body of the loop executed? Record and explain your results.
Java defines a primitive data type boolean. A boolean variable can store one of two possible values, true or false. The statements
boolean playAgain = false; boolean outOfBounds = true;
declare and initialize two variables of data type boolean.
Step 1. Compile and execute J03E08.java. Test the program by responding initially with “London”, then “Berlin”, and finally “Rome”. Record your observations.
import java.util.Scanner; class J03E { public static void main(String[] args) { Scanner scan = new Scanner(System.in);
String answer = ""; boolean correctAnswer = false;
while (!correctAnswer) { System.out.print("The capitol of Italy is: "); answer = scan.nextLine();
if (answer.equals("Rome")) {
correctAnswer = true; } }
System.out.println("Correct: " + answer + " is the capitol of Italy."); } }