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

The while Loopخولی while

Why programs need loops, the two kinds of loops, and Python's while loop: its condition, body and iterations, a three-step way to design a loop, counters, accumulators and sentinel values, infinite loops, and asking again until the input is valid.

بۆچی پێویستیان بە هەیە، دوو جۆرەکەی و : ، جەستە و ، ڕێگەیەکی سێ هەنگاوی بۆ داڕشتنی ، ، و ، و دووبارە پرسیارکردن تا دروست دەبێت.

16 min read16 خولەک خوێندنەوە · 16 stepsهەنگاو

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

  • Explain what a loop is and choose between a condition-controlled and a count-controlled loop.
  • Write and trace a while loop: condition, body, iterations and the value of the counter at the end.
  • Design a loop in three steps and use counters, accumulators and sentinel values.
  • Recognise and stop infinite loops, and use while True with break for input validation.
  • ڕوونی بکەیتەوە چییە و لە نێوان و یەکێکیان هەڵبژێریت.
  • بنووسیت و بەدوایدا بچیت: ، جەستە، و نرخی لە کۆتاییدا.
  • بە سێ هەنگاو دابڕێژیت و ، و بەکار بهێنیت.
  • بناسیتەوە و ڕایانبگریت و while True لەگەڵ break بۆ بەکار بهێنیت.

Why do we need loops?بۆچی پێویستمان بە خول هەیە؟

So far, every line of a program ran once, from top to bottom. A loop tells the program to run statements again and again, and it controls how many times a group of operations is repeated. The course report puts it this way: a loop is a sequence of instructions that is repeated until a certain condition is reached.

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

An example: you must show Programming is fun! 100 times. Without a loop you would type the same print line 100 times — tedious, easy to get wrong, and hard to change to 1,000 times.

نموونەیەک: دەبێت Programming is fun! ⁦100⁩ جار پیشان بدەیت. بەبێ دەبێت هەمان دێڕی print ⁦100⁩ جار بنووسیت — بێزارکەرە، هەڵەی تێ دەکەوێت و گۆڕینی بۆ ⁦1000⁩ جار قورسە.

Without a loopبەبێ خولno_loop.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
print("Programming is fun!")
print("Programming is fun!")
# ... 100 times in total
print("Programming is fun!")
With a loop: four linesبە خولێک: چوار دێڕwith_loop.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
count = 0
while count < 100:
    print("Programming is fun!")
    count = count + 1
Two panels. Left, "Without a loop": the line print("Programming is fun!") written again and again, marked 100 lines. Right, "With a loop": four lines — count = 0, while count < 100:, print("Programming is fun!"), count = count + 1 — marked 4 lines. Both programs print the same 100 lines.دوو بەش. چەپ، «بەبێ خول»: دێڕی print("Programming is fun!") دووبارە و دووبارە نووسراوە و نیشانەی 100 دێڕی لەسەرە. ڕاست، «بە خولێک»: چوار دێڕ — count = 0، while count < 100:، print("Programming is fun!") و count = count + 1 — بە نیشانەی 4 دێڕ. هەردوو بەرنامەکە هەمان 100 دێڕ چاپ دەکەن.
Same output, 100 lines against 4.هەمان ، ⁦100⁩ دێڕ بەرامبەر 4.CêIT

This chapter teaches loops step by step: the kinds of loops, the while loop and how to design a loop (this lesson), the for loop with range() (next lesson), the loop control statements break and continue (Controlling loops) and loops inside loops (Nested loops).

ئەم بەشە هەنگاو بە هەنگاو فێر دەکات: جۆرەکانی ، و چۆنیەتیی داڕشتنی (ئەم وانەیە)، لەگەڵ range() (وانەی داهاتوو)، کۆنترۆڵکردنی ، break و continue (کۆنترۆڵکردنی خولەکان) و لە ناو (خولە ناوەکییەکان).

Quick checkپشکنینی خێراWhat is the biggest gain of the four-line loop over 100 print lines?گەورەترین سوودی چوار دێڕییەکە بەرامبەر ⁦100⁩ دێڕی print چییە؟

Two kinds of loopsدوو جۆری خول

There are two main kinds of loops. A condition-controlled loop repeats as long as a condition is true; in Python this is the while loop. A count-controlled loop repeats a known number of times, or once for each item of a sequence; in Python this is the for loop.

دوو جۆری سەرەکیی هەیە. دووبارە دەبێتەوە تا ڕاست بێت؛ لە ئەمە . ژمارەیەکی زانراو جار دووبارە دەبێتەوە، یان بۆ هەر دانەیەکی جارێک؛ لە ئەمە .

Loops and when to use them (Java, C and C# also have do … while) و کاتی بەکارهێنانیان (Java، C و ⁦C#⁩ هەیە)
LoopChecks the condition… دەپشکنێت…Use it when…کەی بەکاری بهێنیتIn Pythonلە
whilebefore every roundپێش هەموو you do not know how many rounds: ‘until the user types 0’نازانیت چەند : «تا 0 دەنووسێت»yesبەڵێ
for— (it walks through a sequence)— (بە دەڕوات)you know the count or have a sequence: ‘10 times’, ‘each letter’ژمارەکە دەزانیت یان هەیە: «⁦10⁩ جار»، «هەر پیتێک»yesبەڵێ
do … whileafter every round, so the body runs at least onceدوای هەموو ، بۆیە لانیکەم یەکجار جێبەجێ دەبێتmenus that must appear at least once کە دەبێت لانیکەم یەکجار دەربکەونno — see belowنەخێر — خوارەوە ببینە
Laps on the football pitchخولانەوە بە دەوری یاریگای تۆپی پێ

A coach can say "Run 5 laps" — you know the count before you start, like a for loop. Or the coach can say "Keep running laps while I have not blown the whistle" — you check a condition before each lap, like a while loop.

ڕاهێنەر دەتوانێت بڵێت «5 خولانەوە ڕابکە» — پێش دەستپێکردن ژمارەکە دەزانیت، وەک . یان دەتوانێت بڵێت «تا فیکە لێ نەدەم بەردەوام بە لە ڕاکردن» — پێش هەر خولانەوەیەک دەپشکنیت، وەک .

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

A coach can whistle in the middle of a lap; a while loop checks only at the start of each round, so a round that has started is always finished.

ڕاهێنەر دەتوانێت لە ناوەڕاستی خولانەوەیەکدا فیکە لێ بدات؛ تەنها لە سەرەتای هەر دەپشکنێت، بۆیە کە دەستی پێکردبێت هەمیشە تەواو دەکرێت.

Quick checkپشکنینی خێرا"Ask for prices until the user types 0." Which loop fits best?«داوای نرخ بکە تا 0 دەنووسێت.» کام باشترە؟

The while loopخولی while

Definitionپێناسە

A loop that checks its condition first and repeats its block as long as the condition is true. If the condition is false at the start, the block never runs.

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

A while loop runs its statements again and again as long as a condition remains true. Its shape is:

دووبارە و دووبارە جێبەجێ دەکات تا ڕاست بمێنێتەوە. شێوەکەی ئەمەیە:

The shape of a while loopشێوەی خولی whilewhile_syntax.txtTEXTType in a terminalلە تێرمیناڵدا بینووسە
while loop-continuation-condition:
    # loop body
    statement(s)
  • The loop-continuation-condition is a Boolean expression: it is True or False.
  • It is checked before every round. If it is True, the loop body (the indented lines) runs once.
  • One run of the loop body is called an iteration (a repetition).
  • When the condition is False, the whole loop ends and the program goes on with the first statement after the loop.
  • : Trueـە یان False.
  • پێش هەموو دەپشکنرێت. ئەگەر True بێت، (دێڕە بەرەوناوچووەکان) یەکجار جێبەجێ دەبێت.
  • یەکجار جێبەجێبوونی پێی دەوترێت .
  • کاتێک False بێت، هەموو کۆتایی دێت و بە یەکەم دوای بەردەوام دەبێت.
Flowchart of a while loop. An arrow enters a diamond, "loop-continuation condition?". The True arrow goes down to a box "Statement(s) (loop body)", and from there a "repeat" arrow goes back up to the diamond. The False arrow goes right and down to the box "statement after the loop".هێڵکاریی ڕەوتی خولی while. تیرێک دەچێتە ناو ئەڵماسییەک: «مەرجی بەردەوامبوونی خول؟». تیری True بەرەو خوار دەچێت بۆ چوارگۆشەی «ڕستەکان (جەستەی خول)» و لەوێوە تیرێکی «دووبارە» دەگەڕێتەوە بۆ ئەڵماسییەکە. تیری False بەرەو ڕاست و پاشان بەرەو خوار دەچێت بۆ چوارگۆشەی «ڕستەی دوای خولەکە».
The condition is checked before every iteration. (The flowchart symbols are explained in Algorithms and flowcharts.) پێش هەموو دەپشکنرێت. ( لە ئەلگۆریزمەکان و هێڵکارییەکانی ڕەوت ڕوون کراونەتەوە.)CêIT
Quick checkپشکنینی خێراWhen does a while loop check its condition? کەی دەپشکنێت؟

Python For Beginners - While Loop [ Kurdish ] پایسۆن بۆ ئاستی سەرەتایی - وایڵ لووپK97 Media · Kurdish (Sorani)کوردی (سۆرانی) · 1.7K viewsبینین · 72 likesلایک

In Kurdish: how the while loop works in Python.

بە کوردی: لە چۆن کار دەکات.

Kurdish (Sorani), whole video (4:14).

ڤیدیۆیەکی کوردی (سۆرانی)، هەموو ڤیدیۆکە ⁦(4:14)⁩.

Example 1: a counter that stops at 100نموونەی 1: ژمێرەرێک کە لە ⁦100⁩دا دەوەستێت

Here is the 100-times program again, with its parts named. count is a counter: it starts at 0, the condition count < 100 is checked before every round, and the body prints one line and adds 1 to count. After 100 rounds count is 100, the condition is False and the loop ends.

ئەمە دیسان ⁦100⁩ جارییەکەیە، بە ناوی بەشەکانییەوە. count : لە 0ەوە دەست پێدەکات، count < 100 پێش هەموو دەپشکنرێت و دێڕێک چاپ دەکات و 1 دەخاتە سەر count. دوای ⁦100⁩ ، count دەبێتە ⁦100⁩، False دەبێت و کۆتایی دێت.

Example 1نموونەی 1example1.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
count = 0
while count < 100:
    print("Programming is fun!")
    count = count + 1
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 1

Set the counter before the loop.

پێش دابنێ.

Lineدێڕی 2

The loop-continuation-condition.

.

Lineدێڕی 3-4

The loop body: both lines are indented, so both repeat.

: هەردوو دێڕەکە بەرەوناو چوون، بۆیە هەردووکیان دووبارە دەبنەوە.

Quick checkپشکنینی خێراIn Example 1, how many times is the condition count < 100 checked?لە نموونەی 1دا، count < 100 چەند جار دەپشکنرێت؟

Example 1: a counter that stops at 100نموونەی 1: ژمێرەرێک کە لە ⁦100⁩دا دەوەستێت

Example 1, step by stepنموونەی 1، هەنگاو بە هەنگاو

To watch every step, the animation runs a short version with count < 3. Follow the highlighted line, the value of count, the condition and the output.

بۆ ئەوەی هەموو هەنگاوێک ببینیت، ئەنیمەیشنەکە وەشانێکی کورت بە count < 3 جێبەجێ دەکات. شوێن دێڕە ڕەنگکراوەکە، نرخی count، و بکەوە.

Animation of the program count = 0; while count < 3: print("Programming is fun!"); count = count + 1; print("Done, count is", count). The highlighted line moves through the code. The count box shows 0, 1, 2, 3. A badge shows the condition: 0 < 3 True, 1 < 3 True, 2 < 3 True, then 3 < 3 False. The output panel gains one line "Programming is fun!" in each round, and finally "Done, count is 3".
Animation of the program count = 0; while count < 3: print("Programming is fun!"); count = count + 1; print("Done, count is", count). The highlighted line moves through the code. The count box shows 0, 1, 2, 3. A badge shows the condition: 0 < 3 True, 1 < 3 True, 2 < 3 True, then 3 < 3 False. The output panel gains one line "Programming is fun!" in each round, and finally "Done, count is 3".
The condition is checked before each round; the fourth check is False. پێش هەر دەپشکنرێت؛ پشکنینی چوارەم False دەبێت.CêIT
The short version in the animationوەشانە کورتەکەی ناو ئەنیمەیشنەکەexample1_short.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
count = 0
while count < 3:
    print("Programming is fun!")
    count = count + 1
print("Done, count is", count)
Quick checkپشکنینی خێراWhat is the value of count after the short version finishes?دوای تەواوبوونی وەشانە کورتەکە، نرخی count چییە؟

Counting from 1 to 10ژماردن لە 1 تا ⁦10⁩

The course report shows the same idea with numbers: print 1 to 10. This is its Python code as written in the report:

ڕاپۆرتی کۆرسەکە هەمان بیرۆکە بە ژمارە پیشان دەدات: 1 تا ⁦10⁩ چاپ بکە. ئەمە کۆدەکەیەتی بە ، وەک لە ڕاپۆرتەکەدا نووسراوە:

From the reportلە ڕاپۆرتەکەوەreport_while.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
i=1
while (i <= 10):
  print(i)
  i += 1

It works. Three details: i += 1 is the short form of i = i + 1 (see augmented assignment); the brackets around the condition are allowed but not needed in Python; and PEP 8, Python's style guide, asks for 4 spaces of indentation instead of 2. Here is the same loop in that style, with one extra line after the loop:

کار دەکات. سێ وردەکاری: i += 1 شێوە کورتەکەی i = i + 1ـە (بڕوانە شێوە کورتەکانی وەک +=)؛ کەوانەکانی دەوری ڕێگەپێدراون بەڵام لە پێویست نین؛ ⁦PEP 8⁩، ڕێنمایی شێوازی ، داوای 4 دەکات بۆ لەبری 2. ئەمە هەمان بەو شێوازە، لەگەڵ دێڕێکی زیادە دوای :

count_to_ten.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
i = 1
while i <= 10:
    print(i)
    i += 1
print("After the loop, i is", i)
Trace of the loop
ii <= 10printedچاپکراو
1True1
2True2
3True3
………
10True10
11False— the loop ends— کۆتایی دێت
Quick checkپشکنینی خێراWhy does the last line print 11 and not 10?بۆچی دوایین دێڕ 11 چاپ دەکات نەک 10؟

Three steps to design a loopسێ هەنگاو بۆ داڕشتنی خولێک

Writing a loop that works correctly is not easy at first. The course gives three steps. We follow them for a small task: count down 5, 4, 3, 2, 1 and then print Kick-off!.

نووسینی کە بە دروستی کار بکات لە سەرەتادا ئاسان نییە. کۆرسەکە سێ هەنگاو دەدات. بۆ ئەرکێکی بچووک پەیڕەویان دەکەین: لە 5ەوە بەرەو خوار بژمێرە، 4، 3، 2، 1 و پاشان Kick-off! چاپ بکە.

Loop design strategyستراتیژی داڕشتنی خول
  1. Find what repeatsئەوە بدۆزەرەوە کە دووبارە دەبێتەوە

    Identify the statements that need to be repeated: print the number, then make it one smaller.

    هەموو کە دەبێت دووبارە ببێتەوە دیاری بکە: ژمارەکە چاپ بکە، پاشان یەکێکی لێ کەم بکەرەوە.

    step1.txtTEXTType in a terminalلە تێرمیناڵدا بینووسە
    print(seconds)
    seconds -= 1
  2. Wrap them in a loopبیانخەرە ناو خولێکەوە

    Put those statements inside a loop that would run for ever: while True:. We will add the condition in the next step.

    بخەرە ناو کە بۆ هەمیشە کار دەکات: while True:. لە هەنگاوی دواتردا زیاد دەکەین.

    step2.txtTEXTType in a terminalلە تێرمیناڵدا بینووسە
    while True:
        print(seconds)
        seconds -= 1
  3. Write the condition and the control statementsمەرجەکە و ڕستەکانی کۆنترۆڵ بنووسە

    Replace True with the loop-continuation-condition (seconds > 0) and add the statements that control the loop: set the start value before the loop. The line seconds -= 1 already moves the loop towards its end.

    True بە (seconds > 0) بگۆڕە و کۆنترۆڵکردنی زیاد بکە: نرخی سەرەتا پێش دابنێ. دێڕی seconds -= 1 پێشتر بەرەو کۆتایی دەبات.

    countdown.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
    seconds = 5
    while seconds > 0:
        print(seconds)
        seconds -= 1
    print("Kick-off!")
Quick checkپشکنینی خێراIn step 3 of the countdown, which line makes sure the loop ends one day?لە هەنگاوی 3ی ژماردنە پێچەوانەکەدا، کام دێڕ دڵنیا دەکاتەوە کە ڕۆژێک کۆتایی دێت؟

Counters and accumulatorsژمێرەرەکان و گۆڕاوەکانی کۆکردنەوە

Two patterns appear in almost every loop. A counter counts rounds or things: it starts at 0 and grows by 1 (count += 1). An accumulator collects a result: it starts at 0 (or "" for text) and every round adds to it (total += number). Both are set before the loop, so they are not reset in every round.

دوو شێواز لە زۆربەی دەردەکەون. یان شتەکان دەژمێرێت: لە 0ەوە دەست پێدەکات و هەر جارێک 1 زیاد دەکات (count += 1). ئەنجامێک کۆ دەکاتەوە: لە 0ەوە دەست پێدەکات (یان "" بۆ دەق) و هەموو شتێکی دەخاتە سەر (total += number). هەردووکیان پێش دادەنرێن، بۆ ئەوەی لە هەموو سفر نەکرێنەوە.

Adding 1+2+⋯+1001 + 2 + \cdots + 100کۆکردنەوەی 1+2+⋯+1001 + 2 + \cdots + 100sum_to_n.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
n = 100
total = 0
number = 1
while number <= n:
    total += number
    number += 1
print("The sum of 1 to", n, "is", total)
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

The accumulator starts at 0.

لە 0ەوە دەست پێدەکات.

Lineدێڕی 3

number is the counter: 1, 2, …, 100.

number : 1، 2، …، ⁦100⁩.

Lineدێڕی 5

Add this round's number to the total.

ژمارەی ئەم بخەرە سەر کۆکە.

1+2+⋯+100=100⋅1012=50501 + 2 + \cdots + 100 = \frac{100 \cdot 101}{2} = 5050

A quick check with the well-known formula for the sum of the whole numbers 1 to n: n(n+1)2\frac{n(n+1)}{2}.

پشکنینێکی خێرا بە هاوکێشە ناسراوەکەی کۆی ژمارەکانی 1 تا n: n(n+1)2\frac{n(n+1)}{2}.

Quick checkپشکنینی خێراWhat would the program print if total = 0 were moved inside the loop (as the first line of the body)?ئەگەر total = 0 بگوازرێتەوە بۆ ناو (وەک یەکەم دێڕی )، چی چاپ دەکات؟

Stopping at a special valueوەستان لە نرخێکی تایبەت

Often you do not know how many values the user will type. Then agree on a special stop value, a sentinel value, such as 0. The loop runs while the value is not the sentinel. Read the first value before the loop and the next one at the end of the body, so the test always sees the newest value.

زۆرجار نازانیت چەند نرخ دەنووسێت. ئەوکات لەسەر نرخێکی تایبەتی وەستان ڕێک بکەون، ، وەک 0. کار دەکات تا نرخەکە نەبێت. یەکەم نرخ پێش بخوێنەرەوە و نرخی دواتر لە کۆتایی ، بۆ ئەوەی پشکنینەکە هەمیشە نوێترین نرخ ببینێت.

Adding up prices at the bazaarکۆکردنەوەی نرخەکان لە بازاڕshopping.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
total = 0
count = 0
price = int(input("Price (0 to stop): "))
while price != 0:
    total += price
    count += 1
    price = int(input("Price (0 to stop): "))
print("Items:", count)
print("Total:", total, "dinars")
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 3

The first value is read before the loop.

یەکەم نرخ پێش دەخوێنرێتەوە.

Lineدێڕی 4

0 is the sentinel: it is never a real price.

0 : هەرگیز نرخێکی ڕاستەقینە نییە.

Lineدێڕی 7

The next value is read at the end of the body.

نرخی دواتر لە کۆتایی دەخوێنرێتەوە.

Quick checkپشکنینی خێراWhat does the program print if the user types 0 at once?ئەگەر دەستبەجێ 0 بنووسێت، چی چاپ دەکات؟

Infinite loops and Ctrl+Cخولە بێکۆتاکان و ⁦Ctrl+C⁩

If the condition never becomes False, the loop never ends: an infinite loop. The program seems frozen, or prints without stopping. Stop it with the keyboard shortcut Ctrl+C in the terminal (online editors usually have a Stop button). Python then stops with KeyboardInterrupt.

ئەگەر هەرگیز False نەبێت، هەرگیز کۆتایی نایەت: . وا دەردەکەوێت کە ڕاوەستاوە، یان بێ وەستان چاپ دەکات. بە ⁦Ctrl+C⁩ لە ڕایبگرە (دەستکاریکەرە ئۆنلاینەکان زۆرجار دوگمەی Stopـیان هەیە). ئەوکات بە KeyboardInterrupt دەوەستێت.

Bug: the update is outside the loopباگ: نوێکردنەوەکە لە دەرەوەی خولەکەدایەfun_bug.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
count = 0
while count < 3:
    print("Programming is fun!")
count = count + 1
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 4

Not indented, so not in the body: count stays 0 and count < 3 is always True.

بەرەوناو نەچووە، بۆیە لە نییە: count بە 0 دەمێنێتەوە و count < 3 هەمیشە Trueە.

A second cause: a condition that jumps over its target. Counting in steps of 3 never makes n exactly 10:

هۆکارێکی دووەم: کە بەسەر ئامانجەکەیدا باز دەدات. ژماردن بە هەنگاوی 3 هەرگیز n ڕێک ناکات بە ⁦10⁩:

skips_ten.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
n = 0
while n != 10:
    print(n)
    n += 3
Fixed: use < instead of !=چاککراو: < لەبری != بەکار بهێنەskips_ten_fixed.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
n = 0
while n < 10:
    print(n)
    n += 3
Quick checkپشکنینی خێراA program prints the same line again and again and does not stop. What do you press in the terminal? هەمان دێڕ دووبارە و دووبارە چاپ دەکات و ناوەستێت. لە چی دادەگریت؟

Python Tutorial for Beginners 7: Loops and Iterations - For/While LoopsCorey Schafer · Englishئینگلیزی · 1.1M viewsبینین · 25K likesلایک · Partبەشی 5:05–10:14 (ofلە 10:14)

How a for loop repeats code once for each item, so you can see what one iteration (one pass through the loop) is.

دەبینیت چۆن بۆ هەر یەک جار کۆدەکە جێبەجێ دەکات و چییە: یەک جار جێبەجێبوونی ناوەڕۆکی .

Plays 5:05-10:14, the while-loop chapter: counter, break, and an infinite while True loop.

لە ⁦5:05⁩ تا ⁦10:14⁩ پەخش دەبێت، بەشی : ، break و بە while True.

Asking again until the input is rightدووبارە پرسیارکردن تا نێوخستەکە دروست دەبێت

Sometimes the program must ask again and again until the user types something valid. A common pattern is while True: — a loop that would run for ever — with a break that leaves it as soon as the input is good. (break is explained fully in Controlling loops.)

هەندێک جار دەبێت دووبارە و دووبارە بپرسێت تا شتێکی دروست دەنووسێت. شێوازێکی باو while True:ـە — کە بۆ هەمیشە کار دەکات — لەگەڵ breakـێک کە هەر کاتێک باش بوو لێی دەردەچێت. (break بە تەواوی لە کۆنترۆڵکردنی خولەکان ڕوون کراوەتەوە.)

Input validation with while Trueپشتڕاستکردنەوەی نێوخستە بە while Trueask_age.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
while True:
    age = int(input("Your age (1-120): "))
    if 1 <= age <= 120:
        break
    print("Please type a number from 1 to 120.")
print("Thank you. Your age is", age)
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 3

A chained comparison: 1 <= age and age <= 120 together.

: 1 <= age و age <= 120 پێکەوە.

Lineدێڕی 4

Good input: leave the loop.

باش: لە دەربچە.

Lineدێڕی 5

Bad input: say why, and the loop asks again.

خراپ: هۆکارەکە بڵێ و دووبارە دەپرسێت.

Quick checkپشکنینی خێراIn the age program, what happens when the user types 19?لە تەمەندا، کاتێک ⁦19⁩ دەنووسێت چی ڕوودەدات؟

Asking again until the input is rightدووبارە پرسیارکردن تا نێوخستەکە دروست دەبێت

Python has no do … while loopPython خولی do...whileـی نییە

Languages such as Java, C and C# have a do … while loop: it runs the body first and checks the condition afterwards, so the body always runs at least once — handy for a menu that must be shown before the user chooses. Python has no do … while. You get the same effect with while True: and a check with break at the end of the body.

زمانەکانی وەک Java، C و ⁦C#⁩ هەیە: سەرەتا جێبەجێ دەکات و دواتر دەپشکنێت، بۆیە هەمیشە لانیکەم یەکجار جێبەجێ دەبێت — بۆ کە دەبێت پێش هەڵبژاردنی پیشان بدرێت بەسوودە. نییە. هەمان ئەنجام بە while True: و پشکنینێک بە break لە کۆتایی بەدەست دەهێنیت.

A menu that is always shown at least onceمێنیوێک کە هەمیشە لانیکەم یەکجار پیشان دەدرێتmenu.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
while True:
    print("1) Say hello")
    print("2) Quit")
    choice = input("Choose: ")
    if choice == "1":
        print("Hello!")
    if choice == "2":
        break
print("Bye")
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2-4

The body runs before any test: the menu is always shown once.

پێش هەر پشکنینێک جێبەجێ دەبێت: هەمیشە یەکجار پیشان دەدرێت.

Lineدێڕی 7-8

The test is at the end of the body, as in do … while.

پشکنینەکە لە کۆتایی ، وەک لە do … while.

Quick checkپشکنینی خێراWhy is while True: + break at the end a good copy of do … while?بۆچی while True: + break لە کۆتاییدا لاساییەکی باشی do … whileـە؟

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

while loop mistakesهەڵەکانی
MistakeهەڵەWhat happensچی ڕوودەداتFixچارەسەر
while true:NameError: true is not a Python nameNameError: true ناوێکی نییەwhile True:
while count < 100SyntaxError: the colon is missingSyntaxError: دوو خاڵەکە (:) نییەwhile count < 100:
The update line is not indentedدێڕی نوێکردنەوە بەرەوناو نەچووەInfinite loop: the counter never changes: هەرگیز ناگۆڕێتIndent it into the bodyبیبە ناو
No update line at allهیچ دێڕێکی نوێکردنەوە نییەInfinite loopChange the loop variable in the body لە ناو بگۆڕە
i = 1, while i < 10:Prints 1 to 9: one too few (off-by-one)1 تا 9 چاپ دەکات: یەکێک کەمتر ()while i <= 10:
while n != 10: + n += 3Jumps over 10: infinite loopبەسەر ⁦10⁩دا باز دەدات: while n < 10:
Counter or total set inside the loop یان کۆ لە ناو دانراوەIt is reset every roundلە هەموو سفر دەکرێتەوەSet it before the loopپێش دایبنێ

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

ExerciseڕاهێنانTrace itبەدوایدا بچۆ

Without running it: what does this program print? Make a trace table with x and the condition.

بەبێ جێبەجێکردن: ئەم چی چاپ دەکات؟ بە x و دروست بکە.

triple.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
x = 1
while x < 50:
    x = x * 3
    print(x)
Show answerوەڵامەکە پیشان بدە
x beforex < 50new x, printedxی نوێ، چاپکراو
1True3
3True9
9True27
27True81
81False— end— کۆتایی
triple.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
x = 1
while x < 50:
    x = x * 3
    print(x)

81 is printed although it is not below 50: the test happens before the multiplication, when x is still 27.

⁦81⁩ چاپ دەکرێت هەرچەندە لە ⁦50⁩ کەمتر نییە: پشکنینەکە پێش لێکدانەکە ڕوودەدات، کاتێک x هێشتا ⁦27⁩ە.

ExerciseڕاهێنانTens on one lineدەیەکان لە یەک دێڕدا

Use a while loop to print 10 20 30 … 100 on one line.

بەکار بهێنە بۆ چاپکردنی ⁦10 20 30⁩ … ⁦100⁩ لە یەک دێڕدا.

Hintئاماژە

print(n, end=" ") keeps the output on one line.

print(n, end=" ") لە یەک دێڕدا دەهێڵێتەوە.

Show answerوەڵامەکە پیشان بدە
tens.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
n = 10
while n <= 100:
    print(n, end=" ")
    n += 10
print()
ExerciseڕاهێنانSum of the digitsکۆی تاکژمارەکان

Ask for a whole number and print the sum of its digits (for 2026: 2+0+2+6=102 + 0 + 2 + 6 = 10).

داوای بکە و کۆی چاپ بکە (بۆ ⁦2026⁩: 2+0+2+6=102 + 0 + 2 + 6 = 10).

Hintئاماژە

number % 10 is the last digit; number //= 10 removes it.

number % 10 دوایین ؛ number //= 10 لای دەبات.

Show answerوەڵامەکە پیشان بدە
digit_sum.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
number = int(input("A whole number: "))
digit_sum = 0
while number > 0:
    digit_sum += number % 10
    number //= 10
print("Sum of the digits:", digit_sum)

Each round takes the last digit (6, 2, 0, 2) and cuts it off. When nothing is left, number is 0 and the loop ends.

هەر دوایین وەردەگرێت (⁦6⁩، 2، 0، ⁦2⁩) و لای دەبات. کاتێک هیچ نەما، number دەبێتە 0 و کۆتایی دێت.

ExerciseڕاهێنانA number that reads the same backwardsژمارەیەک کە لە هەردوو ئاڕاستەوە وەک یەکە

12321 reads the same from both ends (a palindrome number), 123421 does not. Ask for a whole number and say whether it is a palindrome — with arithmetic only (% 10 and // 10), not with strings.

⁦12321⁩ لە هەردوو سەرەوە وەک یەک دەخوێنرێتەوە (ژمارەیەکی پالیندرۆم)، بەڵام ⁦123421⁩ نا. داوای بکە و بڵێ ئایا پالیندرۆمە — تەنها بە ژمێرکاری (% 10 و // 10)، نەک بە .

Hintئاماژە

Build the reversed number in a second variable: reverse = reverse * 10 + number % 10, then cut the last digit off with number //= 10.

ژمارە پێچەوانەکراوەکە لە دووەمدا دروست بکە: reverse = reverse * 10 + number % 10، پاشان دوایین بە number //= 10 لاببە.

Show answerوەڵامەکە پیشان بدە
PYTHONType in a terminalلە تێرمیناڵدا بینووسە
number = int(input("A whole number: "))
original = number
reverse = 0
while number > 0:
    reverse = reverse * 10 + number % 10
    number //= 10
if reverse == original:
    print(original, "is a palindrome")
else:
    print(original, "is not a palindrome:", reverse)
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

Keep a copy: the loop destroys number.

کۆپییەک هەڵبگرە: number تێک دەدات.

Lineدێڕی 5

Move the last digit of number to the end of reverse.

دوایین number بگوازەرەوە بۆ کۆتایی reverse.

Trace with 123421 بە ⁦123421⁩
number beforenumber پێشnumber % 10reverse afterreverse دوای
12342111
12342212
12344124
12331243
12212432
11124321

With 12321 the reversed number is 12321 again, so the program prints 12321 is a palindrome. The string version of this test is in Working with strings.

بە ⁦12321⁩ ژمارە پێچەوانەکراوەکە دیسان ⁦12321⁩ە، بۆیە 12321 is a palindrome چاپ دەکات. هەمان پشکنین بە لە کارکردن لەگەڵ زنجیرەنووسەکاندایە.

ExerciseڕاهێنانAll the factors of a numberهەموو دابەشکەرەکانی ژمارەیەک

Ask for a whole number above 0 and print all its factors on one line: the numbers from 1 to the number itself that divide it with no remainder (36 → 1 2 3 4 6 9 12 18 36).

داوای لە 0 گەورەتر بکە و هەموو دابەشکەرەکانی لە یەک دێڕدا چاپ بکە: ئەو ژمارانەی لە 1ەوە تا خودی ژمارەکە کە بەبێ دابەشی دەکەن (⁦36⁩ ← ⁦1 2 3 4 6 9 12 18 36⁩).

Hintئاماژە

Try every candidate d from 1 upwards; number % d == 0 means d divides number.

هەموو ژمارەیەکی d لە 1ەوە بەرەو سەر تاقی بکەرەوە؛ number % d == 0 واتە d ژمارەکە بەبێ دابەش دەکات.

Show answerوەڵامەکە پیشان بدە
PYTHONType in a terminalلە تێرمیناڵدا بینووسە
number = int(input("A whole number: "))
d = 1
while d <= number:
    if number % d == 0:
        print(d, end=" ")
    d += 1
print()
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

d is the counter: 1, 2, 3, … up to number.

d : 1، 2، 3، … تا number.

Lineدێڕی 4-5

Print d only when it divides number exactly.

d تەنها کاتێک چاپ بکە کە number ڕێک دابەش بکات.

A prime number such as 7 prints only 1 7. With for d in range(1, number + 1): the same loop is one line shorter — see The for loop and range().

ژمارەیەکی سەرەتایی وەک 7 تەنها 1 7 چاپ دەکات. بە for d in range(1, number + 1): هەمان یەک دێڕ کورتتر دەبێت — بڕوانە وانەی داهاتوو.

ExerciseڕاهێنانNumber the linesدێڕەکان ژمارە بکە

Change Example 1 so that count goes from 1 to 100 and every line starts with its number: 1 Programming is fun! … 100 Programming is fun!.

نموونەی 1 بگۆڕە بۆ ئەوەی count لە 1ەوە بڕوات تا ⁦100⁩ و هەر دێڕێک بە ژمارەکەی خۆی دەست پێ بکات: 1 Programming is fun! … 100 Programming is fun!.

Show answerوەڵامەکە پیشان بدە
numbered.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
count = 1
while count <= 100:
    print(count, "Programming is fun!")
    count = count + 1

Two changes: the start value (1 instead of 0) and the test (<= 100 instead of < 100). Changing only one of them gives 99 or 101 lines.

دوو گۆڕانکاری: نرخی سەرەتا (⁦1⁩ لەبری ⁦0⁩) و پشکنینەکە (<= 100 لەبری < 100). گۆڕینی تەنها یەکێکیان ⁦99⁩ یان ⁦101⁩ دێڕ دەدات.

ExerciseڕاهێنانCount down from 10لە ⁦10⁩ەوە بەرەو خوار بژمێرە

Rewrite the report's loop so that it prints 10, 9, …, 1 on one line.

ڕاپۆرتەکە بنووسەرەوە بۆ ئەوەی ⁦10⁩، 9، …، 1 لە یەک دێڕدا چاپ بکات.

Show answerوەڵامەکە پیشان بدە
count_down.pyPYTHONType in a terminalلە تێرمیناڵدا بینووسە
i = 10
while i >= 1:
    print(i, end=" ")
    i -= 1
print()

Summaryپوختە

  • A loop repeats statements. while is condition-controlled; for is count-controlled.
  • while condition: checks the condition before every iteration and stops when it is False. The whole body is indented.
  • Design in three steps: find what repeats, wrap it in while True:, then write the condition and the control statements.
  • Counters and accumulators start before the loop; a sentinel value can end input.
  • If the condition never becomes False you get an infinite loop: stop it with Ctrl+C.
  • while True: with break asks again until the input is valid, and replaces do … while.
  • دووبارە دەکاتەوە. while ؛ for .
  • while condition: پێش هەموو دەپشکنێت و کاتێک False بوو دەوەستێت. هەموو بەرەوناو دەچێت.
  • بە سێ هەنگاو دایبڕێژە: ئەوەی دووبارە دەبێتەوە بیدۆزەرەوە، بیخەرە ناو while True:، پاشان و کۆنترۆڵ بنووسە.
  • و پێش دەست پێدەکەن؛ دەتوانێت کۆتایی پێ بهێنێت.
  • ئەگەر هەرگیز False نەبێت دروست دەبێت: بە ⁦Ctrl+C⁩ ڕایبگرە.
  • while True: لەگەڵ break دووبارە دەپرسێت تا دروست دەبێت و جێگەی do … while دەگرێتەوە.
Review questionsپرسیارەکانی پێداچوونەوە
What is an iteration?خولانەوە چییە؟

One run of the loop body.

یەکجار جێبەجێبوونی .

A while loop's condition is False at the very start. How many times does the body run?مەرجی خولی while لە سەرەتاوە Falseە. جەستەی خولەکە چەند جار جێبەجێ دەبێت؟

Zero times.

سفر جار.

Why is while true: wrong?بۆچی while true: هەڵەیە؟

Python's constant is True; true gives a NameError.

نرخە جێگیرەکەی Trueـە؛ true NameError دەدات.

How do you get the effect of do … while in Python?چۆن کاریگەریی do … while لە Pythonـدا بەدەست دەهێنیت؟

while True: with an if … : break at the end of the body.

while True: لەگەڵ if … : break لە کۆتایی .

Videoڤیدیۆ

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

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

While loops in Python are easy! ♾️Bro Code · Englishئینگلیزی · 932K viewsبینین · 22K likesلایک

A while loop repeats while its condition stays True: asking again until the input is right, an infinite loop, and stopping at a special value.

دووبارە دەبێتەوە هەتا True بمێنێت: دووبارە پرسیارکردنەوە تا دروست بێت، و وەستان بە .

Whole video (6:58). Chapters: 0:50 example 1 (ask again for a name), 1:50 infinite loop, 2:25-6:35 three more examples.

هەموو ڤیدیۆکە ⁦(6:58)⁩. بەشەکان: ⁦0:50⁩ نموونەی 1 (دووبارە داوای ناو دەکاتەوە)، ⁦1:50⁩ ، ⁦2:25⁩ تا ⁦6:35⁩ سێ نموونەی تر.

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

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

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

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

The for Loop and range()خولی for و range()

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

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