Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære for Loop in Java | Getting Started with Loops
Java Loops

bookfor Loop in Java

The for loop is one of the most commonly used looping constructs in Java. It allows you to repeat a block of code a specific number of times, making it ideal for tasks like processing elements in a sequence, repeating actions with a known count, or generating patterns. You will often use a for loop when you know in advance how many times you want to execute a statement or a block of statements.

A for loop in Java consists of three main parts: initialization, condition, and increment. The general structure looks like this:

for (initialization; condition; increment) {
    // Code to execute in each iteration
}
  • Initialization: this step is executed once, before the loop starts. It is typically used to declare and set up a loop control variable, such as int i = 1;
  • Condition: before each iteration, the condition is checked. If it evaluates to true, the loop body executes. If it is false, the loop stops. For example, i <= 5 keeps the loop running as long as i is less than or equal to 5;
  • Increment: after each iteration of the loop body, the increment statement runs. It usually updates the loop control variable, such as i++ to increase i by 1.

Putting these together, the for loop in the example starts by setting i to 1, checks if i is less than or equal to 5, prints the value, and then increases i by 1. This process repeats until the condition is no longer true.

Example

Imagine you want to print numbers from 1 to 5 in Java. Instead of writing System.out.println(1), System.out.println(2), and so on, you can use a for loop to do it automatically.

Main.java

Main.java

copy
12345678910
package com.example; public class Main { public static void main(String[] args) { // Print numbers from 1 to 5 using a for loop for (int i = 1; i <= 5; i++) { System.out.println(i); } } }

This Java program prints numbers from 1 to 5 using a for loop. The loop starts at 1, checks the condition i <= 5, and increments i by 1 after each iteration, printing each number in the console.

question mark

Which of the following correctly describes the order of execution in a Java for loop?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 2

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

Suggested prompts:

Can you show me the complete code for printing numbers from 1 to 5 using a for loop in Java?

Can you explain what happens if I change the condition or increment in the for loop?

Can you give another example of using a for loop in Java?

Awesome!

Completion rate improved to 9.09

bookfor Loop in Java

Stryg for at vise menuen

The for loop is one of the most commonly used looping constructs in Java. It allows you to repeat a block of code a specific number of times, making it ideal for tasks like processing elements in a sequence, repeating actions with a known count, or generating patterns. You will often use a for loop when you know in advance how many times you want to execute a statement or a block of statements.

A for loop in Java consists of three main parts: initialization, condition, and increment. The general structure looks like this:

for (initialization; condition; increment) {
    // Code to execute in each iteration
}
  • Initialization: this step is executed once, before the loop starts. It is typically used to declare and set up a loop control variable, such as int i = 1;
  • Condition: before each iteration, the condition is checked. If it evaluates to true, the loop body executes. If it is false, the loop stops. For example, i <= 5 keeps the loop running as long as i is less than or equal to 5;
  • Increment: after each iteration of the loop body, the increment statement runs. It usually updates the loop control variable, such as i++ to increase i by 1.

Putting these together, the for loop in the example starts by setting i to 1, checks if i is less than or equal to 5, prints the value, and then increases i by 1. This process repeats until the condition is no longer true.

Example

Imagine you want to print numbers from 1 to 5 in Java. Instead of writing System.out.println(1), System.out.println(2), and so on, you can use a for loop to do it automatically.

Main.java

Main.java

copy
12345678910
package com.example; public class Main { public static void main(String[] args) { // Print numbers from 1 to 5 using a for loop for (int i = 1; i <= 5; i++) { System.out.println(i); } } }

This Java program prints numbers from 1 to 5 using a for loop. The loop starts at 1, checks the condition i <= 5, and increments i by 1 after each iteration, printing each number in the console.

question mark

Which of the following correctly describes the order of execution in a Java for loop?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 2
some-alt