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 (test) { ... }מריץ את החוסמה רק כשהבדיקה נכונה.- הוסף
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;.
טעויות נפוצות
- השתמש ב
==להשוואה; שימוש ב=מגדיר (ומשתנה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, חלק את הציון ב-10 תחילה, ולאחר מכן הפעל switch על התוצאה. - אל תכתוב
main— הבוקר מספק אותו already.
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. · לחץ על הרץ כדי לראות את התוצא כאן.
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. · לחץ על הרץ כדי לראות את התוצא כאן.
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. · לחץ על הרץ כדי לראות את התוצא כאן.