if(조건문) {
조건문이 참일 경우 실행되는 블록
}
public class IfExam1 {
public static void main(String[] args) {
int a = 5;
if(a > 4) {
System.out.println("a는 4보다 큽니다.");
}
}
}
a는 4보다 큽니다.
if(조건문) {
조건문이 참일 경우 실행되는 블록
} else {
조건문이 거짓일 경우 실행되는 블록
}
public class IfExam2 {
public static void main(String[] args) {
int a = 3;
if(a > 4) {
System.out.println("a는 4보다 큽니다.");
} else {
System.out.println("a는 4 이하입니다.");
}
}
}
a는 4 이하입니다.
if(조건문1) {
조건문1이 참일 경우 실행되는 블록
} else if(조건문2) {
조건문2이 참일 경우 실행되는 블록
} else {
조건문1이나 조건문2에 해당되지 않을 경우 실행되는 블록
}
public class IfExam3 {
public static void main(String[] args) {
int score = 70;
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");
}
System.out.println("프로그램 종료");
}
}
C
프로그램 종료
public class IfExam4 {
public static void main(String[] args) {
int a = 4;
if (a > 5)
System.out.println("a는 10보다 큽니다.");
System.out.println("hello");
}
}
hello
조건식 ? 반환값1 : 반환값2
public class IfExam5 {
public static void main(String[] args) {
int a = 10;
int value = (a > 5) ? 20 : 30;
System.out.println(value);
}
}
*괄호 안의 조건에 대해 참일 때 앞(20), 거짓일 때 뒤(30)가 출력됨
20