> For the complete documentation index, see [llms.txt](https://leeans-dev-book.gitbook.io/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://leeans-dev-book.gitbook.io/docs/lecture/java/section3./if-else.md).

# if / else

주어진 boolean 값에 따라 명령문 실행 여부를 결정

```java
package sec04.chap01;

public class Ex01 {
    public static void main(String[] args) {
        boolean open = true;
        int saleFrom = 10, saleTo = 20;
        int today = 15;

        //  💡 if : 괄호 안의 boolean 값이 true면 다음 명령 실행
        if (open) System.out.println("영업중");
        if (!open) System.out.println("영업종료");

        //  💡 실행할 명령이 한 줄 이상일 경우 블록 사용
        if (today >= saleFrom && today <= saleTo) {
            System.out.println("세일중입니다.");
            System.out.println("전품목 20% 할인");
        }
    }
}

```

```java
	//  💡 else : if문 안의 boolean 값이 false일 경우 실행
        if (open) {
            System.out.println("영업중");
        } else {
            System.out.println("영업종료");
        }
```

```java
package sec04.chap01;

public class Ex02 {
    public static void main(String[] args) {
        boolean willOrderCoffee = true;
        boolean likeMilk = false;
        boolean likeIce = true;
        boolean likeSweet = false;
        boolean angry = false;
        int myRank = 2;

        //  💡 중첩 사용
        if (willOrderCoffee) {

            //  ⭐️ 블록 내에서만 사용되는 변수 (이후 스코프에서 배움)
            String toOrder = "";

            //  필요에 따라 적절한 것 사용
            toOrder = likeMilk ? "라떼" : "아메리카노";
            if (likeIce) toOrder = "아이스 " + toOrder;
            if (!likeSweet) toOrder += " 시럽추가";

            System.out.printf("저는 %s 할게요%n", toOrder);
        } else {
            if (!angry || myRank > 3) {
                System.out.println("저는 괜찮아요.");
            } else {
                System.out.println("너님들끼리 드세요.");
            }
        }

    }
}

```

```java
package sec04.chap01;

public class Ex03 {
    public static void main(String[] args) {
        int score = 85;

        //  💡 else if : 첫 if문이 false일 때 다른 조건을 연속 사용
        //  else만 사용하는 것은 맨 마지막에
        if (score > 90) {
            System.out.println('A');
        } else if (score > 80) {
            System.out.println('B');
        } else if (score > 70) {
            System.out.println('C');
        } else if (score > 60) {
            System.out.println('D');
        } else {
            System.out.println('F');
        }
        
    }
}

```

```java
package sec04.chap01;

public class Ex04 {
    public static void main(String[] args) {
        int score = 85;

        //  ⭐ 보다 가독성 좋은 방식
        //  return문: 해당 메소드를 종료하고 빠져나옴

        if (score > 90) {
            System.out.println('A');
            return;
        }
        if (score > 80) {
            System.out.println('B');
            return;
        }
        if (score > 70) {
            System.out.println('C');
            return;
        }
        if (score > 60) {
            System.out.println('D');
            return;
        }
        System.out.println('F');
    }
}

```
