Lab 10: Loop Drills
Three loop types, sentinel input, accumulators, and validation
Learning Objectives
After completing this lab, you will be able to:
- Choose the right loop type (
while,for,do-while) for a given problem - Implement a sentinel loop that terminates on a special input value
- Use an accumulator variable to compute a running total
- Validate user input with a
do-whileloop
What You’re Building
Three independent methods, each demonstrating a different loop type. Together they form a mini toolkit: a sentinel-based sum calculator, a factorial computer, and an input validator. Each method is called from main so the autograder can test them independently.
Concepts and Common Misconceptions
| Concept | What students get wrong |
|---|---|
while loop |
Forgetting to update the loop variable inside the body, causing an infinite loop. |
for loop |
Off-by-one errors in the bounds. for (int i = 1; i <= n; i++) runs n times; i < n runs n - 1 times. |
do-while |
Forgetting the semicolon after the closing parenthesis: } while (condition); |
| Sentinel value | Including the sentinel in the sum. If -1 is the sentinel, do not add -1 to the total. |
| Accumulator init | Initializing the accumulator to 1 for a sum (should be 0) or to 0 for a product (should be 1). |
Checkpoint 1: Sentinel Sum (1 pt)
Write a method sentinelSum(Scanner scanner) that repeatedly reads integers until the user enters -1. Return the sum of all entered values, excluding the sentinel.
Enter numbers (-1 to stop): 10 20 30 -1
Sum: 60
Use a while loop. Read the first value before the loop, then check if it is the sentinel before entering the loop body.
Debugging tip: If your sum is off by -1, you are adding the sentinel to the total. Read the value, check it, then add it – in that order.
Checkpoint 2: Factorial (1 pt)
Write a method factorial(int n) that returns n! using a for loop. Return 1 for n == 0 (by definition, 0! = 1).
factorial(5) -> 120
factorial(0) -> 1
Use a long return type to avoid overflow for larger values.
Debugging tip: If factorial(0) returns 0, your accumulator is initialized to 0 instead of 1. For multiplication accumulators, always start at 1.
Checkpoint 3: Input Validation (1 pt)
Write a method getValidAge(Scanner scanner) that prompts the user for an age between 0 and 150 inclusive. Use a do-while loop to keep asking until the input is valid, then return the valid value.
Enter age (0-150): -5
Invalid. Enter age (0-150): 200
Invalid. Enter age (0-150): 25
The method returns 25.
Debugging tip: If the loop runs forever, print the value you read and the condition you are checking. Make sure you are reading a new value inside the loop, not outside it.
How to Debug
- Trace by hand. Before running, write down what each variable holds after each iteration for a small input (3 numbers for sentinel, factorial of 3).
- Test boundaries. Sentinel: what if the first input is -1? Factorial: what about 0 and 1? Validation: what about exactly 0 and 150?
- Print inside the loop. Add
System.out.println("i=" + i + " product=" + product);to see each step. - Run the autograder after each checkpoint. Fix issues early.
Scoring Breakdown
| Component | Points |
|---|---|
| Checkpoint 1: Sentinel sum with while loop | 1 |
| Checkpoint 2: Factorial with for loop | 1 |
| Checkpoint 3: Input validation with do-while | 1 |
| Autograder correctness | 5 |
| Timeliness (on-time submission) | 2 |
| Total | 10 |