الحلقات التكرارية
في هذه الصفحة 8 موضوعات
نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.
نظرة عامة
الموضوعات
- for
- for المحسّنة
- while
- do-while
- break
- continue
- الحلقات المتداخلة
تعلّم أولًا
70 دقيقة قراءة · 5 ساعة تدريب (تقدير)
مخرجات التعلم
- Choose
for,while, ordo-whilefor a given task and justify the choice. - Write loops with correct start, condition, and update, avoiding off-by-one and infinite loops.
- Use
breakandcontinueappropriately. - Write nested loops for grid-style problems and estimate how many times the body runs.
الدروس
المختبر والتمارين
Lab
BEG-013-LAB1 · Number guessing game (mini-project 1 of 3)
Outcomes: 1–4 · Time: ~3 h
- Pick a random target between 1 and 100.
- Allow a limited number of attempts (for example 7); give higher/lower hints.
- Reject input that is not a number or is out of range without losing an attempt.
- Report the number of attempts used at the end.
- Ask "play again?" and replay without restarting the program.
- Step through one game in the debugger.
Acceptance: handles invalid input, reports attempts, replay works.
Exercises (assessment: seven exercises plus the guessing game)
| ID | Outcome | Task |
|---|---|---|
| BEG-013-EX01 | 1 | For each task pick for, while, or do-while and justify: print 1-10; read until "quit"; a menu shown at least once. |
| BEG-013-EX02 | 2 | Print the sum of 1 to 100 with a for loop. |
| BEG-013-EX03 | 2 | This loop never ends. Fix it: int i = 0; while (i < 10) { System.out.println(i); } |
| BEG-013-EX04 | 2 | Print the numbers 10 down to 1 with no off-by-one error. |
| BEG-013-EX05 | 3 | Print 1-20, skipping multiples of 3 and stopping at the first number above 15. |
| BEG-013-EX06 | 4 | Print a 5-row triangle of stars. |
| BEG-013-EX07 | 4 | Print 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.
اختبار ذاتي لك: لا توجد درجات، ولا يُحفظ أو يُرسل أي شيء. اختر إجابة ثم تحقق منها، ويمكنك المحاولة مرة أخرى كما تشاء.
الجزء العملي
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).