Working with Arrays and Loops in Java
A Simple Thought
Imagine a teacher who wants to find the total and average marks of her students. Instead of adding each mark by hand, she can let the computer do the math.
You already know that arrays can store multiple values, and loops can go through them one by one. Now, you will learn how to combine both ideas to make your programs calculate results automatically.
In this lesson, you will build a simple program that takes a list of marks, adds them all up, and finds the average.
The Project: Student Marks Analyzer
You will create a program that stores the marks of five students in an array. It will calculate the total and average marks using a loop.
Here’s how your output should look:
1 2 3 4 5 6======================== STUDENT MARKS Marks: 78, 85, 90, 66, 81 Total Marks: 400 Average Marks: 80 ========================
The computer will go through each mark, add them up, and calculate the average automatically.
The Concept: Loops and Arrays Together
When you use arrays, you often want to do something with every value in the list.
That is where loops come in. They help you go through the array step by step.
Here’s how it looks in Java:
1 2 3 4 5 6int[] marks = {78, 85, 90, 66, 81}; int total = 0; for (int i = 0; i < marks.length; i++) { total = total + marks[i]; }
The loop runs once for each mark, adding each value to the total.
After the loop finishes, you can find the average by dividing the total by the number of marks.
Why This Matters
Almost every real-world program that deals with data needs to summarize or analyze it.
Think about it:
- A shopping app adds all item prices to find the total bill.
- A fitness tracker averages your steps or calories over a week.
- A game adds up your points at the end of each round.
Loops and arrays together give you the power to handle long lists of information quickly and accurately.
Let’s Build the Marks Analyzer
Here’s the full working program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27public class StudentMarks { public static void main(String[] args) { int[] marks = {78, 85, 90, 66, 81}; int total = 0; for (int i = 0; i < marks.length; i++) { total = total + marks[i]; } int average = total / marks.length; System.out.println("========================"); System.out.println(" STUDENT MARKS"); System.out.print(" Marks: "); for (int i = 0; i < marks.length; i++) { System.out.print(marks[i]); if (i < marks.length - 1) { System.out.print(", "); } } System.out.println(); System.out.println(" Total Marks: " + total); System.out.println(" Average Marks: " + average); System.out.println("========================"); } }
When you run it, Java will calculate the total and average automatically.
If you change or add marks, the loop will still handle everything correctly without any extra edits.
How It Works
- The array marks stores the list of scores.
- A variable called total starts at zero.
- The loop adds each mark to the total, one by one.
- When the loop ends, total contains the sum of all marks.
- The program divides the total by the number of marks to find the average.
- Finally, everything is printed in a clean format.
This combination of arrays and loops makes your code both short and powerful.
Learn Together with AI
If you are using an AI Copilot, you can easily ask it to build or modify this kind of program for you.
Try this prompt:
“Write a Java program that stores five student marks in an array and calculates the total and average using a loop.”
Then ask it for changes like:
- “Show the highest and lowest mark as well.”
- “Add one more subject and recalculate.”
- “Display a message if the average is above 80.”
The AI will instantly adjust your code, showing how arrays and loops can be extended to handle more logic.
Practice Challenge
Create a program that calculates the total and average price of five products in a store.
The output should look like this:
1 2 3 4 5 6======================== STORE INVENTORY Prices: 250, 400, 120, 300, 280 Total Price: 1350 Average Price: 270 ========================
Use an array to store the prices and a loop to calculate the total and average.
Example Solution
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27public class StoreInventory { public static void main(String[] args) { int[] prices = {250, 400, 120, 300, 280}; int total = 0; for (int i = 0; i < prices.length; i++) { total = total + prices[i]; } int average = total / prices.length; System.out.println("========================"); System.out.println(" STORE INVENTORY"); System.out.print(" Prices: "); for (int i = 0; i < prices.length; i++) { System.out.print(prices[i]); if (i < prices.length - 1) { System.out.print(", "); } } System.out.println(); System.out.println(" Total Price: " + total); System.out.println(" Average Price: " + average); System.out.println("========================"); } }
Run the program and try changing the prices to see how the total and average update automatically.
This is how programmers make computers do calculations efficiently with just a few lines of code.
Coming Up Next
You’ve learned how to store data and calculate totals using arrays and loops.
Next, you will explore a more advanced type of array called a two-dimensional array. It allows you to store data in rows and columns, similar to a table.