조건문의 논리적인 참과 거짓을 구분하여 참 일때 실행하고 거짓일 때 실행을 안한다.
#include <iostream>
int main(){
if(condition1){
statement
}
else if(condition2){ // if(!(condition1) && condition2)와 같은 의미
statement
}
else{ // if(!(condition1 && condition2)) 과 같은 의미
statement
}
return 0
}
괄호안의 인티저 값에 따라 case를 설정하여 그 case에 있는 종속문을 실행한다.
#include <iosteram>
using namespace std;
int main(){
int a = 3;
switch(a){
case 1 :
cout << "good" << endl;
break;
case 2 :
cout << "not bad" << endl;
break;
case 3 :
cout << "bad" << endl;
break;
}
// break를 붙여주지 않으면 case의 값을 찾을 때 까지 내려가며 종속문을 실행
return 0;
}
bad
switch문은 int값만 받기 때문에 이외의 자료형은 사용할 수 없지만 예외로 문자형은 ASCII 코드 값에 대응한 숫자로 받아들이기 때문에 사용할 수 있다.
ex) 'a' == 97