C/C++

繰り返し処理

更新日:

繰り返し処理とは

同じ内容を複数回繰り返す処理です。
変数を用いることで少しずつ内容を変化させることもできます。

for文

for(【カウンタ変数の初期化】;【条件式】;【ループ毎の処理】)
{
    【繰り返す処理】
}


カウンタ変数とは次の条件式に使われる変数です。
その変数の値を用いた条件式が真の場合ブロック内の処理が繰り返されます。
ループ毎の処理はカウンタ変数を変化させるための処理です。主にインクリメント演算子が使われます。

以下はfor文を用いた例です。この場合printf()関数が5回繰り返されます。

loop_sample1.c

int i;

for(i = 0; i < 5; i++)
{
    printf("%d\n", i);
}

while文

while(【条件式】)
{
    【繰り返す処理】
}


while文は条件式しか渡すものがありません。
そのためwhileブロックの中でループ毎の処理を行う必要があります。

また、while文は最初から条件式が偽の場合1度も繰り返しが実行されません。

以下はwhile文を用いた例です。この場合printf()関数が5回繰り返されます。

loop_sample2.c

int i = 0;

while(i < 5)
{
    printf("%d\n", i);
    i++;
}

do while文

while文に対してdo while文は最初から条件式が偽でも最低1回は繰り返します。

使い方は以下の通りです。

do
{
    【繰り返す処理】
}while(【条件式】);

無限ループ

プログラムを組んでいると、ループ回数を指定しない無限ループが必要になることがあります。 無限ループということは、条件式が常に真ということです。

for(;;)
{
    【繰り返す処理】
}


while(1)
{
    【繰り返す処理】
}


この2種類が代表的な書き方です。

また、無限ループから脱出したいときはbreak文またはcontinue文を使います。
break文はループを脱出し、次の行へと進みます。
continue文はループを脱出し、もう一度ループを開始します。

多重ループ

ループ文の処理内容にループ文を書くことで、多重ループを作ることができます。
例えば「命令Aを5回繰り返す」を10回繰り返すと5*10=50回命令Aが実行されることになります。

それぞれにカウンタ変数を用意するので、2次元配列を初期化する場合などはこれを用いると便利です。

以下のプログラムは多重ループの使用例です。

loop_sample3.c

#include <stdio.h>

int main(void)
{
  int multiplTable[9][9];
  int i, j;

  for(i = 1; i <= 9; i++)
  {
    for(i = j; j <= 9; j++)
    {
      multiplTable[i][j] = i * j;
    }
  }

  for(i = 1; i <= 9; i++)
  {
    for(j = 1; j <= 9; j++)
    {
      printf("%2d ", multiplTable[i][j]);
    }
    printf("\n");
  }
  return 0;
}

九九表を作るプログラムです。
外側のループが行、内側のループが列を格納、表示しています。
表示するだけなら配列は必要ないので多重ループは一つで十分ですが、このプログラムでは二次元配列の初期化も行いたかったので初期化と表示のループを分けています。

サンプルコード

例題として「99 Bottles of Beer」という曲の歌詞を表示するプログラムを作りましょう。
歌詞は以下のようになっています。

99 bottles of beer on the wall, 99 bottles of beer.
Take one down and pass it around, 98 bottles of beer on the wall.

98 bottles of beer on the wall, 98 bottles of beer.
Take one down and pass it around, 97 bottles of beer on the wall.

97 bottles of beer on the wall, 97 bottles of beer.
Take one down and pass it around, 96 bottles of beer on the wall.

(中略)

2 bottles of beer on the wall, 2 bottles of beer.
Take one down and pass it around, 1 bottle of beer on the wall.

1 bottle of beer on the wall, 1 bottle of beer.
Take one down and pass it around, no more bottles of beer on the wall.

No more bottles of beer on the wall, no more bottles of beer.
Go to the store and buy some more, 99 bottles of beer on the wall.

このように99から一つずつ数字が減っていきます。
これを繰り返し処理を用いて記述します。

まずはfor文を使った例です。

loop_sample4.c

#include <stdio.h>

int main(void)
{
	int i;
	char *wordsOfSong1 = " bottles of beer on the wall, ";
        char *wordsOfSong2 = "bottles of beer.\nTake one down and pass it around, ";
        char *wordsOfSong3 = "bottles of beer on the wall.\n";

	for (i = 99; i > 0; i--)
		printf("%d%s%d%s%d%s\n", i, wordsOfSong1, i, wordsOfSong2, i - 1, wordsOfSong3);

	printf("No more%s\n", wordsOfSong);

	return 0;

}

続いてwhile文を使った例です。

loop_sample5.c

#include <stdio.h>

int main(void)
{
	int i;
	char *wordsOfSong1 = " bottles of beer on the wall, ";
        char *wordsOfSong2 = "bottles of beer.\nTake one down and pass it around, ";
        char *wordsOfSong3 = "bottles of beer on the wall.\n";

	i = 99;
	while (i > 0)
	{
		printf("%d%s%d%s%d%s\n", i, wordsOfSong1, i, wordsOfSong2, i - 1, wordsOfSong3);
		i--;
	}

	printf("No more%s\n", wordsOfSong);

	return 0;

}

最後にdo while文を使った例です。

loop_sample6.c

#include <stdio.h>

int main(void)
{
	int i;
	char *wordsOfSong1 = " bottles of beer on the wall, ";
        char *wordsOfSong2 = "bottles of beer.\nTake one down and pass it around, ";
        char *wordsOfSong3 = "bottles of beer on the wall.\n";

	i = 99;

	do
	{
		printf("%d%s%d%s%d%s\n", i, wordsOfSong1, i, wordsOfSong2, i - 1, wordsOfSong3);
		i--;
	} while (i > 0);

	printf("No more%s\n", wordsOfSong);

	return 0;

}

いずれの例文も変数iを99から一つずつ減らしています。
最後は数字ではなく「No more」となるのでそこは繰り返しの外で行っています。条件分岐で0のときだけ変更も可能ですが、毎回分岐の条件判定をしなければならないので処理速度が多少ですが遅くなります。



-C/C++

Copyright© ツナのエンジニアブログ , 2025 All Rights Reserved Powered by AFFINGER5.