# break 와 continue

## break

> ### 자신이 포함된 하나의 반복문을 벗어납니다.

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

{% code lineNumbers="true" %}

```java
class Control6_1 {
    public static void main(String[] args) {
        int sum = 0;
        int i = 0;

        while (true) {
            if(sum > 100)
                break;
            ++i;
            sum += i;
        }

        System.out.println("i = " + i);
        System.out.println("sum = " + sum);
    }
}
```

{% endcode %}

## continue

> ### 자신이 포함된 반복문의 끝으로 이동
>
> * 그리고 다음 반복으로 넘어갑니다.
> * 전체 반복 중에서 특정 조건시 반복을 건너뛸 때 유용합니다.

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

{% code lineNumbers="true" %}

```java
class Control6_2 {
    public static void main(String[] args) {
        for (int i = 0; i <= 10; i++) {
            // 3의 배수는 건너뜀 : 3, 6, 9
            if (i % 3 == 0)
                continue;
            System.out.println("i = " + i);
        }
    }
}
```

{% endcode %}

## 이름붙은 반복문&#x20;

> ### 반복문에 이름을 붙여서 하나 이상의 반복문을 벗어납니다.

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

{% code lineNumbers="true" %}

```java
class Control6_3 {
    public static void main(String[] args) {
        allLoop :
        for (int i = 2; i < 10; i++) {
            for (int j = 1; j < 10; j++) {
                if (i == 5) {
                    break allLoop;
                }
                System.out.println(i + " * " + j + " = " + (i * j));
            }
        }
    }
}
```

{% endcode %}

{% code lineNumbers="true" %}

```java
class Control6_4 {
    public static void main(String[] args) {
        int i = 2;
        allLoop :
        while (true) {
            for (int j = 1; j < 10; j++) {
                if (i == 5) {
                    break allLoop;
                }
                System.out.println(i + " * " + j + " = " + (i * j));
            }
            i++;
        }
    }
}
```

{% endcode %}

{% code lineNumbers="true" %}

```java
class Control6_5 {
    public static void main(String[] args) {
        allLoop : for (int i = 2; i < 10; i++) {
            for (int j = 1; j < 10; j++) {
                if (i == 5) {
                    continue allLoop;
                }
                System.out.println(i + " * " + j + " = " + (i * j));
            }
        }
    }
}
```

{% endcode %}

{% hint style="info" %}
Ref. Java의 정석 기초편 Chapter4(20, 21, 22, 23, 24)

Ref. [break continue 이름붙은 반복문](https://www.youtube.com/watch?v=vDoiQAY1iRM\&list=PLW2UjW795-f6xWA2_MUhEVgPauhGl3xIp\&index=39)
{% 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./break-continue.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.
