حلقة while
تعرف على الميزات الأساسية واستخدامات حلقات 'while' لتحقيق التكرار الفعال في البرمجة.
سنغطي ما يلي...
سنغطي ما يلي...
The while
loop keeps iterating over a certain set of operations as long as a certain condition holds True
. It operates using the following logic:
While this condition is true, keep the loop running.
Structure
In a for
loop, the number of iterations is fixed since we know the size of the sequence. On the other hand, a while
loop is not always restricted to a fixed range. Its execution is based solely on the condition associated with it.
Press + to interact
حلقة while
في العمل
هذا مثال بسيط لحلقة while
في Python تطبع الأرقام من 1
إلى 5
لاحظ أن جملة while
تنتهي أيضًا بعلامة نقطتين، كما هو الحال مع جملة for
.
Press + to interact
counter = 1while counter <= 5:print(counter)counter += 1
وهنا شرح الكود أعلاه: