انتقل إلى المحتوى
أمين أبوزيد
BEG-012

الشروط

  • 6 درس
  • 65 دقيقة قراءة
  • 4 ساعة تدريب (تقدير)
في هذه الصفحة 8 موضوعات

نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.

نظرة عامة

الموضوعات

  • if
  • else
  • else if
  • switch
  • تعبيرات switch
  • الشروط المتداخلة

تعلّم أولًا

65 دقيقة قراءة · 4 ساعة تدريب (تقدير)

مخرجات التعلم

  1. Write if/else if/else chains that cover every case, including boundaries.
  2. Choose between an if chain, a switch statement, and a switch expression.
  3. Simplify nested conditions using logical operators and early exits.
  4. Identify unreachable or overlapping branches.

الدروس

  1. 01اتخاذ القرارات باستخدام if10 د قراءة
  2. 02سلاسل else if والقيم الحدّية13 د قراءة
  3. 03switch الكلاسيكي والسقوط (fall-through)10 د قراءة
  4. 04تعبيرات switch (Java الحديثة)10 د قراءة
  5. 05ترويض الشروط المتداخلة11 د قراءة
  6. 06عرض عملي: قواعد تكلفة الشحن وحدودها11 د قراءة

المختبر والتمارين

Lab

BEG-012-LAB1 · Shipping cost calculator

Outcomes: 1–4 · Time: ~2 h

  1. Write the rules on paper: cost by weight band, extra cost by zone, discount for members.
  2. Code the calculator with hard-coded inputs first, then read them with Scanner.
  3. Flatten any nesting deeper than two levels.
  4. 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)

IDOutcomeTask
BEG-012-EX011Print "negative", "zero", or "positive" for an int.
BEG-012-EX021Map a score to grades A-F with an else if chain; test 90, 89, 60, 59.
BEG-012-EX032Convert a month number (1-12) to days using a classic switch with grouped cases.
BEG-012-EX042Rewrite EX03 as a switch expression. Which version do you prefer, and why?
BEG-012-EX053Flatten if (a) { if (b) { if (c) { run(); } } } once with && and once with early exits.
BEG-012-EX064Find 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), default for 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(); or if (!a) return; if (!b) return; if (!c) return; run();.
  • EX06 x > 10 can never be reached: any x above 10 is already caught by x > 5 (unreachable branch). In the second chain every x above 100 also satisfies x >= 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.

اختبار ذاتي لك: لا توجد درجات، ولا يُحفظ أو يُرسل أي شيء. اختر إجابة ثم تحقق منها، ويمكنك المحاولة مرة أخرى كما تشاء.

  1. 1The condition inside if (...) must be:O1
  2. 2In an else if chain, Java runs:O1
  3. 3For if (n >= 10), which values are the boundary to test?O1
  4. 4Which fits a switch best?O2
  5. 5A missing break in a classic switch causes:O2
  6. 6In case 6, 7 -> "Weekend" the arrow form:O2
  7. 7if (a) { if (b) { run(); } } equals:O3
  8. 8An early exit helps because it:O3
  9. 9In if (x > 5) {...} else if (x > 10) {...} the second branch is:O4
  10. 10Two branches that can both be true for the same value are:O4

الجزء العملي

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.

اكتب للبحث في المستويات والمراحل والوحدات والدروس.