Selection & Iteration
Algorithms
Master the building blocks of programming: conditional logic and loop structures that power every Java application.
What Is an Algorithm?
An algorithm is a finite, step-by-step procedure for solving a problem or performing a computation. In Java, algorithms are built from three fundamental control structures: sequence, selection, and iteration.
Selection: Branching Logic
Selection statements let your program make decisions. Java evaluates a boolean expression and executes different code blocks depending on whether the result is true or false.
Iteration: Loop Structures
Iteration repeats a block of code while a condition holds true. Java provides three main loop constructs, each suited to different situations.
The while Loop Pattern
The for Loop Pattern
int i = 0 : executed once before the loop starts.i++ : executed after the loop body each time.Core Rule: Nesting Selection Inside Iteration
The real power emerges when you combine selection and iteration. A loop traverses data, and an if statement inside the loop filters, counts, or transforms each element conditionally.
while vs for: When to Use Each
| while loop | for loop | |
|---|---|---|
| Iterations known? | Not necessarily | Usually known |
| Init / Update | Separate statements | Built into header |
| Use case | Sentinel values, user input | Array traversal, counting |
| Off-by-one risk | Higher (manual update) | Lower (compact header) |
| Infinite loop risk | Higher (forget update) | Lower |
How to Implement an Algorithm
Example 1: Find the Maximum
Given an array of integers, find and return the largest value.
Example 2: The Accumulator Pattern
Compute the average of all even numbers in an array.
Example 3: Nested Iteration
Check if an array contains any duplicate values using nested loops.
Example 4: String Traversal
Count the number of vowels in a given String.
Sentinel-Controlled Loops
A sentinel value is a special input that signals the loop to stop. This pattern uses a while loop because the number of iterations is unknown in advance.
Common Mistakes
while loop causes it to run forever.arr.length causes an ArrayIndexOutOfBoundsException.if statement outside the loop when it should be inside, causing it to check only the last element..equals() instead.num = 0) won't change the original array. For objects, you can still modify the object's state via its methods.Algorithm Patterns Summary
| Pattern | Loop Type | Selection | Example |
|---|---|---|---|
| Find max/min | for | if (comparison) | Largest value |
| Count/filter | for-each | if (test) | Count evens |
| Accumulate | for-each | if (qualify) | Sum positives |
| Search | for / while | if (match) | Find index |
| Sentinel input | while | if (validate) | Read until -1 |
| Duplicate check | nested for | if (\(==\)) | Any repeats? |
Question 1 of 5
Consider the following code segment:
What is printed when this code segment is executed?
Question 2 of 5
Which loop type is most appropriate when the number of iterations is not known before the loop begins?
Question 3 of 5
Consider the following code segment:
What value is printed?
Question 4 of 5
Consider the following code segment:
What value of count is printed?
Question 5 of 5
What is the main risk of placing the loop variable update inside an if statement within a while loop?
i++) is inside an if block, and the condition is never true, the loop variable never changes and the while condition stays true forever, creating an infinite loop.