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.

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.

Which one, and one they cannot do

You want toUse
Ignore some items and keep goingcontinue
Stop as soon as you find onebreak
Stop and remember what you foundbreak, after storing it
Leave a function entirelyreturn — lesson 19
Leave both loops in a nested pairA 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.

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.

Try it live / جرّب بنفسك

Preview / المعاينة

Check yourself / اختبر نفسك

1. What is the difference between break and continue?

2. Why can continue in a while loop freeze the browser?

3. A break inside two nested loops leaves which?

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 صيغتا الحلقة الأولى مع حكم
How do you want to submit? / كيف تريد التسليم؟