CSCE145 F2026 03 Loops Part 02
Overview
Java for loops combine a one-time initialization, a Boolean condition, and an update to express count-controlled repetition; examples show how they correspond to while loops and how nested loops can draw a box. The lesson also compares while, do-while, and for loops, then explains break, continue, and sentinel values through concrete Java examples, including averaging entered values until a negative number stops input.
Key takeaways
- A Java for loop initializes its counter once, checks its condition before each body execution, and updates the counter after the body.
- To print multiples of eight below an input, either test i % 8 == 0 while incrementing by 1 or increment directly by 8; both methods print 0 through 96 for input 100.
- Nested loops map naturally to console output: the outer loop controls box rows, the inner loop prints each row’s stars, and a newline follows each completed row.
- A for-loop counter declared in its header is limited to that loop’s scope, and an inner loop’s counter is reinitialized each time the outer loop runs.
- Use while when the iteration count is unknown, do-while when the body must run at least once, and for when repetition is count-controlled.
- A negative sentinel can terminate input in an otherwise infinite loop; averaging 2, 4, 6, 8, and 10 before the -1 sentinel yields 30 / 5 = 6.
Chapters
- A Java for loop has three semicolon-separated parts: initialization runs once, the Boolean condition is checked before each iteration, and the update runs after the body.
- The execution order is initialization, condition, body, update; subsequent iterations repeat the last three steps until the condition is false.
- A for loop is a count-controlled form of a while loop, and both can express the same repetition logic.
- The octopus-themed Java program reads an integer with Scanner and rejects negative input using a conditional check and System.exit.
- One approach starts i at 0, increments by 1 while i is less than the input, and prints i when i % 8 equals 0.
- A second approach increments i by 8 each iteration, so every value is a multiple of eight and no remainder test is needed.
- For an input of 100, both approaches print the multiples of eight from 0 through 96.
- The box program reads length and width with Scanner and rejects either value when it is less than or equal to zero.
- An outer for loop repeats once per row using length; an inner for loop prints width asterisks with System.out.print.
- System.out.println runs after the inner loop to end each row, producing a box, for example, a 2-by-3 input prints two rows of three stars.
- For a 2-by-3 box, the inner loop’s j takes values 0, 1, and 2 for each outer-loop iteration.
- After j reaches 3, the condition j < width is false; execution leaves the inner loop, prints a newline, then updates i.
- The counting variables declared in the for-loop headers are scoped to their loops and are unavailable after those loops end.
- A while loop may execute zero or more times and suits cases where the iteration count is unknown; a do-while loop always executes at least once.
- A for loop is suited to count-controlled repetition when the number of iterations is known or tracked by a counting variable.
- The break statement exits its current loop immediately; in nested loops, it exits only the loop containing it.
- The continue statement skips the rest of the current iteration; the example increments A from 0 to 10 and prints only odd values, 1, 3, 5, 7, and 9.
- A sentinel is a value chosen to be distinct from valid inputs and signal that a loop should stop.
- The averaging example uses while (true) to read values, then breaks when an entered value is negative.
- For inputs 2, 4, 6, 8, and 10 followed by -1, the program accumulates sum = 30 and count = 5, producing an average of 6.
Transcript excerpt
0:00 Hey everyone, how goes it and welcome back to computer science 145. Today, we're going to talk about loops. We're going to talk about loops. We're I'm not going to do that again. In a previous lecture, we discussed looping statements such as a while loop and a do-while loop. There is another very common kind of loop that's called a for loop. For loops can be considered a counting loop and this can also be considered a special type of a while loop. For loops look a little different from any other statement that we've encountered because the arguments, in other words, the thing that's inside the parenthesis has three separate parts. The first part can be called the init
0:48 and this is the initialization of a counting variable and this statement runs one time only. The next part is called the boolean expression and this is just like the boolean expressions that we've seen in while loops or even if statements. Finally, the last argument is the update and this updates the counting variable after all of the other statements in the body have been executed. Each different part of the arguments are separated using a semicolon. As we can see right here, this is the initialization, this is the boolean expression and here's the update, all separated by a semicolon. Now, just like everything before, I
1:38 strongly encourage to use the curly braces in order to denote the body of the for loop. And once again, do not put a semicolon after the parenthesis right here. Now, as we are continuing to learn how to speak to the machines, what you can think of is a for loop is saying do that for this many times. Taking a look at the flowchart for a for loop, it looks like this.
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.