Glossary
Body (Loop)
The block of code enclosed within the loop's curly braces that is executed repeatedly during each iteration.
Example:
In a loop that calculates a sum, sum += num; would be part of the loop body, executed for each number.
Condition (Loop)
The boolean expression that is evaluated before each iteration of a loop; the loop continues to execute as long as this condition is true.
Example:
In while (score < 100), score < 100 is the condition that determines if the game continues.
For Loop
A control flow statement that provides a concise way to write a loop that is executed a specific number of times, combining initialization, condition, and iteration into a single line.
Example:
To print numbers from 1 to 10, a for loop is ideal: for (int i = 1; i <= 10; i++) { System.out.println(i); }.
Infinite Loop
A loop that continues to execute indefinitely because its termination condition is never met, often due to a missing or incorrect modifier.
Example:
If you write while (true) { System.out.println("Help!"); }, you've created an infinite loop that will print "Help!" forever.
Initialization (Loop)
The step in a loop where a control variable is declared and assigned an initial value, executed only once before the loop begins.
Example:
In for (int i = 0; i < 10; i++), int i = 0 is the initialization step, setting the starting point for the loop counter.
Iteration
The process of executing a block of code repeatedly, typically using loops, forming the backbone of complex algorithms.
Example:
When a game needs to update the position of every enemy on the screen, it uses iteration to process each enemy one by one.
Modifier (Loop)
The statement within a loop (or part of the for loop header) that changes the loop control variable, ensuring the condition eventually becomes false and the loop terminates.
Example:
In i++ within a for or while loop, i++ is the modifier that increments the counter, moving towards the loop's end.
Nested Loop
A loop structure where one loop is placed inside another, causing the inner loop to complete all its iterations for each single iteration of the outer loop.
Example:
When printing a multiplication table, you'd use a nested loop: the outer loop for rows (multiplicands) and the inner loop for columns (multipliers).
Off-by-one Errors
A common programming mistake where a loop iterates one too many or one too few times, often due to incorrect boundary conditions (e.g., using `<` instead of `<=`).
Example:
If a loop is supposed to process elements from index 0 to length-1 but uses i <= length, it might cause an off-by-one error by trying to access an invalid index.
String Algorithms
Specific computational procedures or sets of rules applied to strings, often involving loops for tasks like reversing, searching for substrings, or counting characters.
Example:
A common string algorithm is checking if a word is a palindrome by comparing characters from the beginning and end.
String Traversal
The process of iterating through each character of a string, typically using a loop and string methods like `.charAt()` or `.substring()`.
Example:
To check if a password contains any digits, you would perform a string traversal, examining each character one by one.
Tracing (Loops)
The systematic, step-by-step manual execution of a loop's code, tracking variable values and output to understand its behavior and predict its results.
Example:
To debug why a loop isn't producing the correct sum, you would perform tracing, writing down the value of the sum variable after each iteration.
While Loop
A control flow statement that repeatedly executes a block of code as long as a specified boolean condition remains true, checking the condition before each execution.
Example:
To simulate a countdown, you might use a while loop like while (count > 0) { System.out.println(count--); }.