Skip Top Navigation Bar

Write Expressions that Require do..while Loops

Your Turn

  • Write program code to complete the provided tasks.
  • Run the code in the Java Program to see if your code generates the desired result.
  • Make necessary changes until you have satisfied the tasks.

Practice 1: Enter a Positive Number

  • Write a do..while loop that will repeatedly generate and print a random number between 1 and 12 until the number 7 is generated.
  • Try the code in the Java Playground.

Practice 2: Drink Order

  • A drink cost $3 and an order consists of at least 1 drink.
  • Write a do..while loop that will repeatedly generate and print a random number between 0 and 1 to indicate whether another drink will be ordered. If a 1 is entered another drink is added to the order, otherwise the loop stops. After all the drinks are ordered, the number of drinks ordered and the total cost are printed.
  • Try the code in the Java Playground.

Practice 3: Modify the while Loop to do..while Loop

  • Run the provided code segment in the Java Playground.
  • Re-write it to be a do..while loop instead.
  • Run the new code in the Java Playground to ensure you obtain the same functionality.

Using an IDE

  • Complete the following practices using an IDE.

Practice 4: Multiplication Practice

  • Generate two integer values between 1 and 10.
  • Continuously prompt the user for the product of the two values until they enter the right answer.
  • Identify test cases and outcomes that can be used to check you code segment is functioning as expected.
  • Try the code in the IDE.

Practice 5: Grade Input

  • Continuously prompt the user for a grade or -999 to stop. Accumulate all the grades and keep track of the number of grades entered. The value -999 is not a grade, be careful to not include it in the sum of the grades or the count of grades.
  • Once all the grades have been entered, calculate and print the average.
  • Identify test cases and outcomes that can be used to check you code segment is functioning as expected.
  • Try the code in the IDE.

Resources

Practice: Evaluate Expressions that Contain do..while Loops