if : if 문은 조건이 참(true)일 때 실행
if - else : if 문에 else 블록을 추가하면 조건이 거짓(false)일 때 실행할 코드를 지정할 수 있다.
if - else if - else : 여러 조건을 체크하려면 else if를 사용하는데, 마지막에는 모든 조건이 거짓일 때 실행될 else 블록을 추가할 수 있다.
비교 연산자는 boolean 으로 실행된다.
package 조건문;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
int a = 10;
int b = 20;
System.out.println( a==b );
System.out.println( a!=b );
System.out.println( a > b );
System.out.println( a < b );
System.out.println( a <= b );
System.out.println( a >= b );
}
}
package 조건문;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
int a = 10;
int b = 20;
int c = 30;
int d = 25;
// 그리고 = &&
System.out.println( a == 10 && c == d );
// 또는 = ||
System.out.println( a == 10 || c == d );
if( a > 10 ){
System.out.println( "Hello" );
} else {
System.out.println( "Bye" );
}
}
}
package 조건문;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
int score = 60;
// 스코어가 90점 이상으면 A
// 70점 이상이고 90 미만이면 B
// 60~70 이면 C
// 나머지는 F 로 출력하는 코드 작성.
if (score >= 90){
System.out.println( "A" );
} else if( score >=70 && score < 90) {
System.out.println( "B" );
} else if ( score >= 60 && score < 70) {
System.out.println( "C" );
} else {
System.out.println( "F" );
}
}
}
int a = 2;
// a 가 1 이면, Hello 출력
// a 가 2 이면, Bye 출력
// a 가 3 이면, Good 출력
// 모두 아니면, End 출력
// 맞는 괄호 안만 실행.
if(a == 1 ) {
System.out.println("Hello");
}else if (a == 2) {
System.out.println("Bye");
} else if ( a== 3 ) {
System.out.println("Good");
} else {
System.out.println("End");
}
System.out.println();
'Software Development > Java' 카테고리의 다른 글
[Java] for/while 반복문 (0) | 2024.05.31 |
---|---|
[Java] 조건문 ② Switch 문 사용 하기 (0) | 2024.05.31 |
[Java] 연산 (0) | 2024.05.31 |
[Java] 변수 선언, 메모리 할당, 값 저장하기 (0) | 2024.05.31 |
[Java] 이클립스(eclipse) 설치하기, 프로젝트 / 클래스 만들기 (0) | 2024.05.31 |