# 여러 가지의 조건

## 그룹 조건

if 를 이용하여 그룹 조건을 구현할 수 있다. 그룹조건이라 함은 다음과 같은 경우를 말한다.

* 세 가지 중 하나는 반드시 실행된다.
* 네 가지 중 하나는 반드시 실행된다.
* ...

### 형태

```java
if(조건식1){

}
else if(조건식2){

}
else if(조건식3){

}
//else if 무제한 추가 가능
else{

}
```

### 사례

서울에서 부산까지 가는 방법은 비행기, KTX, 고속버스 세 가지라고 가정한다.\
각각 요금은 다음과 같다.

| 종류   | 요금     | 소요시간 |
| ---- | ------ | ---- |
| 비행기  | 10000원 | 1시간  |
| KTX  | 5000원  | 3시간  |
| 고속버스 | 3000원  | 5시간  |

가진 돈에 맞게 가장 빨리 이동할 수 있는 수단으로 서울에서 부산까지 이동한다고 할 때 돈에 따라 선택할 수 있는 선택지는 다음과 같다.

1. 소지금이 10000원 이상이면 비행기를 탄다.
2. 1번이 아니며 소지금이 5000원 이상이면 KTX를 탄다.
3. 2번이 아니며 소지금이 3000원 이상이면 버스를 탄다.
4. 3번이 아니면 이동 가능한 방법이 없다.

### 데모 1 : if, else로 구성

#### 분석

if와 else만을 사용하기 위해 분석한 다이어그램은 다음과 같다.

<div align="left"><img src="/files/-MeNZdUUWxaUQzkAGbpD" alt=""></div>

#### 코드

위의 다이어그램을 코드로 나타내면 다음과 같다.

```java
int money = 12000;
if(money >= 10000){
    System.out.println("비행기로 이동");
}
else{
    if(money >= 5000){
        System.out.println("KTX로 이동");
    }
    else{
        if(money >= 3000){
            System.out.println("고속버스로 이동");
        }
        else{
            System.out.println("이동 가능한 수단이 없습니다");
        }
    }
}
```

if와 else만으로도 구성할 수 있지만 구문이 매우 복잡해지는 문제가 발생한다.

### 데모 2 : if, else if, else로 구성

#### 분석

다이어그램으로 구조를 표현해본다.

<div align="left"><img src="/files/-MeNZiK2XXUr0OBhlf2a" alt=""></div>

#### 코드

위의 다이어그램을 코드로 나타내면 다음과 같이 데모1에 비해 좀 더 간결해진다.

```java
int money = 12000;
if(money >= 10000){
    System.out.println("비행기로 이동");
}
else if(money >= 5000){
    System.out.println("KTX로 이동");
}
else if(money >= 3000){
    System.out.println("고속버스로 이동");
}
else{
    System.out.println("이동 가능한 수단이 없습니다");
}
```

else if는 원하는 만큼 추가가 가능하며 else에는 조건을 적을 수 없다.


---

# 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://docs.sysout.co.kr/base-language/java/beginner/control-statement/if-statement/group-condition.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.
