Section 01 if if else if if else

  • Slides: 18
Download presentation

Section 01 제어문의 종류 종류 제어문 보조 제어문 선택 제어 반복제어 if if ~

Section 01 제어문의 종류 종류 제어문 보조 제어문 선택 제어 반복제어 if if ~ else ~ if 중첩된 if ~ else switch while for do ~ while break continue 2 무조건 분기 goto return

Section 5 -1】Ex 1. c 01 【예제 02 If ~ else 문 예제 01

Section 5 -1】Ex 1. c 01 【예제 02 If ~ else 문 예제 01 02 03 04 05 06 07 08 09 10 11 12 #include <stdio. h> int main(void) { int input; printf("양수를 입력하세요. n"); scanf("%d", &input); if (input % 2 == 0) printf("입력한 수 %d는 짝수입니다. n", input); 표준입력 스트림 else printf("입력한 수 %d는 홀수입니다. n", input); return 0; } 4

Section 5 -2】Ex 2. c 01 【예제 02 01 #include <stdio. h> 02 03

Section 5 -2】Ex 2. c 01 【예제 02 01 #include <stdio. h> 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 int main(void) { int jumsu; printf("0점에서 100점수 사이의 점수를 입력하세요. n"); scanf("%d", &jumsu); if(jumsu >= 0 && jumsu <= 100) printf("입력한 점수는 %d입니다. n", jumsu); else{ printf("0점에서 100점사이를 입력하세야 합니다. n"); return 0; } if (jumsu >= 0 && jumsu < 60) printf("%d점은 F학점입니다. n", jumsu); else if(jumsu >= 60 && jumsu < 70) printf("%d점은 D학점입니다. n", jumsu); else if(jumsu >= 70 && jumsu < 80) printf("%d점은 C학점입니다. n", jumsu); else if(jumsu >= 80 && jumsu < 90) printf("%d점은 B학점입니다. n", jumsu); else if(jumsu >= 90 && jumsu <= 100) printf("%d점은 D학점입니다. n", jumsu); return 0; } 5

Section 5 -3】Ex 3. c 01 【예제 02 다중 If 문 예제 01 #include

Section 5 -3】Ex 3. c 01 【예제 02 다중 If 문 예제 01 #include <stdio. h> 02 int main(void) 03 { 04 int input; 05 printf("양수를 입력하세요. n"); 06 scanf("%d", &input); 07 if(input >= 0){ 08 if (input % 2 == 0) 09 printf("입력한 수 %d는 짝수입니다. n", input); 10 else 11 printf("입력한 수 %d는 홀수입니다. n", input); 12 } 13 else{ 14 printf("입력한 수 %d는 음수입니다. n", input); 15 } 16 return 0; 6 17 }

Section 5 -4】Ex 4. c 01 【예제 02 다중 If 문을 사용하지 않은 예제

Section 5 -4】Ex 4. c 01 【예제 02 다중 If 문을 사용하지 않은 예제 01 #include <stdio. h> 02 int main(void) 03 { 04 int input; 05 printf("양수를 입력하세요. n"); 06 scanf("%d", &input); 07 if( input >= 0 && input % 2 == 0) 08 printf("입력한 수 %d는 양수이며 짝수입니다. n", input); 09 10 else if ( input >= 0 && input % 2 != 0) 11 printf("입력한 수 %d는 양수이며 홀수입니다. n", input); 12 13 else 14 printf("입력한 수 %d는 음수입니다. n", input); 15 return 0; 16 } 7

Section 5 -5】Ex 5. c 01 【예제 02 switch~case 문 기본 예제 01 #include

Section 5 -5】Ex 5. c 01 【예제 02 switch~case 문 기본 예제 01 #include <stdio. h> 02 int main(void) 03 { 04 int input; 05 printf("영어로 인사하는 법을 배우겠습니다. n"); 06 printf("아침 인사는 1번을 누르세요. n"); 07 printf("점심 인사는 2번을 누르세요. n"); 08 printf("저녁 인사는 3번을 누르세요. n"); 09 scanf("%d", &input); 10 switch(input){ 11 case 1: 12 printf("Good Morning!!n"); 13 break; 14 case 2: 15 printf("Good Afternoon!!n"); 16 break; 17 case 3: 18 printf("Good Night!!n"); 19 break; 20 } 21 return 0; 22 } 9 표준입력 스트림

Section 5 -6】Ex 6. c 01 【예제 02 switch~case 문 default 사용 예제 01

Section 5 -6】Ex 6. c 01 【예제 02 switch~case 문 default 사용 예제 01 #include <stdio. h> 02 int main(void) 03 { 04 int input; 05 printf("영어로 인사하는 법을 배우겠습니다. n"); 06 printf("아침 인사는 1번, 점심인사는 2번, 저녁인사는 3번을 누르세요. n"); 07 scanf("%d", &input); 08 switch(input){ 09 case 1: 10 printf("Good Morning!!n"); 11 break; 12 case 2: 13 printf("Good Afternoon!!n"); 14 break; 15 case 3: 16 printf("Good Night!!n"); 17 break; 18 default: 19 printf("잘못 입력하였습니다. 1~3사이의 숫자를 입력하세요. n"); 20 } 21 return 0; 22 } 10

Section 5 -7】Ex 7. c 01 【예제 02 break 문 활용 예제 01 #include

Section 5 -7】Ex 7. c 01 【예제 02 break 문 활용 예제 01 #include <stdio. h> 02 int main(void) 03 { 04 int input; 05 printf("영어로 인사하는 법을 배우겠습니다. n"); 06 printf("아침 인사는 1번을 누르세요. n"); 07 printf("점심 인사는 2번을 누르세요. n"); 08 printf("저녁 인사는 3번을 누르세요. n"); 09 scanf("%d", &input); 10 switch(input){ 11 case 1: 12 printf("Good Morning!!n"); 13 case 2: 14 printf("Good Afternoon!!n"); 15 case 3: 16 printf("Good Night!!n"); 17 break; 18 default: 19 printf("잘못 입력하였습니다. 1~3사이의 숫자를 입력하세요. n"); 20 } 21 return 0; 22 } 11

Section 5 -9】Ex 9. c 01 【예제 02 switch 문으로 수식이 사용된 예제 01

Section 5 -9】Ex 9. c 01 【예제 02 switch 문으로 수식이 사용된 예제 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 #include <stdio. h> int main(void) { int score; printf("당신의 점수를 입력하세요 : "); scanf("%d", &score); switch(score/10) { case 10 : case 9 : printf("점수는 %d 이고 성적은 %c 입니다. n", score, 'A'); break; case 8 : printf("점수는 %d 이고 성적은 %c 입니다. n", score, 'B'); break; case 7 : printf("점수는 %d 이고 성적은 %c 입니다. n", score, 'C'); break; case 6 : printf("점수는 %d 이고 성적은 %c 입니다. n", score, 'D'); break; default : printf("점수는 %d 이고 성적은 %c 입니다. n", score, 'F'); break; } return 0; } 13

Section 5 -10】Ex 10. c 01 【예제 02 switch 문으로 사칙연산 예제 01 02

Section 5 -10】Ex 10. c 01 【예제 02 switch 문으로 사칙연산 예제 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include <stdio. h> int main(void) { int num 1, num 2; char arithmetic; printf("두 수와 연산자를 입력하세요 n"); printf("연산자는 아래와 같습니다. n"); printf("더하기 : + 빼기 : - 곱하기 : * 나누기 : / n"); printf("입력순서 : 정수 1 연산자 정수 2 n"); scanf("%d %c %d", &num 1, &arithmetic, &num 2); switch(arithmetic) { case '+' : printf("%d %c %d = %d n", num 1, arithmetic, num 2, break; case '-' : printf("%d %c %d = %d n", num 1, arithmetic, num 2, break; case '*' : printf("%d %c %d = %d n", num 1, arithmetic, num 2, break; case '/' : printf("%d %c %d = %d n", num 1, arithmetic, num 2, break; default : printf("연산자를 잘못 입력했습니다. n"); break; } return 0; } 14 num 1 + num 2); num 1 - num 2); num 1 * num 2); num 1 / num 2);

Section 5 -11】Ex 11. c 01 【예제 02 If문을 이용한 사칙연산 01 02 03

Section 5 -11】Ex 11. c 01 【예제 02 If문을 이용한 사칙연산 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include <stdio. h> int main(void) { int num 1, num 2; char arithmetic; printf("두 수와 연산자를 입력하세요 n"); printf("연산자는 아래와 같습니다. n"); printf("더하기 : + 빼기 : - 곱하기 : * 나누기 : / n"); printf("입력순서 : 정수 1 연산자 정수 2 n"); scanf("%d %c %d", &num 1, &arithmetic, &num 2); switch(arithmetic) { case '+' : printf("%d %c %d = %d n", num 1, arithmetic, num 2, break; case '-' : printf("%d %c %d = %d n", num 1, arithmetic, num 2, break; case '*' : printf("%d %c %d = %d n", num 1, arithmetic, num 2, break; case '/' : printf("%d %c %d = %d n", num 1, arithmetic, num 2, break; default : printf("연산자를 잘못 입력했습니다. n"); break; } return 0; } 15 num 1 + num 2); num 1 - num 2); num 1 * num 2); num 1 / num 2);

Section 5 -12】Ex 12. c 01 【예제 02 조건 연산자 사용 예제 01 02

Section 5 -12】Ex 12. c 01 【예제 02 조건 연산자 사용 예제 01 02 03 04 05 06 07 08 09 10 11 12 #include <stdio. h> int main(void) { int min, max; int x = 10, y = 20; max = ( x > y ) ? x : y; min = ( x > y ) ? y : x; printf("두 수 %d 와 %d 중에 큰 수는 %d 이다. n", x, y, max); printf("두 수 %d 와 %d 중에 작은 수는 %d 이다. n", x, y, min); return 0; } 17

Section 5 -13】Ex 13. c 01 【예제 02 조건 연산자 사용 예제 01 #include

Section 5 -13】Ex 13. c 01 【예제 02 조건 연산자 사용 예제 01 #include <stdio. h> 02 int main(void) 03 { 04 int x; 05 printf("양수를 입력하세요. n"); 06 scanf("%d", &x); 07 ( x >= 0 ) ? printf("x는 유효한 값입니다. n") 08 : printf("x는 유효한 값이 아닙니다. n"); 09 10 11 return 0; } 18