# FOR

## for

> ### for (초기화; 조건식; 증감식) {&#x20;
>
> ### &#x20;         조건식의 결과가 <mark style="color:blue;">참</mark>인 동안 반복적으로 실행하고자 하는 문장;
>
> ### }
>
> #### 실행 순서
>
> 1. 초기화
> 2. 조건식
> 3. 조건식 이 <mark style="color:blue;">참일 경우 문장 수행</mark>
> 4. 증감식
> 5. <mark style="color:blue;">조건식이 거짓</mark>이 될 때 까지 반복

* 아래 예제코드로 학습해 보겠습니다.

{% code lineNumbers="true" %}

```java
class Control3_1 {
    public static void main(String[] args) {
        int i = 0;
        for (i = 0; i < 5; i++) {
            System.out.println("for 문이 " + (i + 1) + "번째 반복 실행중입니다.");
        }
        System.out.println("for 문이 종료된 후 변수 i의 값은 " + i + "입니다.");
    }
}
```

{% endcode %}

{% code lineNumbers="true" %}

```java
class Control3_2 {
    public static void main(String[] args) {
        // 1번
        for (int i = 1; i <= 10; i = i * 2) {
            System.out.println("1번 i는 현재 " + (i) + "입니다.");
        }
        System.out.println();
        // 2번
        for (int i = 10; i >= 1; i--) {
            System.out.println("2번 i는 현재 " + (i) + "입니다.");
        }
    }
}
```

{% endcode %}

{% code lineNumbers="true" %}

```java
class Control3_3 {
    public static void main(String[] args) {
        // 초기화 시 변수 2개 사용 가능합니다. 단, 타입이 같아야 한다.
        for (int i = 1, j = 10; i <= 10; i++, j--) {
            System.out.println("i는 현재 " + (i) + "입니다.");
            System.out.println("j는 현재 " + (j) + "입니다.");
        }
        System.out.println();
        // 이렇게 변수 2개를 사용하여 조건식을 구성할 수 있습니다.
        for (int k = 1, t = 10; k <= 10 && t > 2; k++, t--) {
            System.out.println("k는 현재 " + (k) + "입니다.");
            System.out.println("t는 현재 " + (t) + "입니다.");
        }
    }
}
```

{% endcode %}

## 중첩 for

> ### for (초기화; 조건식1; 증감식) {&#x20;
>
> ### &#x20;         <mark style="color:blue;">조건식1의 결과</mark>가 <mark style="color:blue;">참</mark>인 동안 반복적으로 실행하고자 하는 문장;
>
> ### &#x20;         for (초기화; 조건식2; 증감식) {&#x20;
>
> ### &#x20;                      <mark style="color:blue;">조건식2의 결과</mark>가 <mark style="color:blue;">참</mark>인 동안 반복적으로 실행하고자 하는 문장;
>
> ### &#x20;          }
>
> ### }

* 아래 예제코드로 학습해 보겠습니다.

{% code lineNumbers="true" %}

```java
class Control3_4 {
    public static void main(String[] args) {
        for (int i = 2; i < 10; i++) {
            System.out.println(i + "단 시작합니다.");
            for (int j = 1; j < 10; j++) {
                System.out.println("j는 현재 " + (j) + "입니다.");
                System.out.println(i + "*" + j + "=" + (i * j));
            }
        }
    }
}
```

{% endcode %}

## 향상된 for

> ### for (타입 변수이름 : 배열 or 컬렉션) {&#x20;
>
> ### &#x20;         배열 or 컬렉션의 길이만큼 반복적으로 실행하고자 하는 문장;
>
> ### }

* 배열을 배우고 난 후에 학습 하셔도 좋습니다!

* 아래 예제코드로 간단하게 학습해 보겠습니다.

{% code lineNumbers="true" %}

```java
class Control3_5 {
    public static void main(String[] args) {
        int[] arr = new int[]{1, 2, 3, 4, 5};

        for (int e : arr) {
            System.out.print(e + " ");
        }
    }
}
```

{% endcode %}

{% hint style="info" %}
Ref. Java의 정석 기초편 Chapter4(13, 14, 15)

Ref. [for](https://www.youtube.com/watch?v=6UjmGzjynQw\&list=PLW2UjW795-f6xWA2_MUhEVgPauhGl3xIp\&index=36), [중첩 for](https://www.youtube.com/watch?v=9wL0NdLXqz0\&list=PLW2UjW795-f6xWA2_MUhEVgPauhGl3xIp\&index=37)
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://nbcamp.gitbook.io/java-handbook/part-03./for.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
