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

الحلقات التكرارية

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

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

نظرة عامة

الموضوعات

  • for
  • for المحسّنة
  • while
  • do-while
  • break
  • continue
  • الحلقات المتداخلة

تعلّم أولًا

70 دقيقة قراءة · 5 ساعة تدريب (تقدير)

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

  1. Choose for, while, or do-while for a given task and justify the choice.
  2. Write loops with correct start, condition, and update, avoiding off-by-one and infinite loops.
  3. Use break and continue appropriately.
  4. Write nested loops for grid-style problems and estimate how many times the body runs.

الدروس

  1. 01لماذا الحلقات: التكرار دون نسخ ولصق10 د قراءة
  2. 02حلقة for10 د قراءة
  3. 03while وdo-while10 د قراءة
  4. 04break وcontinue والتحكم في الحلقة10 د قراءة
  5. 05الحلقات المتداخلة10 د قراءة
  6. 06أخطاء الإزاحة بواحد والحلقات اللانهائية10 د قراءة
  7. 07عرض عملي: لعبة تخمين الرقم مع تتبّع الحلقة في المصحّح (debugger)10 د قراءة

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

Lab

BEG-013-LAB1 · Number guessing game (mini-project 1 of 3)

Outcomes: 1–4 · Time: ~3 h

  1. Pick a random target between 1 and 100.
  2. Allow a limited number of attempts (for example 7); give higher/lower hints.
  3. Reject input that is not a number or is out of range without losing an attempt.
  4. Report the number of attempts used at the end.
  5. Ask "play again?" and replay without restarting the program.
  6. Step through one game in the debugger.

Acceptance: handles invalid input, reports attempts, replay works.

Exercises (assessment: seven exercises plus the guessing game)

IDOutcomeTask
BEG-013-EX011For each task pick for, while, or do-while and justify: print 1-10; read until "quit"; a menu shown at least once.
BEG-013-EX022Print the sum of 1 to 100 with a for loop.
BEG-013-EX032This loop never ends. Fix it: int i = 0; while (i < 10) { System.out.println(i); }
BEG-013-EX042Print the numbers 10 down to 1 with no off-by-one error.
BEG-013-EX053Print 1-20, skipping multiples of 3 and stopping at the first number above 15.
BEG-013-EX064Print a 5-row triangle of stars.
BEG-013-EX074Print the multiplication table 1-9 and state how many times the body runs.
أظهر الحلول
  • EX01 for (known count); while (unknown count, may run zero times); do-while (at least once).
  • EX02 int sum = 0; for (int i = 1; i <= 100; i++) sum += i; result 5050.
  • EX03 Add i++; inside the body.
  • EX04 for (int i = 10; i >= 1; i--).
  • EX05 for (int i = 1; i <= 20; i++) { if (i > 15) break; if (i % 3 == 0) continue; System.out.println(i); }
  • EX06 for (int r = 1; r <= 5; r++) { for (int c = 1; c <= r; c++) System.out.print("*"); System.out.println(); }
  • EX07 Nested loops 1..9 by 1..9; the body runs 81 times.
  • The guessing game and quiz also cover outcomes 2 and 3 further.

الاختبار

Outcomes: O1 choosing a loop · O2 correct loop parts · O3 break/continue · O4 nested loops.

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

  1. 1Best loop when you know the exact count:O1
  2. 2Which loop always runs its body at least once?O1
  3. 3How many times does for (int i = 0; i < 5; i++) run?O2
  4. 4while (x < 10) { print(x); } with no change to x is:O2
  5. 5An off-by-one error often comes from mixing up:O2
  6. 6break inside a loop:O3
  7. 7continue inside a loop:O3
  8. 8In nested loops, break leaves:O3
  9. 9Outer loop 4 times, inner loop 5 times: the inner body runsO4
  10. 10To print a grid of rows and columns you use:O4

الجزء العملي

Seven exercises BEG-013-EX01 to EX07 plus the guessing game. Pass: at least six exercises correct and a working guessing game that meets every acceptance point.

أخطاء شائعة

Off-by-one boundaries; modifying the loop variable in the body; forgetting to update the while condition.

التقييم

7 exercises plus the guessing game.

الصلة بالمسار الوظيفي

Iteration cost leads into complexity (BEG-039).

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