CSCE145 F2026 03 Loops Part 01
Watch on YouTube →
Overview
Java loops repeat statements while a Boolean condition remains true, with `while` checking before each pass and `do-while` guaranteeing one pass before checking. Examples build a number-guessing game with `Scanner` and `Random`, calculate factorials, diagnose infinite loops, and use nested loops plus variable scope to let the game restart.
Key takeaways
- A Java `while` loop is a pre-test loop: if its condition is false initially, its body runs zero times.
- A Java `do-while` is a post-test loop, so it always executes at least once and requires a semicolon after its condition.
- `Random.nextInt(100)` generates values from 0 through 99 because the bound is exclusive.
- A loop must update state so its condition can eventually become false; incrementing 10 while checking whether it is less than or equal to zero never makes progress toward termination.
- Floating-point values such as `double` may not reach an exact target after repeated decimal arithmetic, making equality-based loop termination unreliable.
- A variable declared inside a loop body is unavailable to a `do-while` condition placed after that body; declare condition variables in an enclosing scope.
Chapters
0:00
How Java `while` Loops Repeat Until a Condition Fails
- A `while` loop checks its Boolean condition before each iteration and exits as soon as it evaluates to false.
- Curly braces mark the loop body; unlike a `do-while`, a `while` header has no trailing semicolon.
- A condition that is false at the start skips the body entirely; the flowchart shows true returning to the condition and false continuing past the loop.
3:46
Build a Number-Guessing Game with `Scanner` and `Random`
- The Java program uses `Scanner` for console guesses and `Random.nextInt(100)` to select an integer from 0 through 99.
- A `while (!correctGuess)` loop keeps requesting guesses; `if` and `else if` report whether each guess is above or below the secret number.
- A correct guess prints a success message and sets `correctGuess` to true, so the condition fails on the next check.
- The debugger walkthrough traces a secret value of 87: guesses 50 and 95 are classified as low and high before 87 ends the loop.
15:21
Use `do-while` When the Loop Must Run at Least Once
- A `do-while` executes its body before checking the Boolean condition, unlike a pre-test `while` loop.
- With `A = 10` and a condition `A < 10`, a `do-while` prints 10 once, while a `while` loop would print nothing.
- Java requires a semicolon after the closing condition of a `do-while`; curly braces make its body boundaries explicit.
17:31
Calculate a Factorial by Decrementing a Copy in a `do-while`
- The factorial example reads an integer with `Scanner` and exits if the input is zero or negative.
- It preserves the original input in `factValue`, uses `factCopy` as a countdown, and initializes the product `factorial` to 1.
- Each iteration multiplies `factorial` by `factCopy` and decrements the copy; input 5 produces 5 × 4 × 3 × 2 × 1 = 120.
22:31
Prevent Infinite Loops and Trace Nested Loop Execution
- A loop becomes infinite when its condition never turns false; one example starts `A` at 10, increments it, and checks whether it is less than or equal to zero.
- Repeatedly subtracting 0.1 from a `double` while testing `J != 0` can fail because floating-point rounding may prevent an exact zero.
- For floating-point loop conditions, prefer range comparisons such as `<=` or `>=` over exact equality checks.
- In nested `while` loops, the inner loop completes before execution continues through the rest of the outer loop body; flowcharts can help reveal logic errors.
27:01
Restart the Guessing Game with Nested Loops and Correct Variable Scope
- An outer `do-while` wraps the entire number-guessing game so the user gets at least one round and can choose to play again.
- The program prompts for a Boolean response with `keyboard.nextBoolean()` and repeats the outer loop when `playAgain` is true.
- Because the `do-while` condition is outside the loop body, `playAgain` must be declared before the loop and reassigned inside it rather than declared there.
- The final demonstration plays multiple rounds, then stops when the user enters false.
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, UofSC_CSCE145_CSCE146.