2.3 기 타
2.3.1 goto 문
goto label_name은 label_name이 있는 곳으로 수행순서를 옮긴다.
goto문은 사용하지 않는 것이 좋다.-이유: error 발생시 문제를 복잡하게 함.
2.3.2 break 문과 continue 문
break는 사용된 가장 안쪽 루프에서 탈출을 행한다. 그러나 switch문에서의 break는 switch 문의 탈출을 행한다. continue 문은 break 문과 반대개념의 제어문이다.
예제 1) break와 continue 문
#include <stdio.h>
void main() {
printf("char q가 입력되지 않는 한 while loop 계속수행\n");
printf("Enter charter\n");
while(1) {
if(getchar() !='q') continue;
printf("quit because of getting 'q'\n");
break;
}
printf("program end\n\n");
}
#include <iostream.h>
void main() { char ch;
cout<<"char q가 입력되지 않는 한 while loop 계속수행\n";
cout<<"Enter charter\n";
while(1) {
cin>>ch;
if(ch !='q')
continue;
cout<<"quit because of getting 'q'\n";
break;
}
cout<<"program end\n\n";
}
예제 2) continue문으로 작성된 1과 10사이의 두 수합이 15되는 수의 출력
#include <stdio.h>
void main() { int i, j, k;
for(i=1;i<=10;i++)
for(j=1; j<=10; j++) { k=i+j;
if(k==15) {
printf("%d+%d=15\n",i,j);
continue; }
} }