break وcontinue والخروج المبكر
تحكّم في الحلقات بدقة
break وcontinue والخروج المبكر درس مجاني في Mojo Academy على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Mojo Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Mojo Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Steer a Loop Precisely
Sometimes you must leave a loop early or skip a single pass. Mojo gives you break and continue for exactly that control. 🎛️
Stop Early with break
The break statement exits the loop immediately. No further passes run, and execution jumps to the code after the loop.
for i in range(10):
if i == 3:
breakA Classic Search
break shines when searching: scan until you find the target, then stop so you do not waste passes on the rest.
for name in names:
if name == target:
breakSkip a Pass with continue
The continue statement skips the rest of the current pass and jumps straight to the next iteration of the loop.
for i in range(5):
if i == 2:
continue
print(i)break vs continue
Keep them straight: break leaves the loop entirely, while continue only ends the current pass and keeps looping.
Filter While Looping
Use continue to filter. Skip the values you do not care about and let only the interesting ones reach the rest of the body.
for n in nums:
if n < 0:
continue
total += nThey Work in while Too
break and continue are not just for for loops. They behave the same inside a while loop, controlling it the same way.
while True:
if done():
breakOnly the Inner Loop
In nested loops, break and continue affect just the innermost loop they sit in. Outer loops keep running normally.
for row in grid:
for cell in row:
if cell == 0:
breakEarly Exit from a Function
For leaving a whole function early, use return. It ends the function on the spot and hands back a value to the caller.
fn find(x: Int) -> Bool:
if x < 0:
return FalseGuard Clauses Read Well
Returning early on bad input, a guard clause, keeps the happy path clean and avoids deep nesting in your functions.
fn process(n: Int):
if n == 0:
return
work(n)Use Sparingly, Clearly
break, continue, and return are powerful but can hide flow. Reach for them when they make intent clearer, not just to save a line.
Quick Check
Inside a loop you hit a value you want to ignore, but you still want the loop to keep going. Which statement fits?
Recap
break leaves a loop, continue skips one pass, and return exits a function early. Use each to make your flow clearer. 🎯
الأسئلة الشائعة
هل درس «break وcontinue والخروج المبكر» مجاني؟
نعم — نص درس «break وcontinue والخروج المبكر» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Mojo Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Mojo Academy 4 دروس في المجموع.
ماذا ستتعلم في «break وcontinue والخروج المبكر»؟
تحكّم في الحلقات بدقة تتمرن على Mojo Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Mojo Academy؟
لا تُشترط خبرة سابقة. Mojo Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «break وcontinue والخروج المبكر»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Mojo Academy هذا؟
نعم. كل درس في Mojo Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- اتخاذ القرارات باستخدام if/else
- التكرار باستخدام while
- التكرار باستخدام for وrange
- break وcontinue والخروج المبكر