목차
- else 예제 - if ... else ... 구문
- else 정의
- else 예제 - if ... else if ... else ... 구문
else 예제 - if ... else ... 구문
public class Hz {
public static void main(String[] args) {
int time = 15;
if (time < 12) {
System.out.println("오전");
} else {
System.out.println("오후");
}
}
}
else 정의
if 조건문에서 if 조건이 거짓일 때 실행할 내용 지정.
PS.
if... : if 조건이 참이면 실행.
...else if ... : if 조건이 거짓이면 실행. (※ 0개 이상 가능.)
...else ... : if 및 else if 조건이 거짓이면 실행.
switch : 경우 선택 조건문.
else 예제 - if ... else if ... else ... 구문
Hz.java
public class Hz {
public static void main(String[] args) {
int time = 20;
if (time < 12) {
System.out.println("오전");
} else if (time < 18) {
System.out.println("오후");
} else {
System.out.println("저녁");
}
}
}
최신댓글