📕1. 조건문이란?
- 주어진 조건에 따라서 다른 문장을 선택할 수 있도록 프로그래밍 하는 것
- if문과 switch문으로 나뉜다.
📕2. if문
if (조건식) {
수행구문; [위 조건식이 참일 경우 해당 구문이 실행된다.]
}
int a = 10;
if(a>8) {
System.out.println("참입니다.");
}
if(조건식) {
수행구문; [위 조건식이 참일 경우 해당 구문이 실행된다.]
}
else {
수행구문; [if조건식에 해당 되지 않을 경우 else수행구문이 실행된다.]
}
int a = 5;
if(a > 6) {
System.out.println("거짓입니다.");
}
else {
System.out.println("참 입니다.");
}
📕3. switch문
switch(값) {
case 1: 수행구문;
break;
case 2: 수행구문;
break;
...
}
int a = 1;
switch(a) {
case 1: color = "red";
break;
case 2: color = "green"
break;
}
System.out.println(color);