---
title: How to use For Loop in Java | 100% best for beginners
url: https://kensoftph.com/for-loop-in-java/
date: 2021-03-28T04:27:00+00:00
modified: 2022-09-21T10:02:00+00:00
lang: en_US
---

# How to use For Loop in Java | 100% best for beginners

## How to use For Loop in Java

[**For Loop** in Java](https://www.tutorialspoint.com/java/java_for_loop.htm) is easy as 1 2 3. When you want your code to be executed dynamically instead of doing it manually, use for loop.

## Syntax

```
for (statement 1; statement 2; statement 3) {
//code block
}
```

**Statement 1:** Sets a variable with a value. The loop start from here.

**Statement 2:** This is where you set your condition. Define the condition to run the loop. If the condition is true, It will keep running until it is false.

**Statement 3:** Increases a value every time until it reaches the end value of the condition or loop.

## Example

When you are printing numbers from 1 - 5. Do not code like this. See this example code below.

```
System.out.println("1");
System.out.println("2");
System.out.println("3");
System.out.println("4");
System.out.println("5");
```

## Output

```
1
2
3
4
5
```

Although, the output is the same as expected. But it seems like not a professional one. Your code should be like this.

```
for (int i = 1; i <= 5; i++) {
    System.out.println(i);
}
```

## Output

```
1
2
3
4
5
```

## [![For loop in Java](https://kensoftph.com/wp-content/uploads/2021/03/for-loop-in-java.png)](https://kensoftph.com/wp-content/uploads/2021/03/for-loop-in-java.png)

## YouTube Video

https://youtu.be/GhQdQmx1zVY
