Skip to Content
Course content
Click on the "Edit" button in the top corner of the screen to edit your slide content.

A few years ago, I was reviewing a pull request from a junior developer who was tasked with calculating the total value of a shopping cart. When I opened the file, I found something that made me chuckle: he had written cartTotal += item[0]; cartTotal += item[1]; and so on, all the way down to index 15. It worked, but it was fragile. The moment a customer added a 16th item to their cart, the code ignored it. When I asked him how he'd handle a cart with 1,000 items, he realized he'd spent half his afternoon writing code that a three-line loop could have handled. That's the core of why we use for loops—not just to save typing, but to write code that scales regardless of the input size.

The Mechanics of the Iteration

At its heart, a for loop is just a way to tell Java, "Do this specific block of code a certain number of times, and keep track of where we are." You'll see the syntax usually broken into three parts inside the parentheses: the initialization, the condition, and the update expression. I like to think of it as a contract. You define where you start (usually int i = 0), the rule that keeps the loop running (like i < 10), and how you move to the next step (i++).

for (int i = 0; i < 5; i++) {
    System.out.println("Processing record number: " + i);
}

In this example, i is our counter. It starts at 0, and as long as it's less than 5, the code inside the curly braces runs. After each run, i++ bumps the number up by one. Once i hits 5, the condition 5 < 5 becomes false, and Java jumps straight to the code following the loop. It's clean, predictable, and prevents that manual copy-paste nightmare I mentioned earlier.

Walking Through Data Arrays

While counting from 0 to 10 is useful, you'll most often use for loops to traverse arrays or lists. Instead of hardcoding a number like 5 or 10, we use the .length property of the array. This is where the loop becomes powerful; your code now works whether the array has two elements or two million.

Let's say you're building a simple system to apply a tax to a list of product prices. You wouldn't want to manually call every index. Instead, you'd do something like this:

double[] prices = {12.99, 25.00, 9.95, 100.00, 4.50};
double taxRate = 0.07;

for (int i = 0; i < prices.length; i++) {
    double taxAmount = prices[i] * taxRate;
    System.out.println("Tax for item " + i + " is: " + taxAmount);
}

Notice how I'm using prices[i]. As i increments, we are essentially sliding a window across the array, grabbing one value at a time. One quick tip: always be careful with your comparison operator. Using <= prices.length is a classic mistake that will crash your program with an ArrayIndexOutOfBoundsException because Java arrays are zero-indexed. If an array has 5 elements, the last index is 4, not 5.




📋 Practical Task

Exercise: Weekly Step Tracker Analysis

You are building a fitness app. You have an array containing the number of steps a user took each day for one week. Your task is to write a program that uses a for loop to accomplish two things: calculate the total number of steps for the week and identify the day with the highest step count.

Requirements:

  • Create an integer array named weeklySteps with the following values: {8500, 12000, 7000, 11000, 15000, 9000, 10000}.
  • Use a for loop to sum all the values in the array into a variable called totalSteps.
  • Inside the same loop (or a second one), track the highest value found in the array and store it in a variable called maxSteps.
  • Print both the totalSteps and the maxSteps to the console.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.