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

# if statement

* 조건식의 다양한 예

| 조건문                                       | 조건식이 참일 조건                        |
| ----------------------------------------- | --------------------------------- |
| ch == 'y' \|\| ch == 'Y'                  | 문자 ch가 'y' 또는 'Y'일 때              |
| ch == ' ' \|\| ch == '\t' \|\| ch == '\n' | 문자 ch가 공백이거나 탭 또는 개행 문자일 때        |
| 'A' <= ch && ch <= 'Z'                    | 문자 ch가 대문자일 때                     |
| 'a' <= ch && ch <= 'z'                    | 문자 ch가 소문자일 때                     |
| '0' <= ch && ch <= '9'                    | 문자 ch가 숫자일 때                      |
| **str.equals("yes")**                     | 문자열 str의 내용이 "yes"일 때(대소문자 구분)    |
| str.equalsIgnoreCase("yes")               | 문자열 str의 내용이 "yes"일 때(대소문자 구분 안함) |

* else문 생략을 통한 코드 리팩토링 팁 : default 값을 지정해놓으면 else문을 굳이 쓰지 않아도 되고 코드가 훨씬 간결해진다.

```java
import java.util.Scanner;

class Ex4_4 {
	public static void main(String[] args) { 
		int score  = 0;   
		char grade ='D'; 

		System.out.print("점수를 입력하세요.>");
		Scanner scanner = new Scanner(System.in);
		score = scanner.nextInt(); // 화면을 통해 입력받은 숫자를 저장한다.

		if (score >= 90) {         
			 grade = 'A';             
		} else if (score >=80) {    
			 grade = 'B'; 
		} else if (score >=70) {   
			 grade = 'C'; 
		} //else {                   
			 //grade = 'D'; 
		//}
		System.out.println("당신의 학점 "+ grade +"입니다");
	}
}
```

**formatter %c를 이용하여 학점(A,B,C..)와 +-까지 출력하는 코드 + 리팩토**

```java
import java.util.Scanner;
//점수에 따라 자신의 점수와 학점(A,B,C) 와 +-까지 출력하는 코드 
class Ex4_5 {
	public static void main(String[] args) {
		int score = 0;
		char grade = 'C', opt = '0';
		
		System.out.println("점수를 입력해주세요.>");
		
		Scanner scanner = new Scanner(System.in);
		score = scanner.nextInt();
		
		System.out.printf("당신의 점수는 %d입니다%n",score);
		
		if(score >= 90) {
			grade = 'A';
			if(score >= 98) {
				opt = '+';
			} else if(score < 94) {
				opt = '-';
			}
		} else if (score >= 80){     
			grade = 'B';
			if (score >= 88) {
				opt = '+';
			} else if (score < 84)	{
				opt = '-';
			}
		} 
		System.out.printf("당신의 학점은 %c%c입니다%n",grade,opt);
	}
}
```
