1 / 23

제 25 강

제 25 강. Loop 에서 빠져나오기. 내용. break continue return exit. break. for, while, do-while, switch-case 에서 빠져나온다 . while (1){ c = getchar(); if (c==EOF) break; putchar(c); }. flow chart (while-break). false. expr. true. ... break;. 끝. while ( ){ … break; … }.

nanda
Download Presentation

제 25 강

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 제 25 강 Loop 에서 빠져나오기 shcho.pe.kr

  2. 내용 • break • continue • return • exit

  3. break • for, while, do-while, switch-case에서 빠져나온다. • while (1){ • c = getchar(); • if (c==EOF) break; • putchar(c); • }

  4. flow chart (while-break) false expr true ... break; ... 끝

  5. while ( ){ … break; … } while ( ){ … goto label1; .. } label1: Goto와의 비교

  6. 예제 프로그램 1 • EOF가 나올 때까지 한 글자씩 읽으면서 “.”이 나오면 멈춘다. • while ((c=getchar())!=EOF){ • putchar(c); • if (c=='.') break; • }

  7. 예제 2 • 비정상적인 경우의 종료 • while (scanf(“%d %d”,&x, &y)>0){ • if (y==0) break; • ratio = x/y; • printf(“비율 = %f\n”, ratio); • }

  8. 다음 shcho.pe.kr

  9. 실습 lab25_01/copy.c • getchar, putchar를 이용하여 표준입력에 들어오는 모든 문자를 표준 출력으로 그대로 내보내는 프로그램을 작성한다. • 단, while 문의 조건을 while (1) 로 쓰고 EOF 문제는 break로 해결한다.

  10. break: loop를종료 while(){ ... break; ... } continue: loop를 다시 시작 while (){ ... continue; ... } Continue

  11. flow chart (while-continue) false expr true ... continue; ... 끝

  12. for 문에서의 continue • for (expr1; expr2; expr3){ • ... • continue; • ... • } * 따라서 expr3을 수행

  13. flow chart(for-continue) expr1 false expr2 true ... continue; ... expr3 끝

  14. flow chart(for-break) expr1 false expr2 true ... break; ... expr3 끝

  15. 다음 shcho.pe.kr

  16. 실습 lab25_02/continue.c • 목적: for와 continue의 관계 이해 • 파일: continue.c • 내용: for를 이용하여 i를 0에서 99까지 하나씩 증가시키며 반복하게 한다. for문의 block 내에서는 만일 홀수이거나 3의 배수이면 건너뛰게 한다. (else는 사용하지 않는다) • 건너 뛰지 않았으면 sum에다 i값을 더하게 한다. • for문이 끝나면 출력하게 한다.

  17. 실습 lab25_03(break.c) • while (1){ • } 을 이용한다. • 표준 입력에서 글자를 읽어 들이면서 숫자문자(‘0’~’9’)이면 이들의 합을 구하고('2' 이면 2를 더한다) 다른 문자들은 무시한다. 입력이 끝나면 총 합을 출력한다. • 예: 입력이 “My birthday is 1988/12/1.”의 경우는 총 합은 30이 된다. (1+9+8+8+1+2+1) • 숫자 문자가 아닌 경우는 continue를 이용하여 건너 뛴다. • EOF이면 break한다. • 숫자 문자를 숫자로 바꾸려면 c – '0'을 하면된다.

  18. 주의 • if 또는 else block에서는 해당되지 않는다. • if (X){ • // ... • if (i<0) break; • // ... • }

  19. return • 함수 전체를 빠져나갈 때 • int fun(){ • ... • if (...) return 0; • } • int main(){ • ... while (1){ • if (c==EOF) return 1; • ...

  20. exit • 프로그램 전체를 종료 • int fun(){ • ... • if (...) exit(1); • } • int main(){ • ... k = fun(); • ...

  21. 다음 shcho.pe.kr

  22. lab25_04/return.c • 표준입력에서 글자를 읽어들이면서 출력한다. 만일 마침표가 연속하여 두 번 나오면 출력 후에 1 값을 return 하면서 종료한다. EOF에 도달하면 출력하지 않고 exit(0)으로써 종료한다.

  23. 제 25 강 끝. shcho.pe.kr

More Related