الشروط
في هذه الصفحة 8 موضوعات
نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.
نظرة عامة
الموضوعات
- if
- else
- else if
- switch
- تعبيرات switch
- الشروط المتداخلة
تعلّم أولًا
65 دقيقة قراءة · 4 ساعة تدريب (تقدير)
مخرجات التعلم
- Write
if/else if/elsechains that cover every case, including boundaries. - Choose between an
ifchain, aswitchstatement, and a switch expression. - Simplify nested conditions using logical operators and early exits.
- Identify unreachable or overlapping branches.
الدروس
المختبر والتمارين
Lab
BEG-012-LAB1 · Shipping cost calculator
Outcomes: 1–4 · Time: ~2 h
- Write the rules on paper: cost by weight band, extra cost by zone, discount for members.
- Code the calculator with hard-coded inputs first, then read them with
Scanner. - Flatten any nesting deeper than two levels.
- Build a test table and work out every expected result by hand before running the code.
Acceptance: a table of at least 10 test inputs with expected and actual results, including every weight boundary.
Exercises (assessment: six exercises)
| ID | Outcome | Task |
|---|---|---|
| BEG-012-EX01 | 1 | Print "negative", "zero", or "positive" for an int. |
| BEG-012-EX02 | 1 | Map a score to grades A-F with an else if chain; test 90, 89, 60, 59. |
| BEG-012-EX03 | 2 | Convert a month number (1-12) to days using a classic switch with grouped cases. |
| BEG-012-EX04 | 2 | Rewrite EX03 as a switch expression. Which version do you prefer, and why? |
| BEG-012-EX05 | 3 | Flatten if (a) { if (b) { if (c) { run(); } } } once with && and once with early exits. |
| BEG-012-EX06 | 4 | Find the problem in if (x > 5) {...} else if (x > 10) {...} and in a chain that tests x >= 0 then x > 100. |
أظهر الحلول
- EX01
if (n < 0) ... else if (n == 0) ... else .... - EX02 Order from highest:
>= 90,>= 80, and so on. 90 gives A, 89 gives B, 60 gives D, 59 gives F. - EX03
case 4: case 6: case 9: case 11: days = 30; break;and so on; February is 28 (or 29),defaultfor the rest. - EX04
int days = switch (m) { case 4, 6, 9, 11 -> 30; case 2 -> 28; default -> 31; };Preference is open; the arrow form is shorter and has no fall-through. - EX05
if (a && b && c) run();orif (!a) return; if (!b) return; if (!c) return; run();. - EX06
x > 10can never be reached: any x above 10 is already caught byx > 5(unreachable branch). In the second chain every x above 100 also satisfiesx >= 0, so the second test is never reached for the values it was meant for. - Outcome 4 is also covered by quiz Q09 and Q10 and by the lab test table.
الاختبار
Outcomes: O1 if chains · O2 choosing if/switch · O3 simplifying · O4 unreachable/overlap.
اختبار ذاتي لك: لا توجد درجات، ولا يُحفظ أو يُرسل أي شيء. اختر إجابة ثم تحقق منها، ويمكنك المحاولة مرة أخرى كما تشاء.
الجزء العملي
Six exercises BEG-012-EX01 to EX06 plus the shipping lab with its test table. Pass: at least five exercises correct and the test table covering all boundaries.
أخطاء شائعة
Missing break in classic switch; gaps or overlaps at boundaries (< vs <=); deep nesting.
التقييم
6 exercises.
الصلة بالمسار الوظيفي
Business rules in every service; switch expressions return with sealed types in JNR-001.