Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn For-Loop | Loops
Java Basics

bookFor-Loop

for Loop

The for loop is a control flow statement that allows you to repeatedly execute a block of code for a specified number of times. It is commonly used when you know the exact number of iterations or when iterating over a collection or array.

The syntax in for loop in Java is as follows:

Main.java

Main.java

copy
123
for (initialization; condition; update) { // code to be executed }

Step by step, you first initialize a variable in a special section of the loop (unlike the while loop, where it’s done outside). Then we define the condition for the loop to run, such as while the variable is less than 10. Finally, we apply an increment or decrement. Below is a flowchart and an explanation of each loop block.

Here's the breakdown of each part of the for loop:

  • Initialization: this is the initial setup executed only once at the beginning of the loop. Typically, you declare and initialize a loop control variable here. For example, int i = 0;
  • Condition: this is the condition checked before each iteration. If the condition is true, the loop body is executed. If the condition is false, the loop terminates. For example, i < 10;
  • Increment/decrement expression: this is the code executed after each iteration. Typically, you update the loop control variable here. For example, i++ (which is equivalent to i = i + 1);
  • Code inside loop: this is the block of code executed for each iteration of the loop. You can put any valid Java code inside the loop body.

Here's an example that demonstrates the usage of a for loop:

Main.java

Main.java

copy
123456789
package com.example; public class Main { public static void main(String[] args) { for (int i = 0; i < 10; i++) { System.out.println("Iteration: " + i); } } }

In this example, the loop will execute 10 times. It starts with i initialized to 0, checks if i is less than 10, executes the loop body, and then updates i by incrementing it by 1. This process repeats until the condition becomes false.

Let's look at another example where we need to display only even numbers in the range from 1 to 30.

To determine whether a number is even or not, we will use the % operator. The % operator in Java is used to get the remainder of a division. It helps check if a number divides evenly by another, which is useful for determining if a number is even.

main.java

main.java

copy
1234567891011
package com.example; public class Main { public static void main(String[] args) { for (int i = 0; i < 30; i++) { if (i % 2 == 0) { // Check if `i` is even System.out.println(i); } } } }

In the code above, we use the variable i to check the condition, where i represents the current number. We then determine whether i is even using the modulo operator (%). If i % 2 == 0, the number is even because it is divisible by 2 without a remainder.

For example, dividing 3 by 2 leaves a remainder of 1, so 3 % 2 equals 1, and the condition is false, meaning 3 is an odd number.

Also note how the condition for i is set: the logic stays the same, but the loop is limited to values up to 30, as required by the task.

1. How many iterations will be there?

2. How many times i will be displayed

question mark

How many iterations will be there?

Select the correct answer

question mark

How many times i will be displayed

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 5

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you show me a sample code for a for loop in Java?

How does the for loop differ from the while loop in Java?

Can you explain how the modulo operator works in more detail?

bookFor-Loop

Swipe to show menu

for Loop

The for loop is a control flow statement that allows you to repeatedly execute a block of code for a specified number of times. It is commonly used when you know the exact number of iterations or when iterating over a collection or array.

The syntax in for loop in Java is as follows:

Main.java

Main.java

copy
123
for (initialization; condition; update) { // code to be executed }

Step by step, you first initialize a variable in a special section of the loop (unlike the while loop, where it’s done outside). Then we define the condition for the loop to run, such as while the variable is less than 10. Finally, we apply an increment or decrement. Below is a flowchart and an explanation of each loop block.

Here's the breakdown of each part of the for loop:

  • Initialization: this is the initial setup executed only once at the beginning of the loop. Typically, you declare and initialize a loop control variable here. For example, int i = 0;
  • Condition: this is the condition checked before each iteration. If the condition is true, the loop body is executed. If the condition is false, the loop terminates. For example, i < 10;
  • Increment/decrement expression: this is the code executed after each iteration. Typically, you update the loop control variable here. For example, i++ (which is equivalent to i = i + 1);
  • Code inside loop: this is the block of code executed for each iteration of the loop. You can put any valid Java code inside the loop body.

Here's an example that demonstrates the usage of a for loop:

Main.java

Main.java

copy
123456789
package com.example; public class Main { public static void main(String[] args) { for (int i = 0; i < 10; i++) { System.out.println("Iteration: " + i); } } }

In this example, the loop will execute 10 times. It starts with i initialized to 0, checks if i is less than 10, executes the loop body, and then updates i by incrementing it by 1. This process repeats until the condition becomes false.

Let's look at another example where we need to display only even numbers in the range from 1 to 30.

To determine whether a number is even or not, we will use the % operator. The % operator in Java is used to get the remainder of a division. It helps check if a number divides evenly by another, which is useful for determining if a number is even.

main.java

main.java

copy
1234567891011
package com.example; public class Main { public static void main(String[] args) { for (int i = 0; i < 30; i++) { if (i % 2 == 0) { // Check if `i` is even System.out.println(i); } } } }

In the code above, we use the variable i to check the condition, where i represents the current number. We then determine whether i is even using the modulo operator (%). If i % 2 == 0, the number is even because it is divisible by 2 without a remainder.

For example, dividing 3 by 2 leaves a remainder of 1, so 3 % 2 equals 1, and the condition is false, meaning 3 is an odd number.

Also note how the condition for i is set: the logic stays the same, but the loop is limited to values up to 30, as required by the task.

1. How many iterations will be there?

2. How many times i will be displayed

question mark

How many iterations will be there?

Select the correct answer

question mark

How many times i will be displayed

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 5
some-alt