No history yet

Repetition Basics

Repeating Yourself

Think about your morning routine. You brush your teeth, moving the brush back and forth. You don't think: move right, move left, move right, move left. You just think: brush for two minutes. Your brain automatically repeats the action.

In programming, we often need to repeat actions, too. We might want to print a message ten times, process a list of friends, or count down from 100. Doing this manually would be incredibly tedious. Imagine writing the same line of code over and over again.

System.out.println("This is repetitive.");
System.out.println("This is repetitive.");
System.out.println("This is repetitive.");
System.out.println("This is repetitive.");
System.out.println("This is repetitive.");

That's a lot of extra typing, and it's easy to make a mistake. Programmers have a principle for this: , which stands for Don't Repeat Yourself. The goal is to write clean, efficient code that's easy to read and maintain. Instead of copying and pasting, we use loops.

A loop is a programming structure that repeats a sequence of instructions until a specific condition is met.

It's like telling the computer, "Do this thing, and keep doing it until I tell you to stop."

This saves time, reduces the chance of errors, and makes our programs much more powerful. A loop can run a task ten times or a million times with the same amount of code.

The Anatomy of a Loop

To work correctly, every loop needs three key ingredients. Think of it like running laps around a track. You need to know:

  1. Where to start (the starting line).
  2. When to stop (how many laps to run).
  3. How to get from one lap to the next (running around the track).

A programming loop is no different. It requires a starting point, a stopping condition, and a step to get from the start to the stop.

Let's break these down:

  • Starting Point: This is the initial state before the loop begins. If we're counting to ten, our starting point is probably 1.
  • Stopping Condition: This is the test the computer performs before each repetition. It checks if it should run the loop again or stop. For example, "is the count less than or equal to 10?"
  • The Step: This is the action that happens at the end of each repetition to move us closer to the stopping condition. If we're counting to ten, the step is to add 1 to our current count.

Without all three of these, a loop will either not run at all or, worse, run forever in what's called an —a common bug that can freeze a program.

Code in Blocks

In Java, the code that gets repeated inside a loop is placed inside a code block, which is marked by curly braces {}. Anything you put between these braces is part of the loop's body and will be executed on each pass.

{
  // This is a code block.
  // Any instructions here will be repeated.
  System.out.println("This line is inside the block.");
}

Think of the curly braces as bookends for the instructions you want to repeat. As we move forward, you'll see how these blocks fit into the full syntax of different loop types in Java. For now, just remember that they group your repeated actions together.

Quiz Questions 1/4

The programming principle "DRY" is a major reason for using loops. What does it stand for?

Quiz Questions 2/4

A loop needs a starting point, a stopping condition, and a step. What is the primary role of the 'stopping condition'?