






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 ) { body } and directs that the cycle of testing the condition and executing the body 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 { body }while( condition ); and directs that the process of executing the body and then testing the condition be continued until the condition becomes false. Note that the while loop tests the condition before executing the body, and the do-while loop tests the condition after executing the body. 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("Open the bag and ..."); while (numchips < 6) { System.out.println(" can't stop ..."); numChips = numChips + 1; } System.out.println(" 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 = 5; do { System.out.println(x); x++; } while(x < 5); 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. glassPaper = 5; while(glassPaper > 0) { System.out.println("Reduce!\nReuse!\nRecycle!\n"); glassPaper--; // the -- operator subtracts 1 from variable glassPaper }
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.
We have now mentioned all eight of the primitive data types in Java: byte, short, int, long, float, double, char and boolean. All other data types in Java are defined by a class.
Step 1. Compile and execute J03E07.java. Test the program by responding with 6. Record the results. import java.util.Scanner; class J03E { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int num; System.out.print("Enter an integer between 1 and 10: "); num = scan.nextInt(); while(num < 1 || num > 10) { System.out.print("Error: Enter again: "); num = scan.nextInt(); } System.out.println(num + " is my number too!"); } }
Step 2. Execute the program again. This time respond with numbers less than 1 or greater than 10 such as 15, 0, 23... How many times can the body of the loop be executed? Record your observations and the results.
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.
numerator denominator quotient 5 2 ______ 10 0 ______ -33 6 ______ 0 -25 ______
Step 2. Modify the program in Step 1 by inserting an if statement that protects against division by 0. Summarize your work below.
Step 3. Modify the code in Step 2 using an if-else statement that either prints the message "Error, division by 0 is not allowed" or performs the calculation and prints the original message.