C 프로그램 제어문
본 자료는 3페이지 의 미리보기를 제공합니다. 이미지를 클릭하여 주세요.
닫기
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
해당 자료는 3페이지 까지만 미리보기를 제공합니다.
3페이지 이후부터 다운로드 후 확인할 수 있습니다.

목차

3.1 문자의 입력
3.2 중첩된 if문
3.3 ㅊ의 여러 형태
3.4 while 반복문
3.5 do 반복문
3.6 중첩된 반복문
3.7 break 문
3.8 continue 문
3.9 switch 문
3.10 goto 문

본문내용

e "stdio.h"
main()
{
int i, j;
for (i=0; i<5; i++) {
for (j=1; j<100; j++) {
printf("%d", j);
if (j==5) break;
}
printf("\n");
}
}
3.8 continue 문
현재 위치에서 바로 조건 검사 위치로 진행한다.
예. #include "stdio.h"
main()
{
int x;
for (x=0; x<10; x++) {
continue;
printf("%d ", x); /* 이 문장은 결코 실행되지 않는다. */
}
printf("%d\n", x);
}
** for 문의 경우, 반복의 증가 연산 부분이 먼저 수행되고 조건 검사가 수행된 후
반복을 계속한다.
예제. #include "stdio.h"
#include "conio.h"
main()
{
int total, i;
total = 0;
do {
printf("Enter next number(between 1 and 5): ");
scanf("%d", &i);
if (i<1 || i>5)
continue; /* continue 대신 break 인 경우는? */
total += i; /* total = total + i;
} while (i); /* i == 0 이면 빠져나옴 */
printf("Total is %d\n", total);
}
3.9 switch 문
다중 선택 문장, 즉 여러 가지 중 하나를 선택하게 한다.
형식: switch(variable) {
case constant1:
statement sequence
break;
case constant2:
statement sequence
break;
...
default: --------> 일치하는 것이 없을 때 실행, 생략 가능
statement sequence
break;
}
예. #include "stdio.h"
main()
{
int i;
printf("Enter a number between 1 and 4: ");
scanf("%d", &i);
switch(i) {
case 1:
printf("one\n");
break; /* break; 가 생략되면 ? */
case 2:
printf("two\n");
break; /* break; 가 생략되면 ? */
case 3:
printf("three\n");
break; /* break; 가 생략되면 ? */
case 4:
printf("four\n");
break; /* break; 가 생략되면 ? */
default:
printf("Unrecognized number.\n");
}
}
** switch 문 안에 또 다른 switch 문이 올 수 있다.
예. switch(a) {
case 1:
switch(b) {
case 0: printf("b is false");
break;
case 1: printf("b is true");
}
break;
case 2:
...
}
switch문에는 int 와 char 형만을 사용할 수 있다.
break 문이 없으면 switch문을 빠져나가지 않고, 다음 case를 계속 수행한다.
case와 관련된 문장을 공유할 수 있다.
예. #include "stdio.h"
#include "conio.h"
main()
{
char ch;
printf("Enter the letter: ");
ch = getche();
switch(ch) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'y':
printf("\n%c is a vowel\n", ch);
break;
default:
printf("\n%c is a consonant\n", ch);
}
}
3.10 goto 문
레이블(label)이 있는 곳으로 무조건 분기한다.
예. goto mylable;
printf("this will not print");
mylable: printf("this will print");
** 특별한 곳에서만 사용한다.
(예: 심각한 오류가 발생할 때 여러 번 중첩된 루틴를 빠져나올 경우)
예제. #include "stdio.h"
main()
{
int i=1;
again:
printf(%d", i++);
if (i<10) goto again;
}
프로그래밍 연습
3.1절 연습문제 1
3.3절 연습문제 1
3.5절 연습문제 2
3.6절 연습문제 1, 2
3.7절 연습문제 2
3.8절 연습문제 1
3.9절 연습문제 2
종합문제 1-1

키워드

  • 가격1,300
  • 페이지수9페이지
  • 등록일2003.02.10
  • 저작시기2003.02
  • 파일형식한글(hwp)
  • 자료번호#221064
본 자료는 최근 2주간 다운받은 회원이 없습니다.
청소해
다운로드 장바구니