Booleans and selection: if, else, switch · 布尔与选择:if、else、switch
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 没有单独的真/假类型。它用数字:
0表示假,任何其他数字表示真。 - 像
5 > 3这样的判断会得到1(真)。5 < 3会得到0(假)。 - (如果你
#include <stdbool.h>,就能写bool、true、false—— 但普通的int在哪里都能用。)
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.
比较与组合
- 用
==(等于)、!=(不等于)、<、>、<=、>=来比较值。 - 小心:
==判断是否相等;单个=是赋值。两者不一样! - 用
&&(与)、||(或)、!(非)来组合判断。a && b只有在两者都为真时才为真。
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 (判断) { ... }只在判断为真时运行这个代码块。- 加
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;
}
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根据一个值(int或char)来选择分支。- 每个
case用break;结尾,这样它就不会掉进下一个case。default:接住其他所有情况。 - 当你要把同一个变量和许多固定的值比较时,
switch很清爽。
Common mistakes
- Use
==to compare;=assigns (andif (x = 5)is always true). - Each
switchcase needs abreak;.
常见错误
- 比较用
==;=是赋值(而if (x = 5)永远为真)。 - 每个
switch分支都要break;。
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是假,其他都是真。 - 在
switch任务里,先把 score 除以 10,再对结果做 switch。 - 不要自己写
main—— 检查器会提供。
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. · 完成 int sign(int n):当 n 为负数时返回 -1,为零时返回 0,为正数时返回 1。不要写 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. · 完成 int in_range(int n, int lo, int hi):当 n 在 lo 和 hi 之间(含端点)时返回 1,否则返回 0。使用 &&。不要写 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. · 用对 score / 10 的 switch 完成 char grade_letter(int score)。90–100 返回 'A',80 多返回 'B',70 多返回 'C',60 多返回 'D',低于 60 返回 'F'。不要写 main。
Click Run to see the output here. · 点击“运行”查看此处输出。