PythonPython · Chapterبەش 7 · Lectureوانە 7.3

Controlling Loops: break and continueکۆنترۆڵکردنی خولەکان: break و continue

Leave a loop early with break, skip a round with continue, and see exactly where control jumps. Solve Lab 1's even-number tasks, meet pass and the loop else, and avoid the continue trap in while loops.

بە break زوو لە دەربچۆ، بە continue جێبهێڵە و ڕێک ببینە کۆنترۆڵ بۆ کوێ باز دەدات. ئەرکەکانی ژمارە جووتەکانی تاقیگەی 1 چارەسەر بکە، pass و elseی بناسە و خۆت لە تەڵەی continue لە بپارێزە.

13 min read13 خولەک خوێندنەوە · 14 stepsهەنگاو

What you will learnچی فێر دەبیت

  • Use break to leave a loop and continue to skip one round, in for and while loops.
  • Predict the output of loops that contain break and continue.
  • Solve the Lab 1 even-number tasks and decide whether the end value is included.
  • Explain pass and the loop else, and avoid continue before the update in a while loop.
  • break بۆ دەرچوون لە و continue بۆ جێهێشتنی یەک بەکار بهێنیت، لە و .
  • ئەو پێشبینی بکەیت کە break و continueیان تێدایە.
  • ئەرکەکانی ژمارە جووتەکانی تاقیگەی 1 چارەسەر بکەیت و بڕیار بدەیت ئایا نرخی کۆتایی تێدایە یان نا.
  • pass و elseی ڕوون بکەیتەوە و خۆت لە continue پێش نوێکردنەوە لە بپارێزیت.

Changing the normal flow of a loopگۆڕینی ڕەوتی جێبەجێکردنی ئاسایی خول

Normally a while loop stops only when its condition becomes False, and a for loop only when the sequence runs out. Two loop control statements change that from inside the body: break leaves the loop at once, and continue skips the rest of the current round and goes on with the next one. Both work in for and in while loops.

بە ئاسایی تەنها کاتێک دەوەستێت کە False بێت و تەنها کاتێک کە تەواو دەبێت. دوو کۆنترۆڵکردنی ئەمە لە ناو دەگۆڕن: break دەستبەجێ لە دەردەچێت و continue باقیی ئێستا جێدەهێڵێت و بە دواتر بەردەوام دەبێت. هەردووکیان لە و کار دەکەن.

Checking eggs at the bazaarپشکنینی هێلکە لە بازاڕدا

A shopkeeper checks a tray of eggs one by one. A cracked egg is put aside and the checking goes on with the next egg — that is continue. If the shopkeeper finds that the whole tray is from the wrong order, the checking stops completely and the next job starts — that is break.

دووکاندارێک شانەیەک هێلکە یەک یەک دەپشکنێت. هێلکەیەکی شکاو لادەنرێت و پشکنین بە هێلکەی دواتر بەردەوام دەبێت — ئەمە continueـە. ئەگەر دووکاندارەکە بزانێت هەموو شانەکە هی داواکارییەکی هەڵەیە، پشکنین بە تەواوی دەوەستێت و کاری دواتر دەست پێدەکات — ئەمە breakـە.

Where the analogy stops workingسنووری ئەم نموونەیە

A shopkeeper can go back to an egg later. A loop never goes back: a skipped round is gone, and after break the loop does not restart.

دووکاندار دەتوانێت دواتر بگەڕێتەوە بۆ هێلکەیەک. هەرگیز ناگەڕێتەوە: جێهێڵراو ڕۆیشتووە و دوای break دەست پێ ناکاتەوە.

Quick checkپشکنینی خێراWhich statement leaves a loop completely?کام بە تەواوی لە دەردەچێت؟

17- دوباره بوونه وه While Loop & Break - ContinueMR. Ant · Kurdish (Sorani)کوردی (سۆرانی) · 6.7K viewsبینین · 223 likesلایک

In Kurdish: the while loop, and how break and continue change it.

بە کوردی: و چۆن break و continue ڕەفتاری دەگۆڕن.

Kurdish (Sorani), whole video (9:36): part 17 of MR. Ant's Python course (while loop, break, continue).

ڤیدیۆیەکی کوردی (سۆرانی)، هەموو ڤیدیۆکە ⁦(9:36)⁩: بەشی ⁦17⁩ لە کۆرسی ⁦MR. Ant⁩ (، break و continue).

break: leave the loop nowbreak: ئێستا لە خول دەربچۆ

Definitionپێناسە

break stops a loop (or a switch) at once, and the program continues with the code after it. In Python it leaves only the innermost loop; PHP's break 2; can leave two nested loops.

break (یان switchـێک) دەستبەجێ دەوەستێنێت و بە کۆدی دوای ئەو بەردەوام دەبێت. لە Pythonـدا تەنها ناوەکیترین خول جێدەهێڵێت؛ break 2;ـی PHP دەتوانێت دوو خولی ناوەکی جێبهێڵێت.

When a break statement runs inside a loop body, the loop stops immediately and control moves to the first statement after the loop. Any rounds that were still to come are dropped. You can use it in a for or a while loop.

کاتێک لە ناو جێبەجێ دەبێت، دەستبەجێ دەوەستێت و کۆنترۆڵ دەچێتە سەر یەکەم دوای . هەموو تر کە هێشتا مابوو فڕێ دەدرێت. دەتوانیت لە یان بەکاری بهێنیت.

Stop at 3لە 3دا بوەستەbreak_demo.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
for a in range(1, 9):
    if a == 3:
        break
    print(a)
print("After the loop")
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2-3

When a is 3, break ends the loop before print(a) runs.

کاتێک a 3 بێت، break پێش جێبەجێبوونی print(a) کۆتایی پێ دەهێنێت.

Lineدێڕی 5

Control jumps here: the first statement after the loop.

کۆنترۆڵ باز دەدات بۆ ئێرە: یەکەم دوای .

Quick checkپشکنینی خێراWhat does this print? for n in range(10): / if n == 4: break / print(n, end=" ")ئەمە چی چاپ دەکات؟ for n in range(10): / if n == 4: break / print(n, end=" ")

continue: skip to the next roundcontinue: باز بدە بۆ خولانەوەی دواتر

Definitionپێناسە

continue skips the rest of the current iteration and jumps to the next one. Unlike break, the loop keeps going.

continue پاشماوەی خولانەوەی ئێستا تێدەپەڕێنێت و دەچێتە سەر خولانەوەی دواتر. جیاواز لە break، بەردەوام دەبێت.

When control reaches a continue statement, the statements after it in the body are skipped for this round. The loop then moves to the next item of the sequence (or, in a while loop, back to the condition) and runs the body for it. It works in both for and while loops.

کاتێک کۆنترۆڵ دەگاتە ، هەموو کە لە دوای ئەوەوە لە بۆ ئەم جێدەهێڵرێت. پاشان دەچێتە سەر دانەی دواتری (یان لە دەگەڕێتەوە بۆ ) و بۆ ئەو جێبەجێ دەکات. لە هەردوو و کار دەکات.

Skip 33 جێبهێڵەcontinue_demo.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
for a in range(1, 9):
    if a == 3:
        continue
    print(a)
print("After the loop")
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2-3

When a is 3, continue skips print(a) for this round only.

کاتێک a 3 بێت، continue تەنها بۆ ئەم print(a) جێدەهێڵێت.

Lineدێڕی 4

For every other value the print runs as normal.

بۆ هەموو نرخێکی تر print بە ئاسایی جێبەجێ دەبێت.

Quick checkپشکنینی خێراWhat does this print? for n in range(5): / if n == 2: continue / print(n, end=" ")ئەمە چی چاپ دەکات؟ for n in range(5): / if n == 2: continue / print(n, end=" ")

continue: skip to the next roundcontinue: باز بدە بۆ خولانەوەی دواتر

break and continue side by sidebreak و continue لە تەنیشت یەک

The two slide programs differ in one word. The picture shows where control jumps in each: break jumps out, below the loop; continue jumps back up to the next round.

دوو سلایدەکە تەنها لە یەک وشەدا جیاوازن. وێنەکە پیشان دەدات کۆنترۆڵ لە هەر یەکێکیاندا بۆ کوێ باز دەدات: break باز دەدات بۆ دەرەوە، خوار ؛ continue باز دەدات بۆ سەرەوە، بۆ دواتر.

Two small flow diagrams. Left, break: for a in range(1, 9) leads to the test a == 3?; False goes to print(a) and back to the loop; True goes to a break box, from which a green arrow goes down and out to "after the loop". Right, continue: the same loop, but the True arrow goes to a continue box, from which a green arrow goes back up to the for line, the next round.دوو هێڵکاریی ڕەوتی بچووک. چەپ، break: for a in range(1, 9) دەچێتە سەر پشکنینی a == 3?؛ False دەچێت بۆ print(a) و دەگەڕێتەوە بۆ خولەکە؛ True دەچێت بۆ چوارگۆشەی break و لەوێوە تیرێکی سەوز بەرەو خوار و دەرەوە دەچێت بۆ «دوای خولەکە». ڕاست، continue: هەمان خول، بەڵام تیری True دەچێت بۆ چوارگۆشەی continue و لەوێوە تیرێکی سەوز دەگەڕێتەوە بۆ سەرەوە، بۆ دێڕی for، واتە خولانەوەی دواتر.
break jumps out of the loop; continue jumps to the next round.break لە باز دەداتە دەرەوە؛ continue باز دەدات بۆ دواتر.CêIT
break and continue comparedبەراوردی break و continue
StatementEffect on this roundکاریگەری لەسەر ئەم Effect on the loopکاریگەری لەسەر Output of the slide program سلایدەکە
breakEnds itکۆتایی پێ دەهێنێتEnds the whole loopهەموو کۆتایی پێ دەهێنێت1 2
continueEnds itکۆتایی پێ دەهێنێتGoes on with the next itemبە دانەی دواتر بەردەوام دەبێت1 2 4 5 6 7 8
Quick checkپشکنینی خێراA loop prints the numbers 1 to 10. You want to leave out only the 5. Which statement do you use? ژمارەکانی 1 تا ⁦10⁩ چاپ دەکات. دەتەوێت تەنها 5 لابەریت. کام بەکار دەهێنیت؟

continue: skip to the next roundcontinue: باز بدە بۆ خولانەوەی دواتر

Watch them runسەیری جێبەجێبوونیان بکە

The animation runs both slide programs side by side, one value of a at a time. Watch what happens at a = 3.

ئەنیمەیشنەکە هەردوو سلایدەکە لە تەنیشت یەک جێبەجێ دەکات، هەر جارێک یەک نرخی a. سەیر بکە لە a = 3دا چی ڕوودەدات.

Animation with two lanes of number boxes 1 to 8. Top lane: for a in range(1, 9) with if a == 3: continue. Bottom lane: the same with break. A pointer moves along both lanes together. At a = 3 the top lane marks 3 as skipped and keeps going, printing 1 2 4 5 6 7 8; the bottom lane marks 3, greys out 4 to 8 and stops, having printed only 1 2.
Animation with two lanes of number boxes 1 to 8. Top lane: for a in range(1, 9) with if a == 3: continue. Bottom lane: the same with break. A pointer moves along both lanes together. At a = 3 the top lane marks 3 as skipped and keeps going, printing 1 2 4 5 6 7 8; the bottom lane marks 3, greys out 4 to 8 and stops, having printed only 1 2.
At a = 3, continue skips one round; break ends the loop.لە ⁦a = 3⁩دا، continue یەک جێدەهێڵێت؛ break کۆتایی پێ دەهێنێت.CêIT
Quick checkپشکنینی خێراIn the break lane, how many times does print(a) run?لە ڕیزی breakـدا، print(a) چەند جار جێبەجێ دەبێت؟

Lab task 2-1: even numbers from 0 to 40ئەرکی تاقیگە ⁦2-1⁩: ژمارە جووتەکان لە 0 تا ⁦40⁩

ExerciseڕاهێنانTask 2-1ئەرکی ⁦2-1⁩

Write a program that prints only the even numbers between 0 and 40.

بنووسە کە تەنها ژمارە جووتەکانی نێوان 0 و ⁦40⁩ چاپ بکات.

Hintئاماژە

A step of 2 in range() gives only even numbers. Or test n % 2 and use continue.

هەنگاوی 2 لە range()ـدا تەنها ژمارەی جووت دەدات. یان n % 2 بپشکنە و continue بەکار بهێنە.

Show answerوەڵامەکە پیشان بدە
The simplest way: a step of 2سادەترین ڕێگە: هەنگاوی 2even_range.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
for n in range(0, 41, 2):
    print(n, end=" ")
print()

The course owner's solution walks through every number and uses continue to skip the odd ones (a % 2 == 1 means "odd"):

چارەسەری خاوەنی کۆرسەکە بە هەموو ژمارەیەکدا دەڕوات و continue بەکار دەهێنێت بۆ جێهێشتنی ژمارە تاکەکان (a % 2 == 1 واتە «تاک»):

Owner's solutionچارەسەری خاوەنی کۆرسەکەeven_owner.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
#Task 2-1
for a in range(40):
    if a % 2 == 1:
        continue
    print(a, end=" . ")
Quick checkپشکنینی خێراWhich loop prints 0, 2, 4, …, 40 including 40?کام 0، 2، 4، …، ⁦40⁩ چاپ دەکات، ⁦40⁩یش لەگەڵیدا؟

Lab task 2-2: skip 4 and 16ئەرکی تاقیگە ⁦2-2: 4⁩ و ⁦16⁩ جێبهێڵە

ExerciseڕاهێنانTask 2-2ئەرکی ⁦2-2⁩

Repeat the previous task, but let the program ignore both 4 and 16.

ئەرکی پێشوو دووبارە بکەرەوە، بەڵام با 4 و ⁦16⁩ هەردووکیان پشتگوێ بخات.

Show answerوەڵامەکە پیشان بدە
skip_4_16.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
for n in range(0, 41, 2):
    if n == 4 or n == 16:
        continue
    print(n, end=" ")
print()
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2-3

Two numbers are skipped with one continue.

دوو ژمارە بە یەک continue جێدەهێڵرێن.

The owner's solution adds the two tests to the odd test:

چارەسەری خاوەنی کۆرسەکە دوو پشکنینەکە دەخاتە سەر پشکنینی تاک:

Owner's solutionچارەسەری خاوەنی کۆرسەکەskip_owner.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
#Task 2-2
for a in range(40):
    if a % 2 == 1 or a == 4 or a == 16:
        continue
    print(a, end=" . ")

In the owner's file the two tasks stand one after the other with no print() between them. Because both loops print with end=" . ", the second output continues on the same line as the first:

لە فایلەکەی خاوەنی کۆرسەکەدا دوو ئەرکەکە لە دوای یەکدان و هیچ print()ـێک لە نێوانیاندا نییە. چونکە هەردوو بە end=" . " چاپ دەکەن، دووەم لە هەمان دێڕی یەکەمدا بەردەوام دەبێت:

Both tasks in one fileهەردوو ئەرکەکە لە یەک فایلداboth_tasks.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
#Task 2-1
for a in range(40):
    if a % 2 == 1:
        continue
    print(a, end=" . ")

#Task 2-2
for a in range(40):
    if a % 2 == 1 or a == 4 or a == 16:
        continue
    print(a, end=" . ")

A shorter test for "is it one of these values?" uses in with a tuple of the values (see in in Working with strings):

پشکنینێکی کورتتر بۆ «ئایا یەکێکە لەم نرخانە؟» in لەگەڵ لە نرخەکان بەکار دەهێنێت (بڕوانە in لە کارکردن لەگەڵ زنجیرەنووسەکان):

skip_in.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
for n in range(0, 41, 2):
    if n in (4, 16):
        continue
    print(n, end=" ")
print()
Quick checkپشکنینی خێراWhy do the two tasks' outputs end up on one line in the owner's file?بۆچی لە فایلەکەی خاوەنی کۆرسەکەدا هەردوو ئەرکەکە لە یەک دێڕدا کۆتایی دێن؟

pass: a statement that does nothingpass: ڕستەیەک کە هیچ ناکات

Python needs at least one statement in every block. pass is a statement that does nothing: use it as a placeholder for a body you will write later. Do not confuse it with continue: after pass the rest of the body still runs; after continue it does not.

لە هەموو لانیکەم یەک پێویستە. pass کە هیچ ناکات: وەک جێگرەوەیەک بۆ جەستەیەک بەکاری بهێنە کە دواتر دەینووسیت. تێکەڵی مەکە لەگەڵ continue: دوای pass باقیی هەر جێبەجێ دەبێت؛ دوای continue نا.

pass against continuepass بەرامبەر continuepass_vs_continue.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
for n in range(1, 6):
    if n == 3:
        pass  # nothing happens: the loop goes on to print(n)
    print(n, end=" ")
print()
for n in range(1, 6):
    if n == 3:
        continue  # skip the rest of this round
    print(n, end=" ")
print()
A placeholder bodyجەستەیەکی جێگرەوەplaceholder.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
for n in range(3):
    pass  # TODO: write the loop body later
print("The program still runs")
Without any body: an errorبەبێ هیچ جەستەیەک: هەڵەempty_body.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
for n in range(3):
print("Hello")
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

Not indented, so the for has no body at all.

بەرەوناو نەچووە، بۆیە for هیچ جەستەیەکی نییە.

Quick checkپشکنینی خێراWhat does for n in range(3): pass do?for n in range(3): pass چی دەکات؟

Extra: else on a loopزیادە: else لەسەر خول

A for or while loop may have an else block. It runs only if the loop finished normally, without break. This fits searches perfectly: if the loop never found what it wanted (no break), the else says so.

یان دەکرێت elseـی هەبێت. تەنها کاتێک جێبەجێ دەبێت کە بە ئاسایی تەواو بووبێت، بەبێ break. ئەمە بۆ گەڕان زۆر گونجاوە: ئەگەر هەرگیز ئەوەی نەدۆزیبێتەوە کە دەیویست (هیچ breakـێک نەبووبێت)، else ئەوە دەڵێت.

search_else.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
names = ["Aram", "Shilan", "Karwan"]
wanted = "Dilan"
for name in names:
    if name == wanted:
        print("Found", wanted)
        break
else:
    print(wanted, "is not in the list")
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 7

else is lined up with for, not with if: it belongs to the loop.

else لە ئاستی forـدایە، نەک if: سەر بە .

Is it prime?ئایا ژمارەیەکی سەرەتاییە؟prime_check.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
n = int(input("A whole number above 1: "))
for d in range(2, n):
    if n % d == 0:
        print(n, "is not prime:", d, "divides it")
        break
else:
    print(n, "is prime")
prime_check.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
n = int(input("A whole number above 1: "))
for d in range(2, n):
    if n % d == 0:
        print(n, "is not prime:", d, "divides it")
        break
else:
    print(n, "is prime")

This check tries every divisor from 2 to n - 1. Comparing algorithms shows three faster methods that give the same answer.

ئەم پشکنینە هەموو دابەشکەرێک لە 2ـەوە تا n - 1 تاقی دەکاتەوە. بەراوردکردنی ئەلگۆریزمەکان سێ ڕێگای خێراتر پیشان دەدات کە هەمان وەڵام دەدەن.

Quick checkپشکنینی خێراWhen does the else block of a for loop run? elseی کەی جێبەجێ دەبێت؟

Python For Loops - Python Tutorial for Absolute BeginnersProgramming with Mosh · Englishئینگلیزی · 855K viewsبینین · 14K likesلایک · Partبەشی 3:42–6:30 (ofلە 14:41)

break stops a loop early, and a for loop's else block runs only when the loop ends without break; then how nested loops run.

break زوو دەوەستێنێت و تەنها کاتێک جێبەجێ دەبێت کە بەبێ break کۆتایی بێت؛ پاشان چۆن کار دەکەن.

Plays 3:42-6:30, "Loop control and else clause": break, and an else block that runs only if the loop was not broken.

لە ⁦3:42⁩ تا ⁦6:30⁩ پەخش دەبێت، بەشی Loop control and else clause: break و کە تەنها کاتێک جێبەجێ دەبێت بە break نەوەستابێت.

Common mistakesهەڵە باوەکان

The worst trap: continue in a while loop before the counter is updated. The update is skipped, the counter never changes, and the loop never ends:

خراپترین تەڵە: continue لە پێش نوێکردنەوەی . نوێکردنەوەکە جێدەهێڵرێت، هەرگیز ناگۆڕێت و هەرگیز کۆتایی نایەت:

Bug: infinite loopباگ: خولی بێکۆتاcontinue_trap.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
n = 0
while n < 5:
    if n == 2:
        continue
    print(n)
    n += 1
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 3-4

When n is 2, continue jumps back to the condition before n += 1 runs: n stays 2 for ever.

کاتێک n 2 بێت، continue پێش جێبەجێبوونی n += 1 باز دەداتەوە بۆ : n بۆ هەمیشە 2 دەمێنێتەوە.

Fixed: update firstچاککراو: سەرەتا نوێ بکەرەوەcontinue_fixed.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
n = 0
while n < 5:
    n += 1
    if n == 2:
        continue
    print(n)
break, continue and pass mistakesهەڵەکانی break، continue و pass
MistakeهەڵەWhat happensچی ڕوودەداتFixچارەسەر
continue before the update in a while loopcontinue پێش نوێکردنەوە لە Infinite loopUpdate the counter firstسەرەتا نوێ بکەرەوە
break outside any loopbreak لە دەرەوەی هەموو SyntaxError: 'break' outside loopSyntaxError: 'break' outside loopUse it only inside a loop bodyتەنها لە ناو بەکاری بهێنە
Expecting break to leave all nested loopsچاوەڕوانی ئەوەی break لە هەموو دەربچێتOnly the innermost loop endsتەنها ناوەکیترین کۆتایی دێتSee Nested loopsبڕوانە خولە ناوەکییەکان
Using pass to skip a roundبەکارهێنانی pass بۆ جێهێشتنی Nothing is skippedهیچ شتێک جێناهێڵرێتUse continuecontinue بەکار بهێنە
if a==3:breakWorks, but hard to readکار دەکات، بەڵام خوێندنەوەی قورسەTwo lines, spaces around ==دوو دێڕ، لە دەوری ==
break with no loopbreak بەبێ خولbreak_outside.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
age = 15
if age < 18:
    break

Exercisesڕاهێنانەکان

ExerciseڕاهێنانNo multiples of 3بێ چەندجارەکانی 3

Print the numbers 1 to 20 on one line, but skip every multiple of 3.

ژمارەکانی 1 تا ⁦20⁩ لە یەک دێڕدا چاپ بکە، بەڵام هەموو چەندجارێکی 3 جێبهێڵە.

Show answerوەڵامەکە پیشان بدە
skip_threes.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
for n in range(1, 21):
    if n % 3 == 0:
        continue
    print(n, end=" ")
print()
ExerciseڕاهێنانTotal of positive numbersکۆی ژمارە موجەبەکان

Read numbers until the user types 0. Ignore negative numbers (say so), and print the total of the others. Use while True, break and continue.

ژمارە بخوێنەرەوە تا 0 دەنووسێت. پشتگوێ بخە (ئەمەش بڵێ) و کۆی ئەوانی تر چاپ بکە. while True، break و continue بەکار بهێنە.

Show answerوەڵامەکە پیشان بدە
positive_total.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
total = 0
while True:
    number = int(input("Number (0 to stop): "))
    if number == 0:
        break
    if number < 0:
        print("Negative numbers are ignored.")
        continue
    total += number
print("Total:", total)
ExerciseڕاهێنانPredictپێشبینی بکە

What does this program print? Explain each step.

ئەم چی چاپ دەکات؟ هەموو هەنگاوێک ڕوون بکەرەوە.

predict.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
for n in range(10):
    if n % 2 == 0:
        continue
    if n > 6:
        break
    print(n)
Show answerوەڵامەکە پیشان بدە
predict.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
for n in range(10):
    if n % 2 == 0:
        continue
    if n > 6:
        break
    print(n)

Even numbers are skipped by continue. The odd numbers 1, 3 and 5 are printed. At 7 the test n > 6 is True, so break ends the loop and 9 is never reached.

ژمارە جووتەکان بە continue جێدەهێڵرێن. ژمارە تاکەکانی 1، 3 و 5 چاپ دەکرێن. لە 7دا پشکنینی n > 6 Trueـە، بۆیە break کۆتایی پێ دەهێنێت و هەرگیز ناگاتە 9.

ExerciseڕاهێنانThe first vowelیەکەم پیتی بزوێن

Ask for a word and print its first vowel. If it has none, print No vowel in … — use a loop else.

داوای وشەیەک بکە و یەکەم پیتی بزوێنی چاپ بکە. ئەگەر هیچی نەبوو، No vowel in … چاپ بکە — elseی بەکار بهێنە.

Show answerوەڵامەکە پیشان بدە
first_vowel.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
word = input("A word: ")
for letter in word:
    if letter.lower() in "aeiou":
        print("First vowel:", letter)
        break
else:
    print("No vowel in", word)
first_vowel.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
word = input("A word: ")
for letter in word:
    if letter.lower() in "aeiou":
        print("First vowel:", letter)
        break
else:
    print("No vowel in", word)
ExerciseڕاهێنانChallenge: break in a loop inside a loopڕاهێنانی قورستر: break لە خولێک لە ناو خولێکی تردا

Here a for loop runs inside another for loop: for every x, the inner loop tries y = 1 to 5. Predict the output. Does break stop both loops?

لێرەدا لە ناو تردا جێبەجێ دەبێت: بۆ هەر xـێک، y لە 1 تا 5 تاقی دەکاتەوە. پێشبینی بکە. ئایا break هەردوو دەوەستێنێت؟

PYTHONType in a terminalلە تێرمیناڵدا بینووسە
for x in range(1, 4):
    for y in range(1, 6):
        if x * y > 6:
            break
        print(x * y, end=" ")
    print("| row", x, "done")
print("All rows done")
Hintئاماژە

break leaves only the loop it is written in — here the inner for y loop.

break تەنها لەو دەردەچێت کە تێیدا نووسراوە — لێرەدا ناوەوە، for y.

Show answerوەڵامەکە پیشان بدە
PYTHONType in a terminalلە تێرمیناڵدا بینووسە
for x in range(1, 4):
    for y in range(1, 6):
        if x * y > 6:
            break
        print(x * y, end=" ")
    print("| row", x, "done")
print("All rows done")
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 3-4

Ends only the inner loop; the line print("| row", x, "done") still runs.

تەنها کۆتایی پێ دەهێنێت؛ دێڕی print("| row", x, "done") هەر جێبەجێ دەبێت.

  • Row 1: 1×5=51 \times 5 = 5 is never above 6, so the inner loop runs all five rounds.
  • Row 2: at y = 4, 2×4=8>62 \times 4 = 8 > 6, so break ends the inner loop after 2 4 6.
  • Row 3: at y = 3, 3×3=9>63 \times 3 = 9 > 6: only 3 and 6 are printed.
  • The outer loop is never stopped: all three rows finish and All rows done is printed.
  • ڕیزی 1: 1×5=51 \times 5 = 5 هەرگیز لە 6 زیاتر نییە، بۆیە هەر پێنج جارەکە جێبەجێ دەبێت.
  • ڕیزی 2: کاتێک y = 4، 2×4=8>62 \times 4 = 8 > 6، بۆیە break دوای ⁦2 4 6⁩ کۆتایی پێ دەهێنێت.
  • ڕیزی 3: کاتێک y = 3، 3×3=9>63 \times 3 = 9 > 6: تەنها 3 و 6 چاپ دەکرێن.
  • هەرگیز ناوەستێت: هەر سێ ڕیزەکە تەواو دەبن و All rows done چاپ دەکرێت.

Summaryپوختە

  • break ends the whole loop at once; control goes to the first statement after the loop.
  • continue skips the rest of this round and goes on with the next one.
  • Both work in for and while loops; break leaves only the innermost loop.
  • Lab 1: range(0, 41, 2) gives the even numbers to 40; continue can skip chosen values; end each output line with print().
  • pass does nothing (a placeholder); a loop's else runs only when no break happened.
  • In a while loop, update the counter before any continue.
  • break دەستبەجێ هەموو کۆتایی پێ دەهێنێت؛ کۆنترۆڵ دەچێتە سەر یەکەم دوای .
  • continue باقیی ئەم جێدەهێڵێت و بە دواتر بەردەوام دەبێت.
  • هەردووکیان لە for و whileـدا کار دەکەن؛ break تەنها لە ناوەکیترین دەردەچێت.
  • تاقیگەی 1: range(0, 41, 2) ژمارە جووتەکان تا ⁦40⁩ دەدات؛ continue دەتوانێت نرخە هەڵبژێردراوەکان جێبهێڵێت؛ هەر دێڕێکی بە print() کۆتایی پێ بهێنە.
  • pass هیچ ناکات (جێگرەوەیە)؛ elseی تەنها کاتێک جێبەجێ دەبێت کە هیچ breakـێک ڕووی نەدابێت.
  • لە پێش هەر continueـێک نوێ بکەرەوە.
Review questionsپرسیارەکانی پێداچوونەوە
What is the output of the slide's break program (range(1, 9), break at 3)?دەرخستەی بەرنامەی breakـی سلایدەکە (range(1, 9)، break لە 3دا) چییە؟

1 and 2.

1 و 2.

And of the continue program?هی بەرنامەکە continue چی؟

1 2 4 5 6 7 8 — only 3 is missing.

⁦1 2 4 5 6 7 8⁩ — تەنها 3 نییە.

What is the difference between pass and continue?جیاوازیی نێوان pass و continue چییە؟

pass does nothing and the body goes on; continue skips the rest of the body for this round.

pass هیچ ناکات و بەردەوام دەبێت؛ continue باقیی بۆ ئەم جێدەهێڵێت.

Why did the owner's Task 2-1 output stop at 38?بۆچی دەرخستەی ئەرکی ⁦2-1⁩ی خاوەنی کۆرسەکە لە ⁦38⁩دا وەستا؟

range(40) stops at 39, and 39 is odd; use range(41) to include 40.

range(40) لە ⁦39⁩دا دەوەستێت و ⁦39⁩ تاکە؛ بۆ ئەوەی ⁦40⁩ تێدا بێت range(41) بەکار بهێنە.

Videoڤیدیۆ

Watch a videoسەیری ڤیدیۆیەک بکە

A video that viewers liked a lot and praised in their comments, chosen for this lesson.ڤیدیۆیەک کە بینەران زۆریان پێ خۆش بووە و لە کۆمێنتەکانیاندا ستایشیان کردووە، بۆ ئەم وانەیە هەڵبژێردراوە.

Python break continue pass ⛔Bro Code · Englishئینگلیزی · 57K viewsبینین · 2.8K likesلایک

break leaves a loop at once, continue skips to the next round, and pass is a statement that does nothing.

break دەستبەجێ لە دەردەچێت، continue بۆ داهاتوو باز دەدات و pass کە هیچ ناکات.

Whole video (4:40). Chapters: 0:20 break, 1:30 continue, 2:57 pass.

هەموو ڤیدیۆکە ⁦(4:40)⁩. بەشەکان: ⁦0:20⁩ break، ⁦1:30⁩ continue، ⁦2:57⁩ pass.

Lesson complete!وانەکە تەواو بوو!

Well done. Review the key terms, then go on to the next lesson.دەستخۆش. سەیرێکی زاراوە سەرەکییەکان بکەرەوە، پاشان بڕۆ بۆ وانەی داهاتوو.

Key termsزاراوە سەرەکییەکان

Next lessonوانەی داهاتوو

Nested Loops and Patternsخولە ناوەکییەکان و کێشانی شێوە

References and further readingسەرچاوەکان و خوێندنەوەی زیاتر

This lecture is a first edition. The Kurdish text and terminology are waiting for review by Kurdish IT teachers.ئەم وانەیە چاپی یەکەمە. دەقە کوردییەکە و زاراوەکانی چاوەڕێی پێداچوونەوەی مامۆستایانی کوردی بواری IT ن.