Lesson 14 / الدرس 14
Leaving a loop early: break and continue / مغادرة الحلقة مبكرًا: break وcontinue
Two words that change how a loop runs. One stops it completely, the other skips to the next round — and knowing them turns several awkward loops into simple ones.
كلمتان تغيّران كيف تعمل الحلقة. إحداهما توقفها تمامًا والأخرى تقفز إلى الدورة التالية — ومعرفتهما تحوّل عدة حلقات محرجة إلى بسيطة.
for (const order of orders) {
if (order.cancelled) {
continue; skip THIS round, carry on with the next
}
if (order.id === wanted) {
found = order;
break; stop the loop entirely
}
total = total + order.amount;
}
continue means "not this one"; break means "I am done". The second is what makes searching efficient — once you have found the thing, there is no reason to look at the remaining ten thousand. Run the demonstration.
continue تعني "ليس هذا"؛ وbreak تعني "انتهيت". والثانية هي ما يجعل البحث فعّالًا — فما إن تجد الشيء حتى لا يبقى سبب للنظر في العشرة آلاف الباقية. شغّل العرض.The shape they replace
Without continue, skipping a case means wrapping the entire rest of the loop body in an if — and then the next exception wraps it again, and after three of them the real work is indented so far that nobody can see what the loop actually does. continue lets each exception be one flat check at the top, in the order you would say them out loud.
continue، يعني تخطي حالة لفّ بقية متن الحلقة كلها في if — ثم يلفّه الاستثناء التالي مرة أخرى، وبعد ثلاثة منها يصير العمل الحقيقي مزاحًا إلى حد لا يرى فيه أحد ما تفعله الحلقة فعلًا. وcontinue يتيح لكل استثناء أن يكون فحصًا مسطحًا في الأعلى، بالترتيب الذي كنت ستقوله بصوت عالٍ.Which one, and one they cannot do
| You want to | Use |
|---|---|
| Ignore some items and keep going | continue |
| Stop as soon as you find one | break |
| Stop and remember what you found | break, after storing it |
| Leave a function entirely | return — lesson 19 |
| Leave both loops in a nested pair | A flag, or move it into a function |
The last row is worth knowing: break only leaves the loop it is in, so in a nested pair it escapes the inner one and the outer one carries on. If you need to leave both, the cleanest answer is almost always to move the inner loop into a function and return.
break لا يغادر إلا الحلقة التي هو فيها، ففي زوج معشش يهرب من الداخلية وتستمر الخارجية. وإن احتجت مغادرة الاثنتين، فأنظف جواب دائمًا تقريبًا نقل الحلقة الداخلية إلى دالة وreturn.continue في حلقة while طريقة كلاسيكية لتعليق المتصفح. فإن كان السطر الذي يزيد العدّاد بعد continue، تُخطّي معه كل شيء آخر — فلا يتحرك العدّاد ولا يصير الشرط خطأً ويتجمّد اللسان. وفي حلقة for لا يمكن أن يحدث هذا، لأن الزيادة في الترويسة وتعمل دائمًا. وهو من أفضل أسباب تفضيل for.
One thing to know before lesson 16: for finding, filtering and totalling a list, JavaScript has purpose-built methods that read better than any loop — find, filter, reduce. They are not a replacement for understanding loops, because they are loops underneath and you will still write plain ones. But once you know them, a break-based search is often the longer way to say find.
find وfilter وreduce. وليست بديلًا عن فهم الحلقات، لأنها حلقات تحتها وستظل تكتب عادية. لكن ما إن تعرفها حتى يصير البحث بـbreak غالبًا الطريقة الأطول لقول find.continue أو أكثر في الأعلى، فتلك غالبًا إشارة إلى أن الترشيح والعمل عملان منفصلان. فترشيح القائمة أولًا ثم الدوران على ما بقي يُقرأ أفضل عادةً، ويتيح لك طباعة القائمة المرشَّحة لترى هل كان الترشيح صحيحًا — وهو ما تخفيه حلقة مدمجة واحدة.Try it live / جرّب بنفسك
Check yourself / اختبر نفسك
1.
What is the difference between break and continue?
break وcontinue؟"I am done" against "not this one". break is what makes a search efficient — no reason to check the remaining items.
break هو ما يجعل البحث فعّالًا — فلا سبب لفحص العناصر الباقية.
2.
Why can continue in a while loop freeze the browser?
continue في حلقة while المتصفح؟In a for loop the increment is in the header and always runs, which is one of the better reasons to prefer it.
for تكون الزيادة في الترويسة وتعمل دائمًا، وهو من أفضل أسباب تفضيلها.
3.
A break inside two nested loops leaves which?
break داخل حلقتين معششتين يغادر أيهما؟To leave both, the cleanest answer is usually to move the inner loop into a function and return from it.
return منها.Score / النتيجة: 0 / 3
Your task / مهمتك
Take a list of at least fifteen numbers containing some zeros, some negatives and one value over a hundred. Write one loop that totals it while skipping the zeros and negatives with continue, and a second that stops at the first value over a hundred with break — printing how many items it had to check. Then rewrite the first loop as a filter followed by a plain loop and say which reads better.
خذ قائمة من خمسة عشر رقمًا على الأقل فيها أصفار وسوالب وقيمة واحدة فوق المئة. واكتب حلقة تجمعها متخطية الأصفار والسوالب بـcontinue، وثانية تتوقف عند أول قيمة فوق المئة بـbreak — مع طباعة كم عنصرًا اضطرت لفحصه. ثم أعد كتابة الحلقة الأولى ترشيحًا يتبعه حلقة عادية وقل أيهما يُقرأ أفضل.
- A list with zeros, negatives and one large value قائمة فيها أصفار وسوالب وقيمة كبيرة
- continue used to skip, with each skip printed continue مستخدم للتخطي، وكل تخطٍّ مطبوع
- break used to stop, with the check count printed break مستخدم للإيقاف، وعدد الفحوص مطبوع
- Both versions of the first loop, with a judgement صيغتا الحلقة الأولى مع حكم