こんにちは、もがちゃんです
今回の記事では、繰り返し処理(for文、while文、do while文)などで使用されるbreakとcontinueの使い方について説明します
目次
break文について
break文は、繰り返し処理の中で使用する他に、switch文と一緒に使うこともあります
どちらで使う場合も、ほぼ意味は一緒で繰り返しやswitch文の処理を終了させたい場合に使用します
この動作は、繰り返し処理の外やswitch文の外へジャンプする動作と同じため、goto文で記述することも可能です
制約
JISX3010 プログラム言語C
break文は、switch本体若しくはループ本体の中に、又はswitch本体若しくはループ本体としてだけ、現れなければならない。
意味規則
break文は、それを囲む最も内側のswitch文又は繰返し文の実行を終了させる。
break文を使ったサンプル
for文、while文、do while文、switch文と一緒にbreak文を使ったサンプルプログラムとその動作結果を紹介します
input.txtの内容
line1
line2
line3
end
line4
line5
ソースコード
#include <stdio.h>
#include <string.h>
/*
* for文とbreak文のサンプル
*/
int main(int argc, char *argv[]) {
FILE *fp = NULL;
fp = fopen("input.txt", "r");
if (fp == NULL) {
fprintf(stdout, "fopen error!\n");
return -1;
}
char buffer[256];
//繰り返し処理
for ( ; !feof(fp); ) {
char *p = fgets(buffer, sizeof(buffer), fp);
if(p != NULL) {
if(strncmp("end", p, strlen("end")) == 0) {
fprintf(stdout, "=== end found. ===\n");
// end文字がみつかったので繰り返し処理を終了
// goto end_forと同じ
break;
}
fprintf(stdout, "%s",p);
}
}
//end_for:
fprintf(stdout, "for loop end.\n");
if (fp != NULL) {
fclose(fp);
}
}
実行結果

input.txtの内容はfor文の時と同じファイルを使用
ソースコード
#include <stdio.h>
#include <string.h>
/*
* while文とbreak文のサンプル
*/
int main(int argc, char *argv[]) {
FILE *fp = NULL;
fp = fopen("input.txt", "r");
if (fp == NULL) {
fprintf(stdout, "fopen error!\n");
return -1;
}
char buffer[256];
//繰り返し処理
while ( !feof(fp) ) {
char *p = fgets(buffer, sizeof(buffer), fp);
if(p != NULL) {
if(strncmp("end", p, strlen("end")) == 0) {
fprintf(stdout, "=== end found. ===\n");
// end文字がみつかったので繰り返し処理を終了
// goto end_whileと同じ
break;
}
fprintf(stdout, "%s",p);
}
}
//end_while:
fprintf(stdout, "while loop end.\n");
if (fp != NULL) {
fclose(fp);
}
}
実行結果

input.txtの内容はfor文の時と同じファイルを使用
ソースコード
#include <stdio.h>
#include <string.h>
/*
* do while文とbreak文のサンプル
*/
int main(int argc, char *argv[]) {
FILE *fp = NULL;
fp = fopen("input.txt", "r");
if (fp == NULL) {
fprintf(stdout, "fopen error!\n");
return -1;
}
char buffer[256];
//繰り返し処理
do {
char *p = fgets(buffer, sizeof(buffer), fp);
if(p != NULL) {
if(strncmp("end", p, strlen("end")) == 0) {
fprintf(stdout, "=== end found. ===\n");
// end文字がみつかったので繰り返し処理を終了
// goto end_dowhileと同じ
break;
}
fprintf(stdout, "%s",p);
}
} while ( !feof(fp) );
//end_dowhile:
fprintf(stdout, "do while loop end.\n");
if (fp != NULL) {
fclose(fp);
}
}
実行結果

ソースコード
#include <stdio.h>
#include <string.h>
void func_switch(int no);
/*
* switch文とbreak文のサンプル
*/
int main(int argc, char *argv[]) {
func_switch(1);
func_switch(2);
func_switch(3);
func_switch(4);
func_switch(5);
}
void func_switch(int no) {
fprintf(stdout, "=== func_switch(%d) ===\n",no);
switch(no) {
case 1:
fprintf(stdout, "case 1\n");
case 2:
fprintf(stdout, "case 2\n");
case 3:
fprintf(stdout, "case 3\n");
// switch文を終了する
// goto end_switchと同じ
break;
case 4:
fprintf(stdout, "case 4\n");
case 5:
fprintf(stdout, "case 5\n");
default:
fprintf(stdout, "default\n");
}
//end_switch:
fprintf(stdout, "switch end.\n");
}
実行結果

continue文について
continue文は、繰り返し処理の中でしか使用できません
continue文は、繰り返し処理の途中で、繰り返し処理ブロックの最後に進めたい時に使用します
この動作は、繰り返し処理ブロックの最後へジャンプする動作と同じため、goto文で記述することも可能です
制約
JISX3010 プログラム言語C
continue文は、ループ本体の中に又はループ本体としてだけ現れなければならない。
意味規則
continue文は、それを囲む最も内側の繰返し文のループ継続部、すなわちループ本体の終わりへの分岐を引き起こす。
continue文を使ったサンプル
for文、while文、do while文を使った繰り返し処理内で、continueを使ったサンプルプログラムとその動作結果を紹介します
ソースコード
#include <stdio.h>
#include <string.h>
/*
* for文とcontinue文のサンプル
*/
int main(int argc, char *argv[]) {
int total = 0;
for (int i = 1; i <= 10; i++) {
fprintf(stdout, "loop block start\n");
if (i % 2) {
//奇数は、totalの計算しない
//goto for_block_endと同じ
continue;
}
fprintf(stdout, "total = %d + %d\n", total, i);
total += i;
//for_block_end:
}
fprintf(stdout, "total = %d\n", total);
}
実行結果

ソースコード
#include <stdio.h>
#include <string.h>
/*
* while文とcontinue文のサンプル
*/
int main(int argc, char *argv[]) {
int total = 0;
int i = 0;
while (++i <= 10) {
fprintf(stdout, "loop block start\n");
if (i % 2) {
//奇数は、total計算しない
//goto while_block_endと同じ
continue;
}
fprintf(stdout, "total = %d + %d\n", total, i);
total += i;
//while_block_end:
}
fprintf(stdout, "total = %d\n", total);
}
実行結果

ソースコード
#include <stdio.h>
#include <string.h>
/*
* do while文とcontinue文のサンプル
*/
int main(int argc, char *argv[]) {
int total = 0;
int i = 1;
do {
fprintf(stdout, "loop block start\n");
if (i % 2) {
//奇数は、total計算しない
//goto dowhile_block_endと同じ
continue;
}
fprintf(stdout, "total = %d + %d\n", total, i);
total += i;
//dowhile_block_end:
} while (++i <= 10);
fprintf(stdout, "total = %d\n", total);
}
実行結果

breakとcontinueのまとめ
- for文、while文、do while文、switch文の中で使用する
- for文、while文、do while文、switch文を終了したい時に使用する
- break文の代わりにgoto文で記述することもできる
- break文は、break文を含む最も内側の、繰り返し処理もしくは、switch文だけを終了する
- 繰り返し処理(for文、while文、do while文)の中で使用する
- 繰り返し処理(for文、while文、do while文)の途中で繰り返しブロックの最後に進みたい時に使用する
- continue文の代わりに、goto文で記述することもできる
- continue文は、continue文を含む最も内側の、繰り返し処理ブロックの最後に進む
二重、三重に繰り返し処理などが重なっている場合に、全ての繰り返し処理を終了したいとか、最初のループの最後に進みたい時などは、goto文を使用した方が理解しやすいコードが書けることもあるので、goto文の使用も頭の片隅に置いておきましょう