0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

C言語におけるループ処理の簡単なまとめ

Last updated at Posted at 2020-02-26

Cには全部で3つのループ文が用意されている

  • for文 : 回数指定
  • while文 : 条件指定(先判定)
  • do~while文 : 条件指定(後判定)

回数指定ループ(for文)

for文 : 回数指定

int i;
for (i = 1;i <= 繰り返し回数;i++) {
繰り返す文;
}
ループを強制終了させる

これは全てのループ処理で使える。

break;

条件指定ループ(while文とdo~while文)

while文 : 条件指定(先判定)

  • 実行前に条件式を判定する
while (条件式) {
繰り返す文;
}

forのように使いたいときは下記のように書く

初期化;
while (条件式) {
	  繰り返す文;
	  更新;
}

do~while文 : 条件指定(後判定)

  • 実行後に条件を判定する

    • 必ず一度実行したい場合はこちらを使用する
  • 入力チェックのときに威力を発揮する。間違っていたら再入力させるときに

do {
繰り返す文;
} while (条件式);
0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?