5 include stdio h int mainvoid int num

  • Slides: 57
Download presentation
5장. 제어문 #include <stdio. h> int main(void) { int num; printf(“Please enter an integer:

5장. 제어문 #include <stdio. h> int main(void) { int num; printf(“Please enter an integer: "); scanf("%d", &num); if ( num < 0 ) printf("Is negative. n"); printf("num = %dn", num); return 0; } 1

조건문 §if if의 사용 예 01: /* Ex 05_01. c */ 02: #include <stdio.

조건문 §if if의 사용 예 01: /* Ex 05_01. c */ 02: #include <stdio. h> 03: 04: int main(void) 05: { 06: int score; 07: 08: printf("점수를 입력하세요 : "); 09: scanf("%d", &score); 10: 11: if( score < 60 ) 12: printf("%d점은 불합격입니다. n", score); 13: 14: if( score >= 60 ) 15: printf("%d점은 합격입니다. n", score); 16: 17: return 0; 18: } 5장. 제어문 If의 사용 4

조건문 §if if else의 사용 예 01: /* Ex 05_02. c */ 02: #include

조건문 §if if else의 사용 예 01: /* Ex 05_02. c */ 02: #include <stdio. h> 03: 04: int main(void) 05: { 06: int score; 07: 08: printf("점수를 입력하세요 : "); 09: scanf("%d", &score); 10: 11: if( score < 60 ) 12: printf("%d점은 불합격입니다. n", score); 13: else 14: printf("%d점은 합격입니다. n", score); 15: 16: return 0; 17: } 5장. 제어문 If else의 사용 7

조건문 §if 다중 if의 사용 예(1/2) 01: /* Ex 05_03. c */ 02: #include

조건문 §if 다중 if의 사용 예(1/2) 01: /* Ex 05_03. c */ 02: #include <stdio. h> 03: 04: int main(void) 05: { 06: int score; 07: char grade; 08: 09: printf("점수를 입력하세요 : "); 10: scanf("%d", &score); 11: 12: if( score >= 90 ) 13: grade = 'A'; 14: else if( score >= 80 ) 15: grade = 'B'; 16: else if( score >= 70 ) 17: grade = 'C'; 18: else if( score >= 60 ) 19: grade = 'D'; 20: else 21: grade = 'F‘; 다중 If 5장. 제어문 10

조건문 §if 중첩된 if의 사용 예(1/2) 01: /* Ex 05_04. c */ 02: #include

조건문 §if 중첩된 if의 사용 예(1/2) 01: /* Ex 05_04. c */ 02: #include <stdio. h> 03: 04: int main(void) 05: { 06: int score; 07: char grade; 08: 09: printf("점수를 입력하세요 : "); 10: scanf("%d", &score); 11: 5장. 제어문 12

조건문 §if 중첩된 if의 사용 예(2/2) 12: 13: 14: 15: 16: 17: 18: 19:

조건문 §if 중첩된 if의 사용 예(2/2) 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: } if ( score < 0 || score > 100 ) { printf("잘못 입력하셨습니다. n"); } else { if ( score >= 90 ) grade = 'A'; else if ( score >= 80 ) grade = 'B'; else if ( score >= 70 ) grade = 'C'; else if ( score >= 60 ) grade = 'D'; else grade = 'F'; 중첩된 if 다중 if printf("%c 학점입니다. n", grade); } return 0; 5장. 제어문 13

조건문 §switch를 이용한 사칙연산 계산기(2/2) 12: 13: 14: 15: 16: 17: 18: 19: 20:

조건문 §switch를 이용한 사칙연산 계산기(2/2) 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: } switch ( op ) { case '+': printf("%d + %d = %dn", a, b, a + b); break; case '-': printf("%d - %d = %dn", a, b, a - b); break; case '*': printf("%d * %d = %dn", a, b, a * b); break; case '/': printf("%d / %d = %dn", a, b, a / b); break; default: printf("계산할 수 없습니다. n"); break; } switch의 사용 return 0; 5장. 제어문 17

조건문 §switch 대신 if else if를 사용하는 경우(2/3) 12: 13: 14: 15: 16: 17:

조건문 §switch 대신 if else if를 사용하는 경우(2/3) 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: if ( op == '+' ) { printf("%d + %d = %dn", a, b, a + b); } else if ( op == '-' ) { printf("%d - %d = %dn", a, b, a - b); } else if ( op == '*' ) { printf("%d * %d = %dn", a, b, a * b); } else if ( op == '/' ) { printf("%d / %d = %dn", a, b, a / b); } else { printf("계산할 수 없습니다. n"); } 5장. 제어문 if else if의 사용 20

조건문 §switch break를 사용하지 않은 경우의 예 11: 12: 13: 14: 15: 16: 17:

조건문 §switch break를 사용하지 않은 경우의 예 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: } switch ( op ) { case '+': printf("%d + %d = %dn", a, b, a + b); case '-': printf("%d - %d = %dn", a, b, a - b); case '*': printf("%d * %d = %dn", a, b, a * b); case '/': printf("%d / %d = %dn", a, b, a / b); default: printf("계산할 수 없습니다. n"); } Break를 생략한 경우 return 0; 5장. 제어문 24

반복문 §for for의 사용 예 01: /* Ex 05_08. c */ 02: #include <stdio.

반복문 §for for의 사용 예 01: /* Ex 05_08. c */ 02: #include <stdio. h> 03: 04: int main(void) 05: { 06: int i; 07: 08: for ( i = 1 ; i <= 10 ; i++) 09: printf("%d ", i); 10: 11: printf("n"); 12: 13: return 0; 14: } for의 사용 5장. 제어문 30

반복문 §for for를 이용한 합계와 곱 구하기 01: /* Ex 05_09. c */ 02:

반복문 §for for를 이용한 합계와 곱 구하기 01: /* Ex 05_09. c */ 02: #include <stdio. h> 03: 04: int main(void) 05: { 06: int i; 합계를 저장할 int형 변수 sum 선언 07: int sum = 0; 08: int factorial = 1; 곱을 저장할 int형 변수 factorial 선언 09: 10: for ( i = 1 ; i <= 10 ; i++) for의 사용 11: { 12: sum += i; 13: factorial *= i; 14: } 15: 16: printf("1~10의 합계 : %dn", sum); 17: printf("1~10의 곱 : %dn", factorial); 18: 19: return 0; 20: } 5장. 제어문 32

반복문 §for 중첩된 for의 사용 01: /* Ex 05_10. c */ 02: #include <stdio.

반복문 §for 중첩된 for의 사용 01: /* Ex 05_10. c */ 02: #include <stdio. 03: 04: int main(void) 05: { 06: int i, j; 07: 08: for ( i = 1 ; i < 10 ; i++ ) 09: { 10: for ( j = 1 ; j < 10 ; j++ ) 11: { 12: printf("%d*%d=%2 d ", i, j, i*j); 13: } 14: printf("n"); 15: } 16: 17: return 0; 18: } 5장. 제어문 중첩된 for의 사용 35

반복문 §while의 사용 예 01: /* Ex 05_11. c */ 02: #include <stdio. h>

반복문 §while의 사용 예 01: /* Ex 05_11. c */ 02: #include <stdio. h> 03: 04: int main(void) 05: { 06: int i; 07: 08: i = 1; 09: while ( i <= 10 ) 10: printf("%d ", i++); 11: 12: printf("n"); 13: 14: return 0; 15: } while의 사용 5장. 제어문 38

반복문 §while을 이용한 합계와 곱 구하기 01: /* Ex 05_12. c */ 02: #include

반복문 §while을 이용한 합계와 곱 구하기 01: /* Ex 05_12. c */ 02: #include <stdio. h> 03: 04: int main(void) 05: { 06: int i; 07: int sum = 0; 08: int factorial = 1; 09: 10: i = 1; for의 초기식은 while 앞에서 수행 11: while ( i <= 10 ) for의 조건식을 while의 조건식으로 사용 12: { 13: sum += i; 14: factorial *= i; 15: i++; for의 증감식을 while의 맨 끝에서 수행 16: } 17: 18: printf("1~10의 합계 : %dn", sum); 19: printf("1~10의 곱 : %dn", factorial); 20: 21: return 0; 22: } 5장. 제어문 40

반복문 §while을 이용한 사칙연산 계산기(1/3) 01: /* Ex 05_13. c */ 02: #include <stdio.

반복문 §while을 이용한 사칙연산 계산기(1/3) 01: /* Ex 05_13. c */ 02: #include <stdio. h> 03: 04: int main(void) 05: { 06: int a, b; 07: char op; 08: 09: printf("수식을 입력하세요 (0 0 0 입력 시 종료) : "); 10: scanf("%d %c %d", &a, &op, &b); 11: 5장. 제어문 41

반복문 §while을 이용한 사칙연산 계산기(2/3) 12: 13: 14: 15: 16: 17: 18: 19: 20:

반복문 §while을 이용한 사칙연산 계산기(2/3) 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: while ( a != 0 || op != '0' || b != 0 ) { switch ( op ) { case '+': printf("%d + %d = %dn", a, b, a + b); break; case '-': printf("%d - %d = %dn", a, b, a - b); break; case '*': printf("%d * %d = %dn", a, b, a * b); break; case '/': printf("%d / %d = %dn", a, b, a / b); break; default: printf("계산할 수 없습니다. n"); break; } 5장. 제어문 op에 따라서 더하기, 빼기, 곱하기, 나누기 연산 수행 while문 수행 42

반복문 §do while의 사용 예 01: /* Ex 05_15. c */ 02: #include <stdio.

반복문 §do while의 사용 예 01: /* Ex 05_15. c */ 02: #include <stdio. h> 03: 04: int main(void) 05: { 06: int i; 07: 08: i = 1; 09: do 10: printf("%d ", i++); 11: while ( i <= 10 ); 12: printf("n"); 13: 14: return 0; 15: } do while의 사용 5장. 제어문 46

분기문 §break의 사용 예 01: /* Ex 05_16. c */ 02: #include <stdio. h>

분기문 §break의 사용 예 01: /* Ex 05_16. c */ 02: #include <stdio. h> 03: 04: int main(void) 05: { 06: int i; 07: 08: for ( i = 1 ; i <= 10 ; i++ ) 09: { 10: if ( i % 2 == 0 ) 11: break; 12: printf("%d ", i); 13: } 14: printf("n"); 15: 16: return 0; 17: } i가 1~10인 동안 수행되는 for 문 break를 만나면 for 루프 탈출 5장. 제어문 49

분기문 §break를 이용한 무한 루프 탈출(1/2) 01: /* Ex 05_17. c */ 02: #include

분기문 §break를 이용한 무한 루프 탈출(1/2) 01: /* Ex 05_17. c */ 02: #include <stdio. h> 03: #include <conio. h> 04: 05: int main(void) 06: { 07: int num, sum; 08: int i; 09: 5장. 제어문 50

분기문 §continue의 사용 예 01: /* Ex 05_18. c */ 02: #include <stdio. h>

분기문 §continue의 사용 예 01: /* Ex 05_18. c */ 02: #include <stdio. h> 03: 04: int main(void) 05: { 06: int i; 07: 08: for ( i = 1 ; i <= 10 ; i++ ) 09: { 10: if ( i % 2 == 0 ) 11: continue; 12: printf("%d ", i); 13: } 14: printf("n"); 15: 16: return 0; 17: } continue를 만나면 루프 시작으로 돌아감 5장. 제어문 53

분기문 §goto의 사용 예 01: /* Ex 05_19. c */ 02: #include <stdio. h>

분기문 §goto의 사용 예 01: /* Ex 05_19. c */ 02: #include <stdio. h> 03: 04: int main(void) 05: { 06: int i; 07: 08: for ( i = 1 ; i <= 10 ; i++ ) 09: { 10: if ( i % 2 == 0 ) 11: goto exit; 12: printf("%d ", i); 13: } 14: exit: goto의 레이블 15: printf("n"); 16: 17: return 0; 18: } goto를 만나면 지정된 위치로 이동 5장. 제어문 55

분기문 §return의 사용 예 01: /* Ex 05_20. c */ 02: #include <stdio. h>

분기문 §return의 사용 예 01: /* Ex 05_20. c */ 02: #include <stdio. h> 03: 04: int main(void) 05: { 06: int i; 07: 08: for ( i = 1 ; i <= 10 ; i++ ) 09: { 10: if ( i % 2 == 0 ) 11: return 1; 12: printf("%d ", i); 13: } 14: printf("프로그램을 종료합니다. n"); 15: 16: return 0; 17: } return을 만나면 함수를 호출한 곳으로 되돌아감 5장. 제어문 57