조건식의 결과에 따라 블록 실행 여부가 결정
조건식 : true/ false 값을 산출할 수 있는 연산식 또는 boolean 타입 변수가 올 수 있음
조건 연산자(삼항 연산자)를 통해 코드 라인을 줄일 수 있음
public class practice {
public static void main(String[] args) {
int age = 15;
if(age >= 20){
System.out.println("성인");
} else if(age >= 14) {
System.out.println("청소년");
}
else{
System.out.println("어린이");
}
}
}
인자로 선택변수를 받아 변수의 값에 따라서 실행문이 결정
default는 else와 같은 역할
public class practice {
public static void main(String[] args) {
int month = 12;
switch(month) {
// 아래와 같이 다른 케이스에 같은 결과값을 출력하고 싶은 경우, 생략 가능
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
System.out.println("31일");
break;
case 4:
case 6:
case 9:
case 11:
//윤년 판별 필요
System.out.println("28일");
break;
default:
System.out.println("존재하지 않음");
}
}
}