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، ثم انتقل إلى حالة النتيجة. - لا تكتب
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. · اضغط تشغيل لرؤية المخرجات هنا.
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. · اضغط تشغيل لرؤية المخرجات هنا.