[06] 조건문 (if문, switch~case문)
1. 제어문
- 분기문(비교문), 반복문, break문, continue문
2. if문
- 조건식에는 연산의 최종결과 값 true, false 수식만 사용가능
3. 1형식 if문
if (조건식){
//조건이 true일때 수행할 문장들;
}
4. 2형식 if문
if (조건식){
//조건이 true일때 수행할 문장들;
}
else{
//조건이 false일때 수행할 문장들;
}
5. 중첩 if문
if (조건식1){
//조건1이 true일때 수행할 문장들;
}
else if (조건식2){
//조건2이 true일때 수행할 문장들;
}
else if (조건식3){
//조건3이 true일때 수행할 문장들;
}
else{
//조건이 false일때 수행할 문장들;
}
6. switch~case문
- 조건값(인자값)은 정수형(byte, short, int)과 문자형(char) 이다.
- long형, 문자열, boolean형, 실수형은 사용할 수 없다.
- case값의 목록이 반드시 순차적일 필요는 없다.
7. switch~case 형식
switch (인자값) {
case 조건값1 :
수행문; break;
case 조건값2 :
수행문; break;
case 조건값3 :
수행문; break;
default :
수행문;
}
파일명 : TestIf.java
public class TestIf{
public static void main(String[] args){
//if문
/*
형식)
if(조건) {
처리내용;
}
int su=5;
if(su>0){
//if(5>0)
System.out.println("0보다 크다");
}
*/
//2형식
/*
if(조건) {
처리내용 A;
}
else{
처리내용 B;
}
if(su>0){
System.out.println("양수");
}
else {
System.out.println("음수");
}
*/
/*
if(조건1){
처리내용 A;
}
else if(조건2){
처리내용 B;
}
else if(조건3){
처리내용 C;
}
else{
처리내용 D;
}
*/
/*
예제)
국,영,수의 평균구해서
1) 평균 95점이상이면 장학생
2) 평균 70점이상이면 합격, 아니면 불합격
3) 평균 90점이상이면 수
80점이상이면 우
70점이상이면 미
60점이상이면 양
나머지는 가
*/
/*
//입력->가공 및 처리 -> 출력
String name="홍길동";
int kor=80,mat=70,eng=90;
int aver;
String grade;
aver=(kor+mat+eng)/3;
if(aver>=90){
System.out.println("장학생");
}
if(aver>=70){
System.out.println("합격");
}
else{
System.out.println("불합격");
}
//문)평균이 70점이상이라도
//국어,영어,수학점수 중에서
//50점미만점수가 한과목이라도 있으면 재시험
if(aver>=70){
if(kor<50 || eng<50 || mat<50){
System.out.println("재시험");
}
else{
System.out.println("합격");
}
}
else{
System.out.println("불합격");
}
if(aver>=90)
grade="수";
else if(aver>=80)
grade="우";
else if(aver>=70)
grade="미";
else if(aver>=60)
grade="양";
else
grade="가";
//출력
System.out.println("이름="+name);
System.out.println("국어="+kor);
System.out.println("영어="+eng);
System.out.println("수학="+mat);
System.out.println("평균="+aver);
System.out.println("학점="+grade);
*/
/*
예제) 사칙 연산 계산기 (+ - * / % )
10+20=30
10-20=-10
10*20=200
10/20=0
10%20=10
*/
char op='+';
int p=10,q=20;
int res=0;
if(op=='+')
res=p+q;
else if(op=='-')
res=p-q;
else if(op=='*')
res=p*q;
else if(op=='/')
res=p/q;
else if(op=='%')
res=p%q;
System.out.println(p+" "+op+" " +q+"=" + res);
int res=0;
double res2=0;
if(op=="+")
res=a+b;
else if(op=="-")
res=a-b;
else if(op=="*")
res=a*b;
else if(op=="/")
res2=(double)a/b;
else if(op=="%")
res=a%b;
if(op=="/")
System.out.println(a+op+b+"="+res2);
else
System.out.println(a+op+b+"="+res);
}
}
파일명 : TestSwitch.java
public class TestSwitch{
public static void main(String[] args){
/*
제어문(분기문)
switch(값)
{
case 값1:
처리내용;
break;
case 값2:
처리내용;
break;
case 값3:
처리내용;
break;
default :
처리내용;
}
break문 : switch~case를 빠져나갈때
*/
/*
switch( 20 ){
case 10:
System.out.println("사과");
break;
case 20:
System.out.println("딸기");
case 30:
System.out.println("바나나");
break;
default :
System.out.println("기타");
}
*/
String name="홍길동";
int kor=80,eng=70,mat=90;
int aver=(kor+eng+mat)/3;
String grade=""; //수 우 미 양 가
switch(aver/10)
{
case 10:
case 9: grade="수"; break;
case 8: grade="우"; break;
case 7: grade="미"; break;
case 6: grade="양"; break;
default: grade="가";
}
//출력
System.out.println("이름="+name);
System.out.println("국어="+kor);
System.out.println("영어="+eng);
System.out.println("수학="+mat);
System.out.println("평균="+aver);
System.out.println("학점="+grade);
/*
switch(op){
case '+' : res=p+q; break;
case '-' : res=p-q; break;
case '*' : res=p*q; break;
case '/' : res=p/q; break;
case '%' : res=p%q; break;
}
System.out.println(p+" "+op+" " +q+"=" + res);
*/
}
}