728x90
반응형
Swich Case
더보기
# include <stdio.h>
# include <float.h>
int main(){
char c;
while((c = getchar())!='.')
{
printf("I like ");
switch(c) // integer type
{
case 'A':
case 'a': // 문자도 정수형이라 가능
printf("apples");
break; // break 존재 이유는 break가 없다면 모두 실행하게 된다.
case 'B':
case 'b':
printf("baseball");
break;
case 'C':
case 'c':
printf("cook");
break;
case 'D':
case 'd':
printf("danawa");
break;
default:
printf("nothing");
}
printf(".\n");
while(getchar()!='\n')
continue;
}
}
switch 문의 경우는 경우의 수가 정해져 있을 경우는 간결하게 보기 위해 switch문이 좋다.
하지만 경우의 수가 많아질 경우는 if문이 더 편할것이다.
728x90
반응형
최근댓글