if문의 구조
키워드(조건식){
실행문1
실행문2
실행문3
}
* ( )소괄호 안의 조건식이 참(true)일 때, if문 { }중괄호 안의 내용이 실행됨
if | 조건문 기본 |
else if | 조건문이 여러개 필요할 때 사용 (순차적인 조건 대입), 참(True)이 나오면 나머지 무시 |
else | 앞선 조건문에 모두 해당하지 않을 경우 |
package day00;
public class Ex01 {
public static void main(String[] args) {
int num = 10;
if(true) {
System.out.println("실행");
}
if(false) {
System.out.println("실행X");
}
int score = 27;
if(score>=60) {
System.out.println("합격");
}
if(score<60) {
System.out.println("불합격");
}
// 문제1) 짝수, 홀수 출력
int x = 8;
if(x%2==0) {
System.out.println("짝수");
}
if(x%2==1) {
System.out.println("홀수");
}
// 문제2) 4의 배수 출력
int y = 15;
if(y%4==0) {
System.out.println("4의 배수");
}
if(y%4!=0){
System.out.println("4의 배수 아님");
}
}
}
'Java > ☞' 카테고리의 다른 글
[13] 반복문 : while (0) | 2020.09.08 |
---|---|
[12] 조건문 : switch-case (0) | 2020.09.08 |
[10] 제어문 (0) | 2020.09.08 |
[09] 연산자 : 논리연산자 (0) | 2020.09.08 |
[08] 연산자 : 비교연산자 (0) | 2020.09.08 |
댓글