
1. 조건문
1-1. 조건문이란?
1-2. 조건문의 종류
1-3. if문
if문은 주어진 조건이 참일 경우에 지정된 구문이 실행된다.
if(조건){
...실행할 구문..
}
실행할 구문이 한 줄만 있을 경우 괄호{}는 생략 가능하다.
1-4. if문의 조건식
1-5. 그렇지 않으면?
if문이 조건이 참일 경우에 실행되는 구문이면, if문의 조건과 반대되는 경우에 실행되는 구문이 else문이다.
else문은 독립적으로 실행될 수 없고, 반드시 if문의 뒤에 위치해야 한다.
if(){
...실행할 구문..
} else {
..반대 경우 실행할 구분
}
1-6. if ~ else if ~ else문
if문과 else문 사이에 else if 문으로 두번째 조건, 세번째 조건을 나열할 수 있다.
else if 문은 필요한 만큼 나열할 수 있으며, 필요치 않을 경우 else문은 생략 가능하다.
if(1차조건){
..실행할 구문..
}else if(2차조건){
..실행할 구문..
}else if(3차조건){
..실행할 구문..
}else{
}
1-7. switch문
switch(기준값){
case: 값1 :
..실행될 구문..
break;
case: 값n :
..실행될 구문..
break;
default :
..모든 경우에 충족하지 않을 경우 실행
break;
}
char grade = 'B';
switch (grade) {
case 'A':
System.out.println("91~100점 사이");
case 'B':
System.out.println("81~90점 사이");
case 'C':
System.out.println("71~80점 사이");
default:
System.out.println("70점 이하입니다.");
=>break; 가 없기 때문에 여러 문장이 출력
// switch문 사용하여서
// grade 가 A 또는 B 또는 C이면 -> Pass 한번 출력
// 그렇지 않은 등급이면 -> Not pass 한번 출력
char grade = 'A';
switch (grade) {
case 'A':
case 'B':
case 'C':
System.out.println("Pass");
break;
default :
System.out.println("Not Pass");
break;
}
/*
* 1. 1은 "축구" 2는 "농구", 3은 "야구"
* 4는 "배구" 그 외에는 "배드민턴"이 출력되도록
* 프로그램을 만드시오.
*/
char sport = 5;
switch(sport) {
case 1:
System.out.println("축구");
break;
case 2:
System.out.println("농구");
break;
case 3:
System.out.println("야구");
break;
case 4:
System.out.println("배구");
break;
default:
System.out.println("배드민턴");
}
System.out.println("==============");
/*
* 2. 숫자가 3이면 "안녕"이 세 줄
* 숫자가 2이면 "안녕"이 두 줄
* 숫자가 1이면 "안녕"이 한 줄
* 그 외에는 "잘가"를 출력
*/
char hello = 5;
switch(hello) {
case(3):
System.out.println("안녕");
case(2):
System.out.println("안녕");
case(1):
System.out.println("안녕");
break;
default:
System.out.println("잘가");
}
1.8. 삼항연산자
간단한 if문을 짧게 처리하는 연산자
if(){
...
} else if () {
...
}
(조건문)? True 일 때 : False 일 때
int a = 0;
if(5<4) {
a = 50;
} else {
a = 40;
}
System.out.println(a);
System.out.println("-------------------");
//삼항연산자
int b = (5<4)? 50 : 40;
System.out.println(b);
/*
* 1. 정수 a가 10보다 작거나 같으면 100을 곱하고
* 그렇지 않으면 a값을 출력
*/
int a = 10;
int result= a <= 10 ? a*100 : a;
System.out.println(result);
/*
* 2. 현재시각 hour 변수가 12시보다 작으며
* '오전' 출력, 그렇지 않으면 '오후' 출력
*/
int hour = 11;
String ampm = (hour < 12)? "오전" : "오후";
System.out.println(ampm);
문제풀이
/*
* 1. 만약 3000원 이상의 돈을 가지고 있으면
* '택시를 타고 가라'
* 그렇지 않으면 '걸어가라'
*/
int money = 3500;
if (money >= 3000) {
System.out.println("택시를 타고 가라");
} else {
System.out.println("걸어가라");
}
/*
* 2. 돈이 3000원 이상 있거나, 카드가 있다면
* '택시를 타고 가라'
* 그렇지 않다면 '걸어가라'
*/
int money2 = 2500;
int card = 0;
if(money2 >= 3000 || card >= 1 ) {
System.out.println("택시를 타고 가라");
} else {
System.out.println("걸어가라");
}
/*
* 3. 어떤 특정 정수 값 'a'가 짝수이면 "짝수"
* 홀수이면 "홀수"
*/
int number = 4;
if (number % 2 <= 0) {
System.out.println("짝수");
}else {
System.out.println("홀수");
}
/*
* 4. 국어, 영어, 수학 점수의 평균이 95점 이상이면
* '장학생'을 출력,
* 국어점수가 70점 이상이면 "국어 : 합격 ",
* 그렇지 않으면 "국어 : 불합격"
*/
int korean = 100;
int english = 100;
int mat = 100;
if((korean + english + mat)/3 >= 95 ) {
System.out.println("장학생");
}
if (korean >= 70) {
System.out.println("국어 : 합격");
} else if (korean < 70) {
System.out.println("국어 : 불합격");
}
/*
* 5. 수학점수가 90점 이상이면 "A"학점,
* 80점 이상이며 "B"학점, 70점 이상이면 "C"학점
* 60점 이상이면 "D"학점, 나머지는 "F"학점
*/
int math = 100;
if(math >= 90) {
System.out.println("A학점");
} else if (math >= 80) {
System.out.println("B학점");
} else if (math >= 70) {
System.out.println("C학점");
} else if (math >= 60) {
System.out.println("D학점");
} else {
System.out.println("F학점");