Booleans and selection: if, else, switch
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · このページには最新のブラウザ(SharedArrayBuffer対応)が必要です。Chrome、Edge、Firefox、Safariを最新バージョンに更新してください。
English
True and false in C
- C has no separate true/false type by default. It uses numbers:
0means false, and any other number means true. - A test like
5 > 3produces1(true).5 < 3produces0(false). - (If you
#include <stdbool.h>you can writebool,true, andfalse— but plainintworks everywhere.)
日本語
C での真偽値
- C にはデフォルトで独立したtrue/false型はありません。数字を使います:
0はfalse、それ以外の数はtrueです。 5 > 3のようなテストは1(true) を生成します。5 < 3は0(false) を生成します。- (
#include <stdbool.h>ならbool、true、falseを書けますが、標準のintはどこでも機能します。)
English
Comparing and combining
- Compare values with
==(equal),!=(not equal),<,>,<=,>=. - Be careful:
==tests equality; a single=assigns a value. They are different! - Combine tests with
&&(and),||(or), and!(not).a && bis true only when both are true.
日本語
比較と結合
- 値を
==(等しい)、!=(等しくない)、<、>、<=、>=で比較します。 - 注意:
==は等価性をテストしますが、単一の=は値を代入します。これらは異なります! &&(and)、||(or)、!(not) でテストを結合します。a && bは両方がtrueの場合にのみtrueです。
English
if / else if / else
if (test) { ... }runs the block only when the test is true.- Add
else if (...)to check another test, andelsefor "none of the above". - Only the first matching branch runs.
日本語
if / else if / else
if (test) { ... }はテストがtrueのときだけブロックを実行します。- 別のテストを確認するために
else if (...)を追加し、「上記以外」にはelseを使います。 - 一致するブランチは最初のもののみが実行されます。
#include <stdio.h>
int main(void) {
int score = 72;
if (score >= 90) {
printf("A\n");
} else if (score >= 60) {
printf("Pass\n"); // this one runs
} else {
printf("Fail\n");
}
return 0;
}
English
switch: choosing among many values
- A
switchpicks a branch based on one value (anintorchar). - Each
caseends withbreak;so it does not fall into the next one.default:catches everything else. switchis neat when you compare the same variable against many fixed values.
日本語
switch: 多くの値の中から選ぶ
switchは1つの値(intまたはchar)に基づいてブランチを選びます。- 各
caseはbreak;で終わるため、次の分支に漏れません。default:は残りのすべてをキャッチします。 - 同一の変数を多くの固定値と比較する場合、
switchは整理されています。
English
Common mistakes
- Use
==to compare;=assigns (andif (x = 5)is always true). - Each
switchcase needs abreak;.
日本語
よくあるミス
- 比較には
==を使い、=は代入します(if (x = 5)は常にtrueです)。 - 各
switchのケースにはbreak;が必要です。
English
Now you try
- Use comparisons and
&&/||to build your tests. Remember0is false, anything else is true. - For the
switchtask, divide the score by 10 first, then switch on the result. - Do not write a
main— the checker provides one.
日本語
あなたも試してみよう
- 比較と
&&/||を使ってテストを構築してください。0はfalse、それ以外はtrueであることを覚えてください。 switchタスクでは、スコアを10で割り、結果に対してswitchを実行します。mainを書かないでください;チェックツールが用意します。
Explore · 探索
if / else if / else
Conditions test top-down; the first true block runs.
Complete int sign(int n) so it returns -1 when n is negative, 0 when n is zero, and 1 when n is positive. Do not write a main.
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。
Complete int in_range(int n, int lo, int hi) so it returns 1 when n is between lo and hi (inclusive), and 0 otherwise. Use &&. Do not write a main.
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。
Complete char grade_letter(int score) using a switch on score / 10. Return 'A' for 90–100, 'B' for the 80s, 'C' for the 70s, 'D' for the 60s, and 'F' below 60. Do not write a main.
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。