Unit 4: Iteration

Delving Deep into Java Loops

Introduction to Loops in Java

In the world of programming, repetitive tasks are omnipresent. Imagine a scenario where you need to print numbers from 1 to 100. Would you write 100 print statements? Or, would you rather choose an elegant solution that uses just a few lines of code? That's where loops come in. In Java, loops provide a means to execute a block of code repeatedly based on a condition or a sequence. In this lesson, we'll introduce you to the different types of loops in Java: while, for, and do-while.


Java Looping Constructs

While Loop

The while loop evaluates a condition and if it's true, executes a block of code. This process continues until the condition becomes false.

int i = 1;
while(i <= 10) {
    System.out.println(i);
    i++;
}

bold note: Always ensure there's a way out of the loop, otherwise, it'll become an infinite loop.

For Loop

The for loop provides a concise way to iterate. It has three parts: initialization, condition, and update.

for(int i = 1; i <= 10; i++) {
    System.out.println(i);
}

Tip

The for loop is best used when the number of iterations is known beforehand.

Do-While Loop

The do-while loop is similar to the while loop but checks the condition after executing the block of code. This ensures that the loop body runs at least once.

int i = 1;
do {
    System.out.println(i);
    i++;
} while(i <= 10);

Watch Out!

Be cautious with loop conditions. Incorrect conditions can lead to infinite loops or loops that never execute.


Common Loop Patterns in Java

Count-Controlled Loops

Often used with for loops, these loops run a specific number of times.

for(int count = 1; count <= 5; count++) {
    System.out.println("Iteration: " + count);
}

Sentinel-Controlled Loops

Used with while or do-while loops, these loops run until a sentinel value is encountered.

Scanner scanner = new Scanner(System.in);
String input;
do {
    System.out.println("Enter 'exit' to quit");
    input = scanner.nextLine();
} while(!input.equals("exit"));

Summary

Java provides us with powerful looping constructs like while, for, and do-while to handle repetitive tasks efficiently. While for loops are ideal for known iteration counts, while and do-while offer flexibility when the number of iterations isn't predetermined. Always be cautious with loop conditions to avoid unintended infinite loops.


References


AP CSA Homework Assignment

Assignment: Exploring Java Loops

Instructions

  1. Create a program that uses a for loop to print numbers from 1 to 20.
  2. Develop a program that prompts the user for an integer and then uses a while loop to print all even numbers up to that integer.
  3. Write a program that uses a do-while loop to keep asking the user for their favorite color until they enter "blue". Display a congratulatory message once they do.

Remember to always test your code to ensure it works as expected. Make use of comments to explain the purpose and logic behind each loop in your programs.

Previous
Switch Statements