> For the complete documentation index, see [llms.txt](https://docs.sysout.co.kr/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.sysout.co.kr/base-language/java/beginner/control-statement/if-statement/single-condition.md).

# 단일 조건

## 단일 조건

이 문서에서는 if 구문을 단독으로 사용하는 방법에 대해서 살펴본다.

### 형태

if구문의 형태는 다음과 같다.

```java
if(조건식) {
    실행코드
}
```

조건식 자리에는 `논리(boolean)`의 결과가 나오는 식이 위치해야 하며, 식이 `참(true)`일 경우 실행 코드가 실행되고, `거짓(false)`일 경우 실행 코드는 실행하지 않는다.

### 사례

구현할 상황은 다음과 같다.

> 자장면을 3그릇 이상 주문하면 1000원 할인이 되도록 프로그램 구현

### 분석

상황을 다이어그램으로 분석해보면 다음과 같다.

<div align="left"><img src="https://4208234536-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M_TNZwLuHV9ipYLbvRq%2F-MeNXOqWBZTeJJJFmiPL%2F-MeNXezHhR4EfBbLkTam%2Fimage.png?alt=media&amp;token=10ed1cfc-7589-4ce3-8c71-5da1cda2223b" alt="자장면 주문 및 할인 프로세스(예시)"></div>

### 코드

```java
import java.lang.*;

public class IfExample1 {
    public static void main(String[] args){
        int order = 3;
        
        if(order >= 3){
            System.out.println("1000원 할인");
        }
        
        System.out.println("결제 진행");
    }
}
```

order 변수의 값을 변경하며 실행해보면 특정 경우에만 할인 메세지가 출력 되는 것을 확인할 수 있다. 이처럼 if 구문을 이용하면 **특정 코드를 원하는 상황에서 실행**시킬 수 있다.

### 응용

만약 실제 결제 금액을 알고 싶다면 변수를 활용하여 금액을 계산할 수 있다. 예시에서는 개별 금액이 5000원이라고 가정하고 진행해본다.

```java
import java.lang.*;

public class IfExample2 {
    public static void main(String[] args){
        int order = 3;
        int price = 5000;
        
        int result = order * price;
        
        if(order >= 3){
            result -= 1000;
        }
        
        System.out.println("결제금액 : " + result + "원");
    }
}
```

### 응용 예제 분석

응용 예제를 `result` 측면에서 분석하면 다음과 같다.

<div align="left"><img src="https://4208234536-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M_TNZwLuHV9ipYLbvRq%2F-MeNXOqWBZTeJJJFmiPL%2F-MeNXttIhIgMh47Qva2o%2Fimage.png?alt=media&amp;token=d549c27e-1aaf-47c0-9fbc-33276c246508" alt=""></div>
