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?

はじめに

この記事について

「C言語の基礎を学ぼう」をテーマに、自身の知識 + α をアドベントカレンダーにまとめます。
25日間でC言語をマスターしよう - Qiita Advent Calendar 2025 - Qiita

こんな方を対象としています

  • コンピュータがプログラムをどのように動かしているか知りたい/知らない方

  • プログラミングをしてみたい方

  • C言語初心者の方

キーワード

  • for文

  • while文

  • do-while文

  • break

  • continue

説明

for文

特定の回数同じ処理をしたい場合、繰り返し処理を利用します。
繰り返し処理の1つに for文 があります。

#include <stdio.h>
int main(void) {
    int i;
    for (i = 1; i <= 5; i++) {
        printf("%d回目の処理です。\n", i);
    }
    return 0;
}
1回目の処理です。
2回目の処理です。
3回目の処理です。
4回目の処理です。
5回目の処理です。

for文には3つの式を ; で区切って記述します。

for (式1; 式2; 式3)
  • 式1:for文初回実行時に1回だけ実行される。初期化処理をすることが多い。

  • 式2:真の場合、処理を継続する。初回実行時も判定される。

  • 式3:繰り返し処理1回分が完了したら実行される。加算処理をすることが多い。

while文

while文には、for文の式2にあたる条件式のみ記述します。

#include <stdio.h>
int main(void) {
    int i = 1;
    while (i <= 5) {
        printf("%d回目の処理です。\n", i);
        i++;
    }
    return 0;
}
1回目の処理です。
2回目の処理です。
3回目の処理です。
4回目の処理です。
5回目の処理です。

do-while文

do-while文は 1回分の繰り返し処理終了後 に繰り返し判定をします。
必ず1回は実行される 繰り返し処理を実装したいときに便利です。

#include <stdio.h>
int main(void) {
    int i = 1;
    do {
        printf("%d回目の処理です。\n", i);
        i++;
    } while (i <= 5);
    return 0;
}
1回目の処理です。
2回目の処理です。
3回目の処理です。
4回目の処理です。
5回目の処理です。

多重ループ

繰り返し処理内に繰り返し処理を実装することができます。

#include <stdio.h>
int main(void) {
    int i, j;
    for (i = 1; i <= 3; i++) {
        for (j = 1; j <= 3; j++) {
            printf("i:%d j:%d\n", i, j);
        }
    }
    return 0;
}
i:1 j:1
i:1 j:2
i:1 j:3
i:2 j:1
i:2 j:2
i:2 j:3
i:3 j:1
i:3 j:2
i:3 j:3

多重ループを実装すると 各ループ回数の積が全体の処理回数 となります。
処理回数が増えすぎないよう注意が必要です。

無限ループ

条件式が常に真の場合、処理が終了しません。これを無限ループといいます。

int main(void) {
    for (;;) {
        // 無限ループ
    }
    return 0;
}
int main(void) {
    while (1) {
        // 無限ループ
    }
    return 0;
}

無限ループを実行してしまった場合、実行中のターミナルでキーボードの ctrl + c を押すと処理を強制的に止めることができます。

break

繰り返し処理を終了したい場合、 break を使用します。

#include <stdio.h>
int main(void) {
    int i = 1;
    while (1) {
        printf("%d回目の処理です。\n", i);
        i++;
        if (i > 3) {
            break;
        }
    }
    return 0;
}
1回目の処理です。
2回目の処理です。
3回目の処理です。

continue

そのループに限り、以降の処理を行いたくない場合は continue を使用します。

#include <stdio.h>
int main(void) {
    int i;
    for (i = 0; i <= 5; i++) {
        if (i % 2 == 0) {
            continue;
        }
        printf("%d回目の処理です。\n", i);
    }
    return 0;
}
1回目の処理です。
3回目の処理です。
5回目の処理です。

練習

1. 何桁の数?

整数aが何桁か調べよう。

10000は5桁です!

ポイント

1桁でなければ2桁?、2桁でなければ3桁?…のように調べます。

解答例

#include <stdio.h>
int main(void) {
    int a = 10000, i, count = 1;
    for (i = 10; a >= i; i *= 10){
        count++;
    }
    printf("%dは%d桁です!\n", a, count);
    return 0;
}
10000は5桁です!

2. 九九表を作ろう

九九の表を表示しよう。

  1  2  3  4  5  6  7  8  9
  2  4  6  8 10 12 14 16 18
  3  6  9 12 15 18 21 24 27
  4  8 12 16 20 24 28 32 36
  5 10 15 20 25 30 35 40 45
  6 12 18 24 30 36 42 48 54
  7 14 21 28 35 42 49 56 63
  8 16 24 32 40 48 56 64 72
  9 18 27 36 45 54 63 72 81

ポイント

横方向の繰り返しと縦方向の繰り返しを考えます。

解答例

#include <stdio.h>
int main(void) {
    int i, j;
    for (i = 1; i <= 9; i++){
        for (j = 1; j <= 9; j++){
            printf("%3d", i * j);
        }
        printf("\n");
    }
    return 0;
}
  1  2  3  4  5  6  7  8  9
  2  4  6  8 10 12 14 16 18
  3  6  9 12 15 18 21 24 27
  4  8 12 16 20 24 28 32 36
  5 10 15 20 25 30 35 40 45
  6 12 18 24 30 36 42 48 54
  7 14 21 28 35 42 49 56 63
  8 16 24 32 40 48 56 64 72
  9 18 27 36 45 54 63 72 81

3. ガチャシミュレーション

あるゲームでは10000分の1の確率でレアキャラが手に入るという。
何回ガチャを回せばレアキャラが当たるかシミュレーションしよう。

12030回目で出ました!

ポイント

疑似乱数を利用します。

#include <stdio.h>
#include <stdlib.h> // srand()、rand()に必要
#include <time.h> // time()に必要
int main(void) {
    int r;

    // 疑似乱数生成準備としてシード値を設定する。
    // シード値にはプログラム実行時刻を用いることが多い。
    // (実行ごとにランダムにするため)
    srand((unsigned int)time(NULL));

    // rand()で乱数を生成する。
    // 生成した値を加工して欲しい乱数にする。
    // 今回は10000で割ることで0から9999の1万通りの乱数を生成する。
    r = rand() % 10000;

    printf("%d", r);
    return 0;
}
8765

解答例

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void) {
    int i;
    srand((unsigned int)time(NULL));
    for (i = 1; ; i++) {
        if (rand() % 10000 == 0) {
            printf("%d回目で出ました!", i);
            break;
        }
    }
    return 0;
}
5898回目で出ました!

乱数が任意の値と一致するまで繰り返すことで、確率のシミュレーションができます。

おわりに

繰り返し処理と分岐、乱数を合わせると様々なプログラムが書けます。

作ったらおもしろそうと思ったものは作ってみるとよいですね。出来ない部分を調べることで自然とプログラミングの勉強になります。

参考文献

↓↓↓ はじめてプログラミングを学んだときに読んだ本です ↓↓↓
詳細(プログラミング入門 C言語)|プログラミング|情報|実教出版

0
0
0

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?