1 / 30

6 장 . 반복 제어문

6 장 . 반복 제어문. while 문. while ( 조건식 ) 문장 ; while ( 조건식 ) { 문장 ; ... }. while 문. #include <stdio.h> void main() { int i = 1; while (i <= 10) { printf("%d ", i); i = i + 1; } }. while 문. int i=1, sum=0; while (i <= 10) {

Download Presentation

6 장 . 반복 제어문

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. 6장. 반복 제어문 Ch.6

  2. while 문 • while (조건식) 문장; • while (조건식) { 문장; ... } Ch.6

  3. while 문 #include <stdio.h> void main() { int i = 1; while (i <= 10) { printf("%d ", i); i = i + 1; } } Ch.6

  4. while 문 int i=1, sum=0; while (i <= 10) { sum += i; /* sum=sum+i; */ ++i; } Ch.6

  5. while 문 • while ( (i <= 10) == 1 ) • while ( (i <= 10) != 0 ) Ch.6

  6. while 문 i = 0; while (++i <= 10) { printf("%d ", i); /* i = i + 1; 을 생략 */ } Ch.6

  7. while 문 #include <stdio.h> void main() { int num; printf("Enter a number. \n"); scanf("%d", &num); /* 2와 3의 공배수 판정하기 */ while ( !(num % 2) && !(num % 3) ) { printf("Yes. Enter another number. \n"); scanf("%d", &num); } printf("No. %d is not divisible by 2 and 3. \n", num); } Ch.6

  8. while 문 • while ( !(num % 2) && !(num % 3) ) • 12의 입력 !(12%2) && !(12%3) → !(0) && !(0) →1 && 1 → 1 →참 • 26의 입력 !(26%2) && !(26%3) → !(0) && !(2) → 1 && 0 → 0 →거짓 Ch.6

  9. 문자 입출력 함수 (p.34) • 변수 = getchar(); Ex> ch = getchar(); • putchar(문자 또는 ASCII코드); Ex> putchar(ch); 또는 putchar(‘A’); Ch.6

  10. 문자 입출력 함수 (p.34) #include <stdio.h> void main() { int ch; ch = getchar(); /* 한 문자 입력, ch 에 저장 */ putchar(ch); /* ch의 문자를 출력 */ } Ch.6

  11. 문자 입출력 함수 (p.34) • 동일한 표현 • ch = getchar(); putchar(ch); • putchar( getchar() ); • putchar(‘A’); • putchar(65); Ch.6

  12. while 문 #include <stdio.h> void main() { char ch; while ( (ch=getchar()) != '\n' ) { /* 개행문자가 아닌 동안 */ if (ch >= 'a' && ch <= 'z') ch = ch + ('A'-'a'); /* 대문자로 변환 */ /* ‘A’-’a’ = 65-96 = -31 */ putchar(ch); } } Ch.6

  13. while 문 • while ( (ch=getchar()) != '\n' ) • ch=getchar() • (ch=getchar()) • (ch=getchar()) != '\n' • while ( ch = getchar() != '\n' ) • getchar()!='\n' • ch=getchar()!='\n' Ch.6

  14. while 문 i = 10; while (i < 20) printf("unexpected loop \n"); Ch.6

  15. for 문 • for (초기식 ; 조건식 ; 증감식) 문장; • 초기식; while (조건식) { 문장; 증감식; } Ch.6

  16. for 문 • i = 1; while (i <= 10) { /* while 문 사용 */ printf("%d ", i); i++; } • for (i = 1 ; i <= 10 ; i++) /* for문 사용 */ printf("%d ", i); Ch.6

  17. for 문 • while (1) { /* while 무한루프 */ . . . } • for ( ; ; ) { /* for 무한루프 */ . . . } Ch.6

  18. for 문 • sum=0; for (i=1; i<=10; i++) sum += i; • for (sum=0, i=1; i<=10; i++) sum += i; Ch.6

  19. for 문 for (i=2; i<=9; i++) { printf("< %d > \n" , i); for (j=1; j<=9; j++) printf("%d * %d = %d \n", i, j, i*j); } Ch.6

  20. for 문 * *** ***** ******* ********* *********** <예제 6-4> Ch.6

  21. for 문 #include <stdio.h> // 예제 6-4 void main() { int i, j; for (i = 0; i <= 5; i++){ /* 각 행에 대해 */ for (j = 5; j > i; j--) printf(“”); /* 행의 앞 공백 출력 */ printf(“*"); for (j = 0; j < i; j++) printf(“**"); printf(“\n"); } } Ch.6

  22. for 문 #include <stdio.h> // 1에서 50사이의 홀수 합 구하기 void main() { int j, sum; for(sum=0, j=1; j<=50; j+=2) {/* j는 2씩 증가 */ sum += j; } printf("sum = %d\n", sum); } Ch.6

  23. do while 문 do 문장; while (조건식) ; Ch.6

  24. do while 문 #include <stdio.h> void main() { int num; do { printf("positive integer only : "); scanf("%d", &num); } while (num < 1); } Ch.6

  25. do while 문 do { printf("%d ", i); } while (++i <= 10); /* do로 시작한 while임을 시각적으로 표시 */ Ch.6

  26. break 문 // 구구단을 곱한 값이 30이내만 순서대로 출력 for (i=2; i<=9; i++) for (j=1; j<=9; j++) { if (i*j > 30) break; printf("%d * %d = %d \n", i, j, i*j); } Ch.6

  27. break 문 #include <stdio.h> #include <conio.h> void main() { while(1) { /* 무한루프 */ printf(“*"); if ( kbhit() ) break; } printf(“\nKeyBoard is pressed!"); } Ch.6

  28. continue 문 #include <stdio.h> void main() { char ch; while ( (ch=getchar()) != '\n' ) { // 대문자만 출력하기 if (!(ch>='A' && ch<='Z')) /* 대문자가 아니면 */ continue; putchar(ch); } } Ch.6

  29. goto 문 . . . goto 레이블명; . . . . . . 레이블명:문장; . . . . . . Ch.6

  30. goto 문 #include <stdio.h> // 1에서 10까지의 합을 출력 void main() { int sum = 0; int num = 1; before:if (num>10) goto after; /* after 로 이동 */ sum += num; num++; goto before; /* before 로 이동 */ after: printf("sum = %d \n", sum); } Ch.6

More Related