Iteration: WHILE and REPEAT · التكرار: WHILE و REPEAT
WHILE tests before each pass
- A
WHILEloop checks its test before each pass. - If the test is false at the start, the body runs zero times.
- The block ends at
ENDWHILE.
WHILE يختبر قبل كل دورة
- حلقة
WHILEتختبر شرطها قبل كل دورة. - إذا كان الاختبار خاطئاً في البداية، يعمل الجسم صفراً من المرات.
- ينتهي الكتلة بـ
ENDWHILE.
Count ← 1
WHILE Count <= 3 DO
OUTPUT Count // 1, 2, 3
Count ← Count + 1
ENDWHILE
REPEAT tests after each pass
- A
REPEATloop checks its test after each pass. - So the body always runs at least once.
- It ends at
UNTIL.
REPEAT يختبر بعد كل دورة
- حلقة
REPEATتختبر شرطها بعد كل دورة. - لذلك يعمل الجسم دائماً مرة واحدة على الأقل.
- تنتهي عند
UNTIL.
N ← 0
REPEAT
N ← N + 2
OUTPUT N // 2, 4, 6
UNTIL N >= 6
REPEAT runs, then tests · REPEAT تشغل، ثم تختبر
WHILE tests before (may run 0 times); REPEAT tests after (runs at least once). · تختبر WHILE قبل (قد تعمل 0 مرة)؛ وتختبر REPEAT بعد (تعمل على الأقل مرة واحدة).
Watch out
- Something inside the loop must move the test toward stopping, or it runs forever.
WHILEmay run 0 times;REPEATalways runs at least 1 time.
انتبه
- يجب أن شيء داخل الحلقة يحوّل الاختبار نحو الإيقاف، وإلا ستعمل إلى ما لا نهاية.
- قد يعمل
WHILE0 مرة؛ بينما يعملREPEATدائماً على الأقل 1 مرة.
Common mistakes
WHILEtests the condition first;REPEAT ... UNTILtests after, so it runs at least once.- Change something inside, or the loop never ends.
أخطاء شائعة
WHILEيفحص الشرط أولاً؛REPEAT ... UNTILيفحص بعد ذلك، لذا فهو يعمل مرة واحدة على الأقل.- غيّر شيئاً داخل الحلقة، وإلا فلن تنتهي أبداً.
Now you try
- Pick
WHILEorREPEATfor each task. - Press Check answer to test it.
الآن جرب بنفسك
- اختر
WHILEأوREPEATلكل مهمة. - اضغط على تحقق من الإجابة لاختبارها.
Use a WHILE loop to output 1, 2, 3, one per line. · استخدم حلقة WHILE لإخراج 1، 2، 3، واحد في كل سطر.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
Use a REPEAT loop to output 2, 4, 6, one per line (add 2 each time, stop at 6). · استخدم حلقة REPEAT لإخراج 2، 4، 6، واحد في كل سطر (أضف 2 في كل مرة، توقف عند 6).
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
Use a WHILE loop to add up 1 to 4, then output the total (it is 10). · استخدم حلقة WHILE لجمع 1 إلى 4، ثم أخرج المجموع (وهو 10).
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.