프로그램이 어떤 조건에 따라 다른 행동을 하게 만드는 문법이야.
예를 들어:
if (조건식) {
// 조건이 참일 때 실행
} else {
// 조건이 거짓일 때 실행
}
public class HelloApplication {
public static void main(String[] args) {
int age = 17;
if (age >= 20) {
System.out.println("성인입니다.");
} else {
System.out.println("미성년자입니다.");
}
}
}
🟢 출력:
미성년자입니다.
int score = 85;
if (score >= 90) {
System.out.println("A 학점");
} else if (score >= 80) {
System.out.println("B 학점");
} else if (score >= 70) {
System.out.println("C 학점");
} else {
System.out.println("F 학점");
}
🟢 출력:
B 학점