[python]
حكاية “While” والشرط العنيد
كان يا ما كان في قديم الزمان، جملة برمجية اسمها “while”. “while” كانت معروفة بكونها عنيدة جدًا! لن تتحرك خطوة واحدة حتى تتأكد أن شرطًا معينًا قد تحقق.
تخيل معي:
counter = 0
while counter < 5:
print("ما زلت هنا! الرقم هو", counter)
counter += 1
في هذا السيناريو، “while” تقول: “لن أترك هذا المكان إلا إذا كان counter
أقل من 5!”. كل مرة تدور فيها الحلقة، تتفقد “while” هذا الشرط. إذا كان الشرط صحيحًا، تقول: “أوه، ما زلت هنا!” وتستمر في طباعة الرقم. إذا اكتشفت أن counter
لم يعد أقل من 5، تقول: “أخيرًا! يمكنني المغادرة الآن!” وتكسر الحلقة.
إذا كان الشرط دائمًا صحيحًا؟
احذر! إذا كان الشرط دائمًا صحيحًا، فإن “while” قد تصبح كالضيف الذي لا يغادر أبدًا! وستستمر في الدوران إلى الأبد:
while True:
print("لن أذهب أبدًا! 😈")
في هذه الحالة، عليك أن تكون حذرًا وتستخدم “break” لتقول: “حسنًا، كفى! حان وقت المغادرة!”
الخلاصة:
“while” هي أداة رائعة ومفيدة، لكنها تحتاج إلى توجيه جيد. تعامل معها بحذر، ولا تنسَ أن تحدد متى يجب أن تتوقف، وإلا ستعلق في حلقة لا نهائية مع صديقك العنيد!
The Tale of “While” and the Stubborn Condition
Once upon a time, there was a programming statement named “while”. “While” was known for being very stubborn! It wouldn’t move an inch until a certain condition was met.
Imagine this:
counter = 0
while counter < 5:
print("I’m still here! The number is", counter)
counter += 1
In this scenario, “while” says: “I won’t leave until counter
is less than 5!” Every time the loop runs, “while” checks the condition. If the condition is true, it says, “Oh, I’m still here!” and continues printing the number. If it finds that counter
is no longer less than 5, it says, “Finally! I can leave now!” and breaks the loop.
What if the condition is always true?
Be careful! If the condition is always true, “while” can become like a guest who never leaves! It will keep looping forever:
while True:
print("I’m never leaving! 😈")
In this case, you need to be cautious and use “break” to say: “Okay, enough! Time to go!”
Conclusion:
“While” is a great and useful tool, but it needs proper guidance. Handle it carefully, and don’t forget to specify when it should stop, or else you’ll be stuck in an endless loop with your stubborn friend!