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 ไม่มีชนิดข้อมูล true/false แยกต่างหากโดยค่าเริ่มต้น จะใช้ตัวเลขแทน:
0หมายถึง false, และ ตัวเลขอื่นใด หมายถึง true - การทดสอบอย่าง
5 > 3จะให้ผลลัพธ์เป็น1(true) ส่วน5 < 3ให้ผลลัพธ์เป็น0(false) - (หากคุณ
#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จะเป็น true ก็ต่อเมื่อทั้งสองเงื่อนไขเป็น true
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;
}
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;เพื่อไม่ให้ไหลเข้าไปในข้อถัดไป ส่วนdefault:จะจับทุกกรณีที่เหลืออยู่ - การใช้
switchเหมาะสมมากเมื่อต้องการเปรียบเทียบตัวแปรเดียวกันกับค่าคงที่จำนวนมาก
Common mistakes
- Use
==to compare;=assigns (andif (x = 5)is always true). - Each
switchcase needs abreak;.
ข้อผิดพลาดที่พบบ่อย
- ใช้
==เพื่อเปรียบเทียบ;=assigning (และif (x = 5)เป็นจริงเสมอ) - Each
switchcase needs abreak;.
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ให้หารคะแนนด้วย 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) ให้ส่งกลับ -1 เมื่อ n เป็นลบ, ส่งกลับ 0 เมื่อ n เป็นศูนย์, และส่งกลับ 1 เมื่อ n เป็นบวก. ห้าม เขียน main
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
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) ให้ส่งกลับ 1 เมื่อ n อยู่ระหว่าง lo และ hi (รวมขอบเขต), และส่งกลับ 0 ในกรณีอื่น. ใช้ &&. ห้าม เขียน main
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
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. · เติมเต็ม char grade_letter(int score) โดยใช้ switch กับ score / 10. ส่งกลับ 'A' สำหรับคะแนน 90–100, ส่งกลับ 'B' สำหรับช่วง 80, ส่งกลับ 'C' สำหรับช่วง 70, ส่งกลับ 'D' สำหรับช่วง 60, และส่งกลับ 'F' ต่ำกว่า 60. ห้าม เขียน main
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่