What is a loop?
A construct that controls repeated executions of a block of statements
What are the three types of loop statements in Java?
while loops, do-while loops, and for loops
What is the syntax of a while loop?
while (loop-continuation-condition) { Statement(s); }
What is the loop body?
The part of the loop that contains the statements to be repeated
What is an iteration?
A one-time execution of a loop body (also called repetition)
What is a loop-continuation-condition?
A Boolean expression that controls the execution of the loop body
When does a while loop terminate?
When the loop-continuation-condition evaluates to false
What is a counter-controlled loop?
A loop where the control variable is used to count the number of iterations
What happens if you don’t increment the counter in a while loop?
The loop becomes infinite because the condition never becomes false
What is an infinite loop?
A loop that runs forever because the loop-continuation-condition never becomes false
How do you stop an infinite loop from command line?
Press CTRL+C
What is an off-by-one error?
A common mistake where the loop executes one more or less time than intended
What would happen with count <= 100 instead of count < 100?
The loop would execute 101 times instead of 100 times (off-by-one error)
In the RepeatAdditionQuiz example, what does Math.random() * 10 do?
Generates a random number from 0.0 to 9.999…
What does (int)(Math.random() * 10) do?
Generates a random integer from 0 to 9
What are the 3 steps for loop design strategy?
1) Identify statements to repeat, 2) Wrap in while(true) loop, 3) Add loop-continuation-condition
What is user confirmation in loops?
Using input from user to decide whether to continue the loop
What is a sentinel value?
A special input value that signifies the end of input
What is a sentinel-controlled loop?
A loop that uses a sentinel value to control its execution
Why shouldn’t you use floating-point values for loop control?
Floating-point arithmetic is approximated and may never reach exact values, causing infinite loops
What does input redirection do?
Takes input from a file rather than keyboard using < filename
What does output redirection do?
Sends output to a file rather than console using > filename
What does input.hasNext() do?
Returns true if there is more input available, false otherwise
How do you end input from keyboard?
Press ENTER, then CTRL+Z, then ENTER again