While loops · حلقات While
Loop while a condition is true
- A
whileloop repeats as long as its condition is true. - It checks the condition before each pass.
- Use it when you do not know the number of repeats in advance.
التكرار طالما الشرط صحيح
- حلقة
whileتكرر طالما كان شرطها صحيحاً. - تفحص الشرط قبل كل دورة.
- استخدمها عندما لا تعرف عدد التكرارات مسبقاً.
Always change the condition
- Something inside the loop must move toward making the condition false.
- If it never becomes false, the loop runs forever (an infinite loop).
- Press Stop if a loop ever gets stuck.
تغيير الشرط دائماً
- يجب أن يغير شيء ما داخل الحلقة toward جعل الشرط خاطئاً.
- إذا لم يصبح خاطئاً أبداً، ستعمل الحلقة إلى الأبد (حلقة لا نهائية).
- اضغط إيقاف إذا عالقت الحلقة.
count = 3
while count > 0:
print(count)
count = count - 1
break and continue
breakstops the loop right away.continueskips the rest of this pass and goes to the next one.while True:loops forever until abreakends it.
break و continue
breakيوقف الحلقة فوراً.continueيتجاوز باقي هذه الدورة وينتقل إلى الدورة التالية.while True:تستمر إلى الأبد حتى ينهيهاbreak.
n = 0
while True:
n = n + 1
if n == 4:
break
print(n)
Checking before or after
- A
whileloop checks first, so it may run zero times. - A "repeat until" loop checks after, so it always runs at least once.
- Python has no
repeat; usewhile True:with abreakat the end.
التحقق قبل أو بعد
- حلقة
whileتفحص أولاً، لذا قد تعمل صفراً مرة. - حلقة "كرر حتى" تفحص بعد، لذا تعمل دائماً مرة واحدة على الأقل.
- Python ليس لديها
repeat؛ استخدمwhile True:معbreakفي النهاية.
In Cambridge pseudocode
whileis the pre-condition loop;REPEAT … UNTILis the post-condition loop.
في الرمز الوهمي لكامبريدج
whileهي حلقة الشرط المسبق؛REPEAT … UNTILهي حلقة الشرط اللاحق.
Count ← 3
WHILE Count > 0
OUTPUT Count
Count ← Count - 1
ENDWHILE
REPEAT
OUTPUT Count
Count ← Count + 1
UNTIL Count = 3
Common mistakes
- Change something inside the loop so the condition can become false, or it runs forever.
- A
whilechecks its condition first, so it may run zero times.
أخطاء شائعة
- غيّر شيئاً داخل الحلقة حتى يصبح الشرط خاطئاً، أو ستعمل إلى الأبد.
- A
whileيتحقق من شرطه أولاً، لذا قد تشغّل صفراً مرة.
Now you try
- Make sure your condition will eventually become false.
- Stuck on the loop? Do the short drill Finish the countdown first. It is one missing line.
- Press Check answer to test your code.
الآن جرب بنفسك
- تأكد من أن شرطك سيصبح خاطئاً في النهاية.
- عالقة في الحلقة؟ قم بالتمرين القصير إنهاء العد التنازلي أولاً. إنه سطر مفقود واحد.
- اضغط على تحقق من الإجابة لاختبار الكود الخاص بك.
A while loop builds up a value · حلقة while تبني قيمة
A while loop repeats until its condition is false. · تكرر حلقة while حتى يصبح شرطها غير صحيح.
Use a while loop to count down. Each turn, print the value of count, then make count one smaller. Stop when it reaches 0, so the output is 5, 4, 3, 2, 1 — one per line. · استخدم حلقة while للعد التنازلي. في كل دورة، اطبع قيمة count، ثم قلل count بمقدار واحد. توقف عندما يصل إلى 0، بحيث يكون الناتج 5، 4، 3، 2، 1 — واحد في كل سطر.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
Use a while loop to add up the numbers 1 to 10. Store the total in total (the answer is 55). · استخدم حلقة while لجمع الأرقام من 1 إلى 10. احفظ المجموع في total (الجواب هو 55).
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
Start n at 1 and add 1 each pass. The loop stops too soon: it ends while n is still 1. Change the condition so it keeps going while n * n is at most 50. When it stops, n is the first whole number whose square is over 50. · ابدأ n عند 1 وأضف 1 في كل مرور. تتوقف الحلقة مبكرًا: تنتهي بينما n لا يزال 1. غيّر الشرط ليستمر ما دامت n * n أقل من أو تساوي 50. عند التوقف، n هو أول عدد صحيح مربعه أكبر من 50.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
Same answer, with break. n starts at 1 inside while True:. The if is already inside the loop and n grows after it. Change the condition so you break when n * n is greater than 50. Leave the answer in n (it is 8). Do not add 1 before the test — that makes the answer 9. · نفس الجواب، مع break. يبدأ n عند 1 داخل while True:. الـ if موجود بالفعل داخل الحلقة وتزداد n بعد ذلك. غيّر الشرط بحيث ⌈⌊ break ⟩⌋ عندما تكون n * n أكبر من 50. اترك الجواب في n (وهو 8). لا تضيف 1 قبل الاختبار — لأن ذلك يجعل الجواب 9.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
Start with n = 1 and steps = 0. While n is smaller than 1000, double n and add 1 to steps. When the loop ends, n is 1024. How many doublings did it take? Leave the answer in steps. · ابدأ بـ n = 1 وsteps = 0. طالما n أصغر من 1000، ضاعف n وأضف 1 إلى steps. عند انتهاء الحلقة، تكون قيمة n هي 1024. كم عدد المضاعفات التي استغرقها؟ اترك الجواب في steps.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.