• 検索結果がありません。

break隴br/>

N/A
N/A
Protected

Academic year: 2021

シェア "break隴br/>"

Copied!
3
0
0

読み込み中.... (全文を見る)

全文

(1)

1

break

1.1

学習のポイント

switch case文またはループの途中から外に脱出するためのスマートな制御文break に ついて学びます。

 breakはswitchまたはループから強制的に脱出するときに使います。breakはすでに switch∼case文のところで説明していますので、ここではfor、while、do∼whileなどの ループ内から抜ける場合の例を説明します。  ループ中のbreakが実行されると最も内側のループから抜けます。breakにより何重も のネスト(ループの中のループが入る構造)から一気に抜けることはできません。

1.2

書式

while (式) { 文1 if (ある条件なら) break; ・ 文2 }

 ループ内の途中からループ外に脱出したい場合、BASICやFORTRANではgotoを使 わなければなりませんでしたが、Cでは、breakによりスマートに(飛び先のラベルが不 要のため)ループ外へ脱出することができます。 配列を探索する時、プログラムは配列の先頭から最後まで探索しますが、大抵の場合、探 索の途中で解が見つかります。この様なときは、break文を使ってループから抜け出すと 処理が速くなります。 1

(2)

1.3

例題

32

/* reidai32.c 二次元配列の各要素を表示するときに、各行要素に0が現れたら、その行の表示 を中止するようにしなさい。 C言語 132頁 */ #include <stdio.h> int main() {

static int a[4][5] = {{ 1, 2, 3, 4, 0}, { 2, 2, 0, 0, 0}, { 3, 4, 5, 0, 0}, { 5, 0, 0, 0, 0}}; int j, k; for(j = 0; j < 4; j++){ for(k = 0; k < 5; k++){ if(a[j][k] == 0){ break; } printf("%d ", a[j][k]); } printf("\n"); } return 0; } 2

(3)

1.4

練習問題

32

/* rensyu32.c 一次元配列のデータの中から指定データ(key)を見つけ出し、 その位置を表示するプログラムを作りなさい。 C言語 133頁 */ #include <stdio.h> int main() {

static int a[] = { 1, 2, 5, 6, 22, 33, 4, 66, 77, 56}; int i, key;

printf("key data ? "); scanf("%d", &key);

for(i = 0; i < 10; i++){

printf("search point = %d\n", i); if(key == a[i]){ printf("now search\n"); break; } } return 0; } 3

参照

関連したドキュメント

Here, instead of considering an instance I and trying to directly develop a feasible solution for the P, G ∗ |prec; c ij dπ k , π l 1; p i 1|C max problem, we consider a

There is a bijection between left cosets of S n in the affine group and certain types of partitions (see Bjorner and Brenti (1996) and Eriksson and Eriksson (1998)).. In B-B,

We establish sharp Br´ezis-Gallou¨et-Wainger type inequalities in Besov and Triebel-Lizorkin spaces as well as fractional Sobolev spaces on a bounded domain Ω ⊂ R n.. We treat

(The Elliott-Halberstam conjecture does allow one to take B = 2 in (1.39), and therefore leads to small improve- ments in Huxley’s results, which for r ≥ 2 are weaker than the result

[r]

“Breuil-M´ezard conjecture and modularity lifting for potentially semistable deformations after

lines. Notice that Theorem 4 can be reformulated so as to give the mean harmonic stability of the configuration rather than that of the separate foliations. To this end it is

S., Oxford Advanced Learner's Dictionary of Current English, Oxford University Press, Oxford