Skip Top Navigation Bar
Why Java?
Learner's Corner
Teacher's Corner
Java Playground
Home
Getting Setup
Learn
Practice
Apply
Other Resources
Write Expressions that Require while Loops
Your Turn
Write program code to complete the provided tasks.
Run the code in the Java Playground to see if your code generates the desired result.
Make necessary changes until you have satisfied the tasks.
while Loops Tutorial
Practice 1: Counting Down Timer
Generate a random number between 1 and 10 as a starting value.
Use a
while
loop to count down to zero, printing each value.
Identify test cases and outcomes that can be used to check you code segment is functioning as expected.
Try the code in the Java Playground.
Practice 2: Savings tracker
Generate a random number between 200 and 1000 as a savings goal.
Generate an amount between 1 and 50 that is being saved and add this to the total saved so far. Print this value to the console.
Keep generating and accumulating total savings until the goal is reached.
Identify test cases and outcomes that can be used to check you code segment is functioning as expected.
Try the code in the Java Playground.
Using an IDE
Complete the following practices using an IDE.
Practice 3: Email Validation
Write a
while
loop that will repeatedly prompt for an email address until the input contains
@
character.
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 4: Quiz Retake
Write a
while
loop that will simulate a person answering a quiz question. The loop should continue to prompt for the answer until
Java
is entered.
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: Guessing Game
Randomly select a number between 1 and 50.
Ask the user to guess the number. Keep looping while the guess is incorrect.
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 6: Alarm Snoozing
Simulate snoozing an alarm by asking each time:
Do you want to snooze the alarm?
Stop if the answer is
no
.
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 while Loops
Apply: While Loop Mini-Labs