CSCE145 F2026 03 Loops Part 01
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
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
Transcript excerpt
0:01 Hey everyone, how goes it and welcome back to computer science 145. Today, we're going to talk about loops. Today, we're going to talk about loops. Today, we're going to talk about loops. Today, we're In a previous lecture, we discussed flow control and we focused on branching statements. Another element of flow control is called a loop statement and this one repeats an action until a stopping condition occurs. One of the most essential looping statements is called the while loop. Now, in a while loop, if the boolean expression evaluates to true, then the body of the while is executed until it is false. Much like branching statements, it's a
0:47 really good idea to put the curly braces to denote the body of a while loop. Also very similar to branching statements like an if statement, do not put a semicolon after the parenthesis of the while loop. Now, as we are learning how to communicate with the machines, you can think of a while loop as being spoken as while this is true, keep doing that. Now, over to the side here, we have the syntax for a while loop. Notice that it looks very similar to an if statement. Now, while the boolean expression is true, we keep running the body of the while. Once that boolean expression evaluates to false, we jump outside of it and keep
1:35 going on. So, our example right here is if we go ahead and create this integer A and assign it the value of zero. While A is less than 10. So, let's look at this. While zero is less than 10, system.out.println A. So, it prints out zero and then increases by one. So, then A becomes one. One is less than 10. Print out one. Increase it. Two is less than 10. Three
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
In this course
Related notes
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.