|
1 | | -# 4.1 if文 |
| 1 | +# [WIP] 4.1 if文 |
2 | 2 |
|
3 | 3 | ## 4.1.1 if |
4 | 4 |
|
| 5 | +特定の条件を満たす時だけ実行したい時は、if 文を用いる。if の後に条件を記し、その後の `{}` に条件を満たしていた時だけ実行したい命令を記述する。 |
| 6 | + |
| 7 | +以下は、入力された点数が60点以上であれば「合格」、そうでなければ「不合格」と表示するプログラムである。 |
| 8 | + |
| 9 | +```cpp:line-numbers |
| 10 | +#include <iostream> |
| 11 | +using namespace std; |
| 12 | +
|
| 13 | +int main() { |
| 14 | + cout << "点数を入力してください。" << endl; |
| 15 | + int score = 0; |
| 16 | + cin >> score; |
| 17 | +
|
| 18 | + if (score >= 60) { |
| 19 | + cout << "合格です。" << endl; |
| 20 | + } |
| 21 | + if (score < 60) { |
| 22 | + cout << "不合格です。" << endl; |
| 23 | + } |
| 24 | +} |
| 25 | +``` |
| 26 | + |
| 27 | +フローチャートで表すと以下のようになる。 |
| 28 | + |
| 29 | +```mermaid |
| 30 | +flowchart LR |
| 31 | + start[Start] --> condover{{"score >= 60?"}} |
| 32 | + condover --> |Yes| printover["合格"] |
| 33 | + printover --> condunder |
| 34 | + condover --> |No| condunder{{"score < 60?"}} |
| 35 | + condunder --> |Yes| printunder["不合格"] |
| 36 | + printunder --> e |
| 37 | + condunder --> |No| e[End] |
| 38 | +``` |
| 39 | + |
| 40 | +`score >= 60` は、 $score \geq 60$ と同じである。 `+-/*` と同じくして、条件を記述する演算子も存在する。演算子は以下の通り。 |
| 41 | + |
| 42 | +| 演算子 | 数学の記号 | |
| 43 | +|------|--------| |
| 44 | +| `>=` | $\geq$ | |
| 45 | +| `>` | $>$ | |
| 46 | +| `<` | $<$ | |
| 47 | +| `<=` | $\leq$ | |
| 48 | +| `==` | $=$ | |
| 49 | +| `!=` | $\neq$ | |
| 50 | + |
| 51 | +:::warning |
| 52 | +`==` と `=` を混同しないように注意。 `=` は**代入** 、 `==` が**等価** である。 |
| 53 | +::: |
| 54 | + |
5 | 55 | ## 4.1.2 else |
6 | 56 |
|
| 57 | +3.1.1. で示した例は、60点以上「でない」ときを `if (score < 60)` と記述することで実装した。ただ、実際には「そうでない時」を else |
| 58 | +文によって簡単に記述できる。 |
| 59 | + |
| 60 | +```cpp:line-numbers |
| 61 | +#include <iostream> |
| 62 | +using namespace std; |
| 63 | +
|
| 64 | +int main() { |
| 65 | + cout << "点数を入力してください。" << endl; |
| 66 | + int score = 0; |
| 67 | + cin >> score; |
| 68 | +
|
| 69 | + if (score >= 60) { |
| 70 | + cout << "合格です" << endl; |
| 71 | + } else { |
| 72 | + cout << "不合格です" << endl; |
| 73 | + } |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +::: tip |
| 78 | +else 文は、if文の終わり( = `}` )の次に書く必要がある。 |
| 79 | +::: |
| 80 | + |
| 81 | +```mermaid |
| 82 | +flowchart LR |
| 83 | + start[Start] --> condover{{"score >= 60?"}} |
| 84 | + condover --> |Yes| printover("合格") |
| 85 | + printover --> e[End] |
| 86 | + condover --> |else| printunder("不合格") |
| 87 | + printunder --> e |
| 88 | +``` |
| 89 | + |
7 | 90 | ## 4.1.3 else if |
8 | 91 |
|
| 92 | +3.1.2. のコード に「満点だったら」という条件を足す。 |
| 93 | + |
| 94 | +```cpp:line-numbers |
| 95 | +#include <iostream> |
| 96 | +using namespace std; |
| 97 | +
|
| 98 | +int main() { |
| 99 | + cout << "点数を入力してください。" << endl; |
| 100 | + int score = 0; |
| 101 | + cin >> score; |
| 102 | +
|
| 103 | + if (score == 100) { |
| 104 | + cout << "満点です" << endl; |
| 105 | + } else { |
| 106 | + if (score >= 60) { |
| 107 | + cout << "合格です" << endl; |
| 108 | + } else { |
| 109 | + cout << "不合格です" << endl; |
| 110 | + } |
| 111 | + } |
| 112 | +} |
| 113 | +``` |
| 114 | + |
| 115 | +```mermaid |
| 116 | +flowchart LR |
| 117 | + start[Start] --> condperfect{{"score == 100?"}} |
| 118 | + condperfect --> |Yes| printperfect("満点") |
| 119 | + printperfect --> e[End] |
| 120 | + condperfect --> |else| condover{{"score >= 60?"}} |
| 121 | + condover --> |Yes| printover("合格") |
| 122 | + condover --> |else| printunder("不合格") |
| 123 | + printover --> e |
| 124 | + printunder --> e |
| 125 | +``` |
| 126 | + |
| 127 | +ただ、このように書くのは冗長なので、`else if` と短縮する事が許されている。 |
| 128 | + |
| 129 | +```cpp:line-numbers |
| 130 | +#include <iostream> |
| 131 | +using namespace std; |
| 132 | +
|
| 133 | +int main() { |
| 134 | + cout << "点数を入力してください。" << endl; |
| 135 | + int score = 0; |
| 136 | + cin >> score; |
| 137 | +
|
| 138 | + if (score == 100) { |
| 139 | + cout << "満点です" << endl; |
| 140 | + } else if (score >= 60) { |
| 141 | + cout << "合格です" << endl; |
| 142 | + } else { |
| 143 | + cout << "不合格です" << endl; |
| 144 | + } |
| 145 | +} |
| 146 | +``` |
| 147 | + |
| 148 | +こちらの方が、若干ではあるがコードを読みやすいと感じるだろう。 |
| 149 | + |
9 | 150 | ## 4.1.4 変数のスコープ |
| 151 | + |
| 152 | +変数が使える範囲には制限があり、これを変数のスコープと呼ぶ。 |
| 153 | + |
| 154 | +具体的には、 `{}` の外からは変数にアクセスできない。 |
| 155 | + |
| 156 | +例えば次のソースコードで言えば、 変数 `z` は 4 ~ 6 行目でしか使用できない。 |
| 157 | +**8 行目は正しく実行できない。(コンパイルエラーとなる)** |
| 158 | + |
| 159 | +変数 `x` は 1行目から 9 行目まで好きなところで使用できる。 |
| 160 | + |
| 161 | +```cpp:line-numbers |
| 162 | +int x = 8; |
| 163 | +
|
| 164 | +if (x < 10) { |
| 165 | + int z = 10; |
| 166 | + cout << z << endl; // OK ! |
| 167 | + cout << x << endl; // OK ! |
| 168 | +} |
| 169 | +cout << z << endl; // NG |
| 170 | +cout << x << endl; // OK ! |
| 171 | +``` |
0 commit comments