Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Loops and Arrays | Applying Loops in Real Problems
Java Loops

bookLoops and Arrays

A Powerful Combination

Arrays let you store a collection of values in a single variable, making it easy to manage lists of data such as scores, names, or sensor readings. When you need to process every value in an array—like summing numbers or searching for a match—loops become essential. Instead of writing repetitive code for each element, you can use a loop to automate the task, saving time and reducing errors.

Using loops with arrays helps you handle large datasets efficiently and write cleaner, more flexible code.

Practical Example – Iterating an Array

You often need to process every item in a list, such as finding the total of expenses or displaying all entries in a contact list. In these cases, you iterate through an array to perform an action with each element.

Task:

  • Imagine you have an array of daily sales amounts for a week;
  • Your goal is to calculate the total sales for the week by iterating over the array and summing each value;
  • Print the total sales amount to the console.
Main.java

Main.java

copy
1234567891011121314151617
package com.example; public class Main { public static void main(String[] args) { // Array of daily sales for a week int[] dailySales = {120, 150, 100, 170, 200, 130, 180}; int totalSales = 0; // Loop through the array to calculate total sales for (int i = 0; i < dailySales.length; i++) { totalSales += dailySales[i]; } System.out.println("Total sales for the week: " + totalSales); } }
  1. We create an array dailySales that stores the sales for each day of the week;
  2. We initialize a variable totalSales to 0 to keep track of the sum;
  3. We use a for loop to iterate over each element of the array:
    • The loop index i starts at 0 and goes up to dailySales.length;
    • At each iteration, we add dailySales[i] to totalSales.
  4. After the loop finishes, we print the total sales for the week using System.out.println.

Arrays allow you to store multiple values of the same type in a single variable. Loops make it easy to iterate over these arrays to read or modify elements efficiently. By combining loops with arrays, you can perform tasks like calculating totals or updating values without writing repetitive code.

question mark

What is the purpose of using a loop when working with arrays in Java

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 3. Kapitel 1

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

Suggested prompts:

Can you show me an example of how to sum the sales using a loop?

What programming language is this example written in?

Can you explain how the loop works in more detail?

Awesome!

Completion rate improved to 9.09

bookLoops and Arrays

Swipe um das Menü anzuzeigen

A Powerful Combination

Arrays let you store a collection of values in a single variable, making it easy to manage lists of data such as scores, names, or sensor readings. When you need to process every value in an array—like summing numbers or searching for a match—loops become essential. Instead of writing repetitive code for each element, you can use a loop to automate the task, saving time and reducing errors.

Using loops with arrays helps you handle large datasets efficiently and write cleaner, more flexible code.

Practical Example – Iterating an Array

You often need to process every item in a list, such as finding the total of expenses or displaying all entries in a contact list. In these cases, you iterate through an array to perform an action with each element.

Task:

  • Imagine you have an array of daily sales amounts for a week;
  • Your goal is to calculate the total sales for the week by iterating over the array and summing each value;
  • Print the total sales amount to the console.
Main.java

Main.java

copy
1234567891011121314151617
package com.example; public class Main { public static void main(String[] args) { // Array of daily sales for a week int[] dailySales = {120, 150, 100, 170, 200, 130, 180}; int totalSales = 0; // Loop through the array to calculate total sales for (int i = 0; i < dailySales.length; i++) { totalSales += dailySales[i]; } System.out.println("Total sales for the week: " + totalSales); } }
  1. We create an array dailySales that stores the sales for each day of the week;
  2. We initialize a variable totalSales to 0 to keep track of the sum;
  3. We use a for loop to iterate over each element of the array:
    • The loop index i starts at 0 and goes up to dailySales.length;
    • At each iteration, we add dailySales[i] to totalSales.
  4. After the loop finishes, we print the total sales for the week using System.out.println.

Arrays allow you to store multiple values of the same type in a single variable. Loops make it easy to iterate over these arrays to read or modify elements efficiently. By combining loops with arrays, you can perform tasks like calculating totals or updating values without writing repetitive code.

question mark

What is the purpose of using a loop when working with arrays in Java

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 3. Kapitel 1
some-alt