Chapterبەش 6 · Lectureوانە 6.2

Loops: while, do...while, for, foreach, break and continueخولەکان: while و ⁦do...while⁩ و for و foreach و break و continue

How to repeat code in PHP: the while, do...while, for and foreach loops, how to stop or skip turns with break and continue, and how to avoid infinite loops.

چۆن کۆد لە PHP دووبارە بکەیتەوە: while و do...while و for و foreach، چۆن بە break و continue ڕابگریت یان بازی بدەیت و چۆن خۆت لە بپارێزیت.

25 min read24 خولەک خوێندنەوە · 27 stepsهەنگاو

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

  • Explain why loops are needed and name the four PHP loops.
  • Write while and do...while loops and explain how they differ.
  • Write a for loop with its three parts, separated by semicolons.
  • Go through an array with foreach, with and without keys.
  • Use break and continue, and avoid infinite loops.
  • Trace a loop by hand and predict its exact output.
  • ڕوونی بکەیتەوە بۆچی پێویستە و ناوی چوار PHP بهێنیت.
  • و بنووسیت و جیاوازییەکەیان ڕوون بکەیتەوە.
  • بە سێ بەشەکەیەوە بنووسیت، بە جیاکراوە.
  • بە foreach بە ناو بڕۆیت، بە و بێ .
  • break و continue بەکاربهێنیت و خۆت لە بپارێزیت.
  • شوێنی بە دەست بکەویت و ڕێکەکەی پێشبینی بکەیت.

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

Often you want the same block of code to run again and again. Imagine printing the numbers 1 to 100 with 100 echo lines. Instead of writing many almost equal lines, you write the code once and put it in a . A loop is a block of code that runs repeatedly, based on a condition. Every programming language has loops: they are repetition, one of the three of every (three building blocks).

زۆرجار دەتەوێت هەمان چەند جار لە دوای یەک جێبەجێ بێت. وێنا بکە ژمارەکانی 1 تا ⁦100⁩ بە ⁦100⁩ دێڕی echo چاپ بکەیت. لە جیاتی نووسینی چەندین دێڕی نزیکەی وەک یەک، کۆدەکە یەک جار دەنووسیت و دەیخەیتە ناو . کە بەپێی چەند جار دووبارە جێبەجێ دەبێت. هەموو هەیە: پێیان دەوترێت دووبارەکردنەوە و یەکێکن لە سێ هەموو (سێ بەردی بناغە).

The tea house waiterچایچیی چایخانە

A waiter in a tea house in Sulaymaniyah fills one glass of tea, then the next, then the next. He repeats the same action while there are empty glasses on the tray. When no empty glass is left, he stops. That is a loop: one action, repeated, with a clear rule for when to stop.

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

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

A waiter never forgets to stop. A program can: if the stop rule never becomes true, the loop runs forever.

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

PHP has four loops:

PHP چوار جۆر هەیە:

  • while – loops through a block of code as long as the condition is true.
  • do...while – runs the block once, and then repeats it as long as the condition is true.
  • for – loops through a block of code a specified number of times.
  • foreach – loops through a block of code for each element in an array.
  • – دووبارە دەکاتەوە مادام ڕاست بێت.
  • – بلۆکەکە یەک جار جێبەجێ دەکات، پاشان مادام ڕاست بێت دووبارەی دەکاتەوە.
  • – ژمارەیەکی دیاریکراو جار دووبارە دەکاتەوە.
  • – بۆ هەر لە جێبەجێ دەکات.

You can also control a loop from inside: break stops it, and continue skips one turn. These two keywords open the course's third module, Loops, Arrays and Functions. The module has three big ideas: control a loop, hold many values in one variable, and package code so you can use it again. Almost every page you write later uses all three. Arrays and functions come in the next chapter.

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

The while loopخولی while

A while loop checks a condition before each turn. If the condition is true, the body runs. Then PHP goes back and checks again. When the condition becomes false, the loop ends and PHP continues after it.

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

The format of a while loopشێوەی نووسینی خولی whileTEXT
while (condition is true) {
    statement;
}

The condition is a Boolean expression: an expression whose answer is true or false. One more rule is very important: something inside the body must change the condition, so that one day it becomes false.

: کە وەڵامەکەی true یان falseـە. یاسایەکی تریش زۆر گرنگە: شتێک لەناو دەبێت بگۆڕێت، تا ڕۆژێک ببێتە هەڵە.

PHP Programming Language Tutorial - Full CoursefreeCodeCamp.org · Englishئینگلیزی · 4.2M viewsبینین · 72K likesلایک · Partبەشی 3:05:09–3:15:18 (ofلە 4:36:39)

Watch PHP print HTML tags with echo, so the browser shows headings and lines that your PHP code made.

سەیر بکە چۆن بە چاپ دەکات، تا ئەو سەردێڕ و دێڕانە پیشان بدات کە کۆدی خۆت دروستی کردوون.

Plays only the 'While Loops' chapter (3:05:09–3:15:18).

تەنها بەشی While Loops لێدەدات (لە ⁦3:05:09⁩ تا ⁦3:15:18⁩).

The while loopخولی while

Example: count from 0 to 10نموونە: ژماردن لە 0 تا ⁦10⁩

while.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
    $count = 0;

    while ($count <= 10) {
        echo $count . ", ";
        $count++;
    }

    echo "<br /> Count: {$count}";
?>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

The starts at 0.

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

Lineدێڕی 4

Before every turn: is $count still 10 or less?

پێش هەموو : ئایا $count هێشتا ⁦10⁩ یان کەمترە؟

Lineدێڕی 5

Print the number and a comma. The dot . joins two strings ().

ژمارەکە و فاریزەیەک چاپ دەکات. خاڵ . دوو پێکەوە دەلکێنێت ().

Lineدێڕی 6

$count++ adds 1 (). Without this line the loop never ends.

$count++ 1 زیاد دەکات (). بەبێ ئەم دێڕە هەرگیز کۆتایی نایەت.

Lineدێڕی 9

After the loop. {$count} puts the variable's value inside the double-quoted string ().

دوای . {$count} دەخاتە ناو جووتەگوتەییەکەوە ().

Two details are worth noticing:

دوو وردەکاری شایەنی سەرنجن:

  • After the loop, $count is 11, not 10. The loop stops only when the test 11 <= 10 is false, so the counter has already gone one step past 10.
  • The output ends with an extra , , because every turn prints a comma, also the last one.
  • دوای ، $count یەکسانە بە ⁦11⁩، نەک ⁦10⁩. تەنها کاتێک دەوەستێت کە پشکنینی 11 <= 10 هەڵە بێت، بۆیە یەک هەنگاو لە ⁦10⁩ تێپەڕیوە.
  • بە , ێکی زیادە کۆتایی دێت، چونکە هەموو فاریزەیەک چاپ دەکات، دوایین .
Two flowcharts. Left, while: Start, then a diamond asks whether the condition ($count <= 10) is true. True leads to the body ({ … $count++; }), and an arrow goes back up to the diamond (repeat). False leads to After the loop. If the condition is false at the start, the body runs 0 times. Right, do … while: Start, then the body ({ … $x++; }) runs first, then a diamond asks whether ($x <= 5) is true. True goes back to the body; false leads to After the loop. The body always runs at least once. The line } while ($x <= 5); ends with a semicolon.دوو هێڵکاریی ڕەوت. لای چەپ، while: دەستپێک، پاشان لاکێشەیەکی لار دەپرسێت ئایا مەرجەکە ($count <= 10) ڕاستە. ئەگەر ڕاست بێت دەچێتە ناوەڕۆکی خولەکە ({ … $count++; }) و تیرێک دەگەڕێتەوە بۆ پرسیارەکە (دووبارە). ئەگەر هەڵە بێت دەچێتە دوای خولەکە. ئەگەر لە سەرەتاوە هەڵە بێت، ناوەڕۆکەکە هیچ جارێک جێبەجێ نابێت. لای ڕاست، do … while: دەستپێک، پاشان سەرەتا ناوەڕۆکەکە ({ … $x++; }) جێبەجێ دەبێت، پاشان پرسیار دەکات ئایا ($x <= 5) ڕاستە. ئەگەر ڕاست بێت دەگەڕێتەوە بۆ ناوەڕۆکەکە؛ ئەگەر هەڵە بێت دەچێتە دوای خولەکە. ناوەڕۆکەکە هەمیشە لانیکەم یەک جار جێبەجێ دەبێت. دێڕی } while ($x <= 5); بە خاڵبۆر کۆتایی دێت.
while checks first; do...while runs the body first and checks afterwards.while سەرەتا دەپشکنێت؛ do...while سەرەتا ناوەڕۆکی جێبەجێ دەکات و پاشان دەپشکنێت.CêIT
Quick checkپشکنینی خێرا$i = 1; while ($i < 4) { echo $i; $i++; } What is printed?$i = 1; while ($i < 4) { echo $i; $i++; } چی چاپ دەبێت؟

The while loopخولی while

An if statement inside a loopڕستەی if لەناو خولدا

You can put any code inside a loop, also an if...else. This example prints the word FIVE instead of the number 5:

دەتوانیت هەر کۆدێک بخەیتە ناو ، if...elseیش. ئەم نموونەیە لە جیاتی ژمارە 5 وشەی FIVE چاپ دەکات:

As in the lecture and the labوەک لە وانە و تاقیگەکەداwhile-five.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
    $count = 0;

    while ($count <= 10) {
        if ($count == 5) {
            echo "FIVE";
        } else {
            echo $count . ", ";
        }
        $count++;
    }

    echo "<br /> Count: {$count}";
?>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 5

In every turn, PHP checks: is this turn the number 5?

لە هەموو PHP دەپشکنێت: ئایا ئەم ژمارە 5ـە؟

Lineدێڕی 6

Yes: print FIVE (but no comma after it).

بەڵێ: FIVE چاپ دەکات (بەڵام بێ فاریزە لە دوایەوە).

Lineدێڕی 8

No: print the number and a comma.

نەخێر: ژمارەکە و فاریزەیەک چاپ دەکات.

Lineدێڕی 10

The counter grows in every turn, inside or outside the if.

لە هەموو زیاد دەبێت، چ لەناو if بێت چ نا.

Look at the output: FIVE6. The word FIVE has no comma or space after it, so it sticks to the next number. Fix it by printing "FIVE, ":

سەیری بکە: FIVE6. وشەی FIVE هیچ فاریزە و لە دوا نییە، بۆیە بە ژمارەی دواترەوە دەلکێت. بە چاپکردنی "FIVE, " چاکی بکە:

while-five-fixed.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
    $count = 0;

    while ($count <= 10) {
        if ($count == 5) {
            echo "FIVE, ";
        } else {
            echo $count . ", ";
        }
        $count++;
    }

    echo "<br /> Count: {$count}";
?>

The while loopخولی while

Worked example: even and odd numbers from 0 to 30نموونەی چارەسەرکراو: ژمارە جووت و تاکەکان لە 0 تا ⁦30⁩

A student's practice notes contain a nice while exercise: print the even numbers from 0 to 30 and their sum, then the odd numbers and their sum. A number is even when the remainder of dividing it by 2 is 0. The operator % gives that remainder: 7 % 2 is 1, 8 % 2 is 0.

تێبینییەکانی ڕاهێنانی خوێندکارێک ڕاهێنانێکی جوانی whileی تێدایە: ژمارە جووتەکان لە 0 تا ⁦30⁩ و کۆکەیان چاپ بکە، پاشان ژمارە تاکەکان و کۆکەیان. ژمارەیەک جووتە کاتێک دابەشکردنی بەسەر 2دا 0 بێت. % ئەو دەدات: 7 % 2 دەکاتە 1، 8 % 2 دەکاتە 0.

The student's solution (tidied)چارەسەرەکەی خوێندکارەکە (ڕێکخراو)even-odd.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
    echo "Even numbers are: <br>";
    displayEvenNumbers();
    echo "<hr>";
    echo "Odd numbers are: <br>";
    displayOddNumbers();

    function displayEvenNumbers() {
        $count = 0;
        $sum = 0;
        while ($count <= 30) {
            if ($count % 2 == 0) {
                echo $count . "<br>";
                $sum += $count;
            }
            $count++;
        }
        echo "Sum of even numbers = " . $sum;
    }

    function displayOddNumbers() {
        $count = 0;
        $sum = 0;
        while ($count <= 30) {
            if ($count % 2 == 1) {
                echo $count . "<br>";
                $sum += $count;
            }
            $count++;
        }
        echo "Sum of odd numbers = " . $sum;
    }
?>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 3, 6

Call the two functions. They are defined lower in the file; PHP allows that.

بانگی دوو دەکات. لە خوارەوەی پێناسە کراون؛ PHP ڕێگەی پێدەدات.

Lineدێڕی 8

function gives a name to a block of code. You will learn this in the Functions lesson.

function ناوێک دەدات بە . ئەمە لە وانەی کردارەکان فێر دەبیت.

Lineدێڕی 12

$count % 2 == 0: the number is even. 0 counts as even.

$count % 2 == 0: ژمارەکە جووتە. 0 بە جووت دادەنرێت.

Lineدێڕی 14

$sum += $count adds the number to the sum.

$sum += $count ژمارەکە دەخاتە سەر کۆکە.

Lineدێڕی 25

% 2 == 1: the number is odd.

% 2 == 1: ژمارەکە تاکە.

If you only need the two sums, one loop is enough. An if...else inside it decides where each number goes:

ئەگەر تەنها دوو کۆکەکەت پێویستە، یەک بەسە. if...elseێک لەناویدا بڕیار دەدات هەر ژمارەیەک بۆ کوێ بچێت:

even-odd-sums.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
    $count = 0;
    $evenSum = 0;
    $oddSum = 0;

    while ($count <= 30) {
        if ($count % 2 == 0) {
            $evenSum += $count;     // 0, 2, 4, ... 30
        } else {
            $oddSum += $count;      // 1, 3, 5, ... 29
        }
        $count++;
    }

    echo "Sum of even numbers = $evenSum<br>";
    echo "Sum of odd numbers = $oddSum";
?>

The do...while loopخولی do...while

The do...while loop will always run the block of code once. Then it checks the condition, and it repeats the loop while the condition is true.

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

SyntaxداڕستنTEXT
do {
    code to be executed;
} while (condition is true);
Tasting the soupتامکردنی شۆربا

When you cook soup, you always taste it once before you decide anything. Then: while it needs salt, add salt and taste again. The tasting happens at least once, even if the soup was perfect from the start. That is do...while.

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

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

In real life you may decide not to taste at all. A do...while loop has no choice: the first turn always happens.

لە ژیانی ڕاستەقینەدا لەوانەیە بڕیار بدەیت هەر تامی نەکەیت. بەڵام هیچ هەڵبژاردنێکی نییە: یەکەم هەمیشە ڕوودەدات.

Example: print 1 to 5

نموونە: چاپکردنی 1 تا 5

do-while.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
    $x = 1;

    do {
        echo "The number is: $x <br>";
        $x++;
    } while ($x <= 5);
?>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 4-7

The body runs first, with $x = 1.

ناوەڕۆکەکە سەرەتا جێبەجێ دەبێت، بە $x = 1.

Lineدێڕی 5

$x inside double quotes is replaced by its value; <br> makes a new line in the browser.

$x لەناو جووتەگوتەدا بە دەگۆڕدرێت؛ <br> لە دێڕێکی نوێ دروست دەکات.

Lineدێڕی 7

After each turn: is $x still 5 or less? Note the semicolon.

دوای هەر : ئایا $x هێشتا 5 یان کەمترە؟ ئاگاداری بە.

The do...while loopخولی do...while

The body always runs at least onceناوەڕۆکەکە هەمیشە لانیکەم یەک جار جێبەجێ دەبێت

This practice question shows the difference clearly. $x starts at 10, so the condition $x <= 5 is false from the start. What is printed?

ئەم پرسیارەی ڕاهێنان جیاوازییەکە بە ڕوونی پیشان دەدات. $x لە ⁦10⁩ـەوە دەست پێدەکات، بۆیە $x <= 5 هەر لە سەرەتاوە هەڵەیە. چی چاپ دەبێت؟

do-while-once.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<!DOCTYPE html>
<html>
<body>

<?php
    $x = 10;

    do {
        echo $x;
        $x++;
    } while ($x <= 5);

    echo "<br />" . $x;
?>

</body>
</html>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 8-11

The body runs once anyway: it prints 10 and makes $x 11.

ناوەڕۆکەکە بە هەر حاڵ یەک جار جێبەجێ دەبێت: ⁦10⁩ چاپ دەکات و $x دەکات بە ⁦11⁩.

Lineدێڕی 11

Now the check: 11 <= 5 is false, so the loop ends.

ئێستا پشکنین: 11 <= 5 هەڵەیە، بۆیە کۆتایی دێت.

Lineدێڕی 13

Prints a line break and 11.

دێڕێکی نوێ و ⁦11⁩ چاپ دەکات.

Now the same numbers with a normal while loop. Here the check comes first, so the body never runs and $x stays 10:

ئێستا هەمان ژمارەکان بە ئاسایی. لێرەدا پشکنین سەرەتا دێت، بۆیە ناوەڕۆکەکە هەرگیز جێبەجێ نابێت و $x بە ⁦10⁩ دەمێنێتەوە:

while-never.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
    $x = 10;

    while ($x <= 5) {
        echo $x;
        $x++;
    }

    echo "<br />" . $x;
?>
Quick checkپشکنینی خێرا$n = 0; do { echo "Hi "; } while ($n > 0); How many times is Hi printed?$n = 0; do { echo "Hi "; } while ($n > 0); چەند جار Hi چاپ دەبێت؟

The for loopخولی for

A for loop is used when you know how many times the code should run. All the counting work is written in one line, in three parts:

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

The format of a for loopشێوەی نووسینی خولی forTEXT
for (init counter; test counter; increment counter) {
    code to be executed;
}
  1. init counter – gives the loop its first value. It runs once, before everything else.
  2. test counter – is checked before every turn. If it is true, the loop continues. If it is false, the loop ends.
  3. increment counter – changes the counter after every turn. Usually it increases it ($i++), but it can also decrease it ($i--) or change it by any step ($i += 2).
  1. ⁦init counter⁩ – یەکەم دەداتە . یەک جار جێبەجێ دەبێت، پێش هەموو شتێک.
  2. ⁦test counter⁩ – پێش هەموو دەپشکنرێت. ئەگەر true بێت، بەردەوام دەبێت. ئەگەر false بێت، کۆتایی دێت.
  3. ⁦increment counter⁩ – دوای هەموو دەگۆڕێت. زۆرجار زیادی دەکات ($i++)، بەڵام دەتوانێت کەمیشی بکاتەوە ($i--) یان بە هەر هەنگاوێک بیگۆڕێت ($i += 2).
The line for ($count = 0; $count <= 10; $count++) { with its three parts in coloured boxes. Part 1, $count = 0, is the start and runs once, first. Part 2, $count <= 10, is the test, checked before every turn. Part 4, $count++, is the change, which runs after every turn. Part 3 is the body, echo $count . ", ";, which runs while the test is true. Below, a flow: 1 $count = 0, then 2 the test; true leads to 3 the body, then 4 $count++, and an arrow goes back to the test for the next turn; false leads to After the loop. Order: 1, then 2, 3, 4, 2, 3, 4 … until test 2 is false. The parts are separated by semicolons, not commas.دێڕی for ($count = 0; $count <= 10; $count++) { و سێ بەشەکەی لەناو چوارگۆشەی ڕەنگاوڕەنگدا. بەشی 1، $count = 0، دەستپێکە و تەنها یەک جار، یەکەم شت، جێبەجێ دەبێت. بەشی 2، $count <= 10، پشکنینە و پێش هەموو خولانەوەیەک دەکرێت. بەشی 4، $count++، گۆڕینە و دوای هەموو خولانەوەیەک جێبەجێ دەبێت. بەشی 3 ناوەڕۆکەکەیە، echo $count . ", ";، کە تا پشکنینەکە ڕاست بێت جێبەجێ دەبێت. لە خوارەوە ڕێگای کارەکە: 1 $count = 0، پاشان 2 پشکنین؛ ئەگەر ڕاست بێت دەچێتە 3 ناوەڕۆک، پاشان 4 $count++ و تیرێک دەگەڕێتەوە بۆ پشکنین بۆ خولانەوەی داهاتوو؛ ئەگەر هەڵە بێت دەچێتە دوای خولەکە. ڕیزبەندی: 1، پاشان 2، 3، 4، 2، 3، 4 … تا پشکنینی 2 هەڵە دەبێت. بەشەکان بە خاڵبۆر (;) جیا دەکرێنەوە، نەک بە فاریزە (,).
The three parts of a for loop and the order in which PHP runs them.سێ بەشەکەی و ئەو ڕیزبەندییەی PHP جێبەجێیان دەکات.CêIT
PHP Full Course for free 🐘Bro Code · Englishئینگلیزی · 3M viewsبینین · 65K likesلایک · Partبەشی 1:29:48–1:33:19 (ofلە 4:00:00)

How to create a function, send values in with arguments, and get a result back with return, with a short practice example.

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

Plays 1:29:48–1:33:19: counting up and down. Stops before the form exercise.

لە ⁦1:29:48⁩ تا ⁦1:33:19⁩ پەخش دەبێت: ژماردن بەرەو سەر و بەرەو خوار؛ پێش ڕاهێنانەکەی ڕادەوەستێت.

The for loopخولی for

Example: print 0 to 10نموونە: چاپکردنی 0 تا ⁦10⁩

for.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
    for ($count = 0; $count <= 10; $count++) {
        echo $count . ", ";
    }
?>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

Start at 0; continue while $count <= 10; add 1 after each turn.

لە 0ـەوە دەست پێدەکات؛ مادام $count <= 10 بێت بەردەوام دەبێت؛ دوای هەر 1 زیاد دەکات.

Lineدێڕی 3

The body: print the number and a comma.

ناوەڕۆکەکە: ژمارەکە و فاریزەیەک چاپ دەکات.

This is the same output as the while example. Both loops do the same three jobs; for just puts them together in one line:

ئەمە هەمان نموونەکەی whileـە. هەردوو هەمان سێ کار دەکەن؛ for تەنها لە یەک دێڕدا کۆیان دەکاتەوە:

The same counting loop written two waysهەمان ژماردن بە دوو شێوە نووسراو
Jobکارwhilefor
1. start the counter1. دەستپێکردنی $count = 0; (before the loop)for ($count = 0; ...
2. test2. پشکنینwhile ($count <= 10)...; $count <= 10; ...
3. change the counter3. گۆڕینی $count++; (last line of the body)...; $count++)

The for loopخولی for

Counting down and counting in stepsژماردن بەرەو خوار و ژماردن بە هەنگاو

for-steps.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
    for ($i = 10; $i > 7; $i--) {
        echo $i . " ";
    }
    echo "<br>";

    for ($i = 0; $i <= 10; $i += 2) {
        echo $i . " ";
    }
?>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

Start at 10 and decrease with $i-- () while $i > 7.

لە ⁦10⁩ـەوە دەست پێدەکات و بە $i-- کەم دەکاتەوە () مادام $i > 7 بێت.

Lineدێڕی 7

$i += 2 adds 2 each time: the even numbers.

$i += 2 هەر جارێک 2 زیاد دەکات: ژمارە جووتەکان.

Each of the three parts may also be empty. Here the counter was created before the loop, so the first part is left empty (but its semicolon stays):

هەر یەکێک لە سێ بەشەکە دەتوانێت بەتاڵیش بێت. لێرەدا پێش دروست کراوە، بۆیە بەشی یەکەم بەتاڵ ماوەتەوە (بەڵام دەمێنێتەوە):

for-empty-part.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
    $i = 10;

    for (; $i > 7; $i--) {
        echo $i . " ";
    }
?>

The for loopخولی for

What happens with commasبە فاریزە چی ڕوودەدات

Wrong: commas between the partsنادروست: فاریزە لە نێوان بەشەکانداfor-commas.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
    for ($count = 0, $count <= 10, $count++) {
        echo $count . ", ";
    }
?>
Quick checkپشکنینی خێراHow many times does for ($i = 5; $i < 8; $i++) run its body?for ($i = 5; $i < 8; $i++) چەند جار ناوەڕۆکەکەی جێبەجێ دەکات؟

The for loopخولی for

A loop inside a loopخولێک لەناو خولێکدا

The body of a loop can contain another loop. This is a . For every turn of the outer loop, the inner loop runs completely. Here the outer loop makes 3 rows, and the inner loop prints 4 numbers in each row: a small multiplication table.

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

nested.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
    for ($row = 1; $row <= 3; $row++) {
        for ($col = 1; $col <= 4; $col++) {
            echo $row * $col . " ";
        }
        echo "<br>";
    }
?>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

Outer loop: $row = 1, 2, 3.

دەرەوە: $row = 1، 2، 3.

Lineدێڕی 3-5

Inner loop: $col = 1 to 4 for each row.

ناوەوە: $col = 1 تا 4 بۆ هەر ڕیزێک.

Lineدێڕی 6

After the inner loop ends, start a new line.

دوای کۆتاییهاتنی ناوەوە، ڕیزێکی نوێ دەست پێدەکات.

Nested loops are used a lot with tables of data, for example . You will meet them again in the Multidimensional arrays lesson.

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

The foreach loopخولی foreach

The foreach loop is made for (lists of values). It loops through the array and takes each element in turn. You do not need a counter or a test: foreach starts at the first element and stops by itself after the last one.

بۆ (لیستی ) دروست کراوە. بە ناو دەڕوات و یەک لە دوای یەک وەردەگرێت. پێویستت بە یان پشکنین نییە: foreach لە یەکەم دەست پێدەکات و دوای دوایین خۆی دەوەستێت.

SyntaxداڕستنTEXT
foreach ($array as $value) {
    code to be executed;
}

Read it like an English sentence: "For each item in $array, make the value available as $value." In every turn, the value of the current element is copied into $value, and then PHP moves on to the next element. You can choose any name instead of $value.

وەک قسەیەکی ئینگلیزی بیخوێنەوە: «بۆ هەر لە $arrayدا، لە $valueدا دابنێ.» لە هەموو ئێستا دەخرێتە ناو $valueـەوە، پاشان PHP دەچێتە سەر دواتر. دەتوانیت لە جیاتی $value هەر ناوێک هەڵبژێریت.

"For each item in $array1, make the value available as $num"«بۆ هەر دانەیەک لە ⁦$array1⁩، نرخەکەی لە ⁦$num⁩ دابنێ»foreach-numbers.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
    $array1 = array(1, 2, 3);

    foreach ($array1 as $num) {
        echo $num;
    }
?>

The foreach loopخولی foreach

Example: colours inside an HTML pageنموونە: ڕەنگەکان لەناو لاپەڕەیەکی HTMLـدا

colors.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<html>
<body>

<?php
    $colors = array("Red", "Green", "Blue", "Yellow");

    foreach ($colors as $value) {
        echo "$value <br>";
    }
?>

</body>
</html>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 5

array() creates an indexed array with four strings.

array() بە چوار دروست دەکات.

Lineدێڕی 7

Four elements, so four turns. In turn 1 $value is "Red", in turn 2 "Green", …

چوار ، کەواتە چوار . لە 1دا $value یەکسانە بە "Red"، لە 2دا "Green"، …

Lineدێڕی 8

Print the value and a line break.

و دێڕێکی نوێ چاپ دەکات.

The array $colors holds four elements: "Red" at index 0, "Green" at 1, "Blue" at 2 and "Yellow" at 3. An arrow goes down from each element to one turn: Turn 1 has $value = "Red", Turn 2 $value = "Green", Turn 3 $value = "Blue", Turn 4 $value = "Yellow". Under each turn the browser shows the value: Red, Green, Blue, Yellow. When there are no more values, foreach stops by itself.ڕیزەکەکە $colors چوار دانە هەڵدەگرێت: "Red" لە ئیندێکسی 0، "Green" لە 1، "Blue" لە 2 و "Yellow" لە 3. لە هەر یەکێکیانەوە تیرێک دێتە خوارەوە بۆ یەک خولانەوە: خولانەوەی 1 ـ $value = "Red"، خولانەوەی 2 ـ $value = "Green"، خولانەوەی 3 ـ $value = "Blue"، خولانەوەی 4 ـ $value = "Yellow". لە ژێر هەر خولانەوەیەکدا تۆڕگەڕ ئەو نرخە پیشان دەدات: Red، Green، Blue، Yellow. کاتێک هیچ نرخێکی تر نەما، foreach خۆی دەوەستێت.
foreach gives you the elements one by one, in order.foreach یەک لە دوای یەک و بە ڕیزبەندی دەداتە دەستت.CêIT

The name after as is your choice. The summary notes write the same loop as foreach($colors as $color) and print "$color <br>"; the result is identical.

ئەو ناوەی دوای as دێت هەڵبژاردنی خۆتە. تێبینییەکانی پوختە هەمان بە foreach($colors as $color) دەنووسن و "$color <br>" چاپ دەکەن؛ ئەنجامەکە هەمان شتە.

The foreach loopخولی foreach

Keys and values: $key => $valueکلیل و نرخ: $key => $value

The lecture says that foreach goes through each key/value pair, but it shows only the value form. With => you get the too. In an indexed array the key is the (0, 1, 2, …); in an it is the name you gave:

وانەکە دەڵێت foreach بە ناو هەموو دەڕوات، بەڵام تەنها شێوەی پیشان دەدات. بە => وەردەگریت. لە (⁦0⁩، 1، 2، …)؛ لە ئەو ناوەیە کە خۆت داتناوە:

foreach-key.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
    $ages = array("Aram" => 20, "Shilan" => 22, "Karwan" => 19);

    foreach ($ages as $name => $age) {
        echo "$name is $age years old.<br>";
    }

    $colors = array("Red", "Green", "Blue");

    foreach ($colors as $index => $color) {
        echo "Index $index: $color<br>";
    }
?>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

An associative array: names are the keys, ages are the values.

: ناوەکان ، تەمەنەکان .

Lineدێڕی 4

$name gets the key, $age gets the value.

$name وەردەگرێت، $age .

Lineدێڕی 10

In an indexed array the keys are 0, 1, 2.

لە 0، 1، 2ن.

Quick checkپشکنینی خێرا$prices = array("tea" => 500, "coffee" => 1500); What does foreach ($prices as $k => $v) { echo $k; } print?$prices = array("tea" => 500, "coffee" => 1500); ئەم کۆدە چی چاپ دەکات؟ foreach ($prices as $k => $v) { echo $k; }

The foreach loopخولی foreach

Two old statements about foreachدوو قسەی کۆن دەربارەی foreach

You will use foreach all the time in the next chapter, Arrays.

لە بەشی داهاتوودا، ڕیزەکەکان، هەمیشە foreach بەکاردەهێنیت.

break and continuebreak و continue

Sometimes you want to stop a loop early, or skip just one turn. Two do this from inside the loop body: break stops the loop, and continue skips one turn.

هەندێک جار دەتەوێت زووتر ڕابگریت، یان تەنها یەک باز بدەیت. دوو ئەم کارە لە ناوەوەی دەکەن: break ڕادەگرێت و continue یەک بازدەدات.

break and continuebreak و continue

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

The break keyword ends the loop early. You put it inside the loop body, usually inside an if. When break runs, the loop stops at once and PHP continues after the loop.

break کە پێش وەخت کۆتایی پێدێنێت. دەیخەیتە ناو ناوەڕۆکی ، زۆرجار لەناو ifێکدا. کاتێک break جێبەجێ دەبێت، دەستبەجێ دەوەستێت و PHP لە دوای بەردەوام دەبێت.

In this example the condition $i == 3 becomes true when the counter reaches 3, and the loop ends:

لەم نموونەیەدا $i == 3 کاتێک ڕاست دەبێت کە دەگاتە 3 و کۆتایی دێت:

break.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
    for ($i = 0; $i < 10; $i++) {
        if ($i == 3) {
            break;
        }
        echo "The value is $i <br>";
    }
?>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 3-5

When $i is 3, break runs before the echo.

کاتێک $i یەکسانە بە 3، break پێش echo جێبەجێ دەبێت.

Lineدێڕی 6

So 3 to 9 are never printed.

بۆیە 3 تا 9 هەرگیز چاپ نابن.

break works in every loop, not only in for. The same example with while:

break لە هەموو کار دەکات، نەک تەنها لە for. هەمان نموونە بە while:

break-while.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
    $i = 0;

    while ($i < 10) {
        if ($i == 3) {
            break;
        }
        echo "The value is $i <br>";
        $i++;
    }
?>

break and continuebreak و continue

continue: skip this turn, keep goingcontinue: ئەم خولانەوەیە بازبدە، بەردەوام بە

The continue keyword stops only the current turn of the loop, not the whole loop. The rest of the body is skipped for this turn, and the next turn starts.

continue کە تەنها ئێستا ڕادەگرێت، نەک هەموو . باقی ناوەڕۆکەکە بۆ ئەم بازی بەسەردا دەدرێت و داهاتوو دەست پێدەکات.

continue.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
    for ($i = 0; $i < 10; $i++) {
        if ($i == 3) {
            continue;
        }
        echo "The value is $i <br>";
    }
?>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 3-5

When $i is 3, continue jumps straight to the next turn ($i++, then the test).

کاتێک $i یەکسانە بە 3، continue ڕاستەوخۆ بازدەدات بۆ داهاتوو ($i++، پاشان پشکنین).

Lineدێڕی 6

So only 3 is missing. All ten turns still happen.

بۆیە تەنها 3 دیار نییە. هەر دە هەر ڕوودەدەن.

break and continuebreak و continue

break or continue?break یان continue؟

Two rows of boxes for $i = 0 to 9. Row break: $i = 0, 1 and 2 are printed; at $i = 3 the loop ends; $i = 4, 5 and 6 … 9 never run. Row continue: $i = 0, 1 and 2 are printed; $i = 3 is skipped; $i = 4, 5 and 6 … 9 are printed. Below are the two loops: if ($i == 3) { break; } and if ($i == 3) { continue; }.دوو هێڵ چوارگۆشە بۆ $i = 0 تا 9. هێڵی break: $i = 0 و 1 و 2 چاپ دەکرێن؛ لە $i = 3 هەموو کارەکە دەوەستێت؛ $i = 4 و 5 و 6 … 9 هەرگیز جێبەجێ نابن. هێڵی continue: $i = 0 و 1 و 2 چاپ دەکرێن؛ $i = 3 بازی بەسەردا دەدرێت؛ $i = 4 و 5 و 6 … 9 چاپ دەکرێن. لە خوارەوە دوو کۆدەکە هەن: if ($i == 3) { break; } و if ($i == 3) { continue; }.
break leaves the loop for good; continue skips only one turn.break بە یەکجاری لە دەردەچێت؛ continue تەنها یەک بازدەدات.CêIT
break stops, continue skipsbreak ڕادەگرێت، continue بازدەدات
KeywordوشەWhat happens when it runsکاتێک جێبەجێ دەبێت چی ڕوودەداتTurns that still runچەند هێشتا جێبەجێ دەبن
breakleaves the loop for goodبە یەکجاری لە دەردەچێتnone after itهیچ لە دوایەوە
continueskips the rest of this turn and jumps to the next turnباقی ئەم بازدەدات و دەچێتە داهاتووall the remaining turnsهەموو ماوەکان
Quick checkپشکنینی خێراfor ($i = 1; $i <= 5; $i++) { if ($i == 4) { continue; } echo $i; } What is printed?for ($i = 1; $i <= 5; $i++) { if ($i == 4) { continue; } echo $i; } چی چاپ دەبێت؟

break and continuebreak و continue

A loop that only break can stopخولێک کە تەنها break دەتوانێت ڕایبگرێت

A practice note asks: what does this page show? while (true) would run forever, but its body is if (true) break;, so the loop ends in its very first turn. Nothing is printed, and there is no error. Note also that neither the loop nor the if has braces: each controls just the one statement after it.

تێبینییەکی ڕاهێنان دەپرسێت: ئەم پەڕەیە چی پیشان دەدات؟ while (true) بۆ هەمیشە بەردەوام دەبوو، بەڵام ناوەڕۆکەکەی if (true) break;ـە، بۆیە هەر لە یەکەم کۆتایی دێت. هیچ شتێک چاپ نابێت و هیچ هەڵەیەکیش نییە. هەروەها ئاگاداربە کە نە و نە if کەوانەی لوولییان نییە: هەر یەکەیان تەنها ئەو یەک کۆنترۆڵ دەکات کە لە دوایەوە دێت.

while-true.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<!DOCTYPE html>
<html>
<body>

<?php
    while (true)
        if (true)
            break;
?>

</body>
</html>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 6

true is always true: this loop has no normal end.

true هەمیشە ڕاستە: ئەم هیچ کۆتاییەکی ئاسایی نییە.

Lineدێڕی 7-8

The body: one if statement, whose body is one break statement.

ناوەڕۆکەکە: یەک if، کە ناوەڕۆکەکەی یەک breakـە.

while (true) with a break inside is a real pattern. It is useful when the stop rule is easier to check in the middle of the body:

while (true) لەگەڵ breakێک لە ناویدا شێوازێکی ڕاستەقینەیە. بەسوودە کاتێک پشکنینی یاسای وەستان لە ناوەڕاستی ناوەڕۆکەکەدا ئاسانترە:

while-true-break.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
    $n = 1;

    while (true) {
        if ($n > 3) {
            break;          // the only way out of this loop
        }
        echo "Turn $n<br>";
        $n++;
    }
    echo "Finished";
?>

break and continuebreak و continue

Extra: break 2 and continue 2زیادە: break 2 و continue 2

Which loop should I use?کام خول بەکاربهێنم؟

The four loops comparedبەراوردی چوار
LoopChecks the condition… دەپشکنێت…Minimum turnsکەمترین Best forباشترینە بۆ
whilebefore each turnپێش هەر 0repeating while something is true, when you do not know how many turnsدووبارەکردنەوە مادام شتێک ڕاستە، کاتێک نازانیت چەند
do...whileafter each turnدوای هەر 1when the body must run at least onceکاتێک ناوەڕۆکەکە دەبێت لانیکەم یەک جار جێبەجێ بێت
forbefore each turnپێش هەر 0counting: a known number of turnsژماردن: ژمارەیەکی زانراوی
foreachno condition: one turn per elementهیچ نییە: یەک بۆ هەر 0going through every element of an arrayڕۆیشتن بە ناو هەموو

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

  • Forgetting to change the counter in a while loop: an . See the warning.
  • Off by one: $i <= 10 runs one more turn than $i < 10. Count the turns by hand for the first and last values.
  • Commas instead of semicolons in the for header: parse error. See the example.
  • A semicolon right after the header, as in for ($i = 0; $i < 3; $i++); or while ($x < 3);. The ; is an empty body. The for version runs its empty body 3 times and then runs the block below only once; the while version never ends, because nothing changes $x.
  • Forgetting the semicolon after do...while: } while ($x <= 5); needs it.
  • Using continue in a while loop before the counter changes: an infinite loop. See the warning.
  • Giving foreach something that is not an array: PHP shows a warning and skips the loop. See the example.
  • Curly quotes copied from slides: most loop examples in Lecture 3 and Lab 3 used “ ”, which causes a parse error. All code here uses straight quotes.
  • The missing space: echo "The number is" . $i . "<br>"; prints The number is1, The number is2 … The dot joins the strings exactly as they are and never adds a space. (The lecture slide's screenshot shows a space that its code does not print.) Put the space inside the quotes, "The number is ", or write "The number is $i<br>".
  • لەبیرکردنی گۆڕینی لە whileدا: . سەیری بەشەکە بکە.
  • یەک زیاتر یان کەمتر: $i <= 10 یەک زیاتر لە $i < 10 جێبەجێ دەکات. بۆ یەکەم و دوایین بە دەست بژمێرە.
  • فاریزە لە جیاتی لە سەرەوەی forدا: . سەیری نموونەکە بکە.
  • ڕاستەوخۆ دوای سەرەوەی ، وەک for ($i = 0; $i < 3; $i++); یان while ($x < 3);. ;ـەکە ناوەڕۆکێکی بەتاڵە. وەشانی for ناوەڕۆکە بەتاڵەکەی 3 جار جێبەجێ دەکات و پاشان بلۆکەکەی خوارەوە تەنها یەک جار؛ وەشانی while هەرگیز کۆتایی نایەت، چونکە هیچ شتێک $x ناگۆڕێت.
  • لەبیرکردنی دوای do...while: } while ($x <= 5); پێویستی پێیەتی.
  • بەکارهێنانی continue لە whileدا پێش گۆڕینی : . سەیری بەشەکە بکە.
  • دانی شتێک بە foreach کە نییە: PHP پیشان دەدات و بازدەدات. سەیری نموونەکە بکە.
  • جووتەگوتەی خوار کە لە کۆپی کراوە: زۆربەی نموونەکانی لە وانەی 3 و تاقیگەی 3دا “ ”یان بەکارهێنابوو، کە دەبێتە هۆی . هەموو کۆدەکانی ئێرە جووتەگوتەی ئاسایی بەکاردەهێنن.
  • : echo "The number is" . $i . "<br>"; دەنووسێت ⁦The number is1⁩، ⁦The number is2⁩ … خاڵەکە ڕێک وەک خۆیان پێکەوە دەلکێنێت و هەرگیز زیاد ناکات. (وێنەی وانەکە پیشان دەدات کە کۆدەکەی چاپی ناکات.) بخە ناو جووتەگوتەکانەوە، "The number is "، یان بنووسە "The number is $i<br>".

Lab exercisesڕاهێنانەکانی تاقیگە

These are the loop tasks of Lab 3 and the course's practice slide, rewritten clearly, plus a few extra exercises. Save them in a folder such as htdocs/loops and try each one before you open the answer.

ئەمانە ئەرکەکانی تاقیگەی 3 و ڕاهێنانی کۆرسەکەن، بە ڕوونی دووبارە نووسراونەتەوە، لەگەڵ چەند ڕاهێنانێکی زیادە. لە وەک htdocs/loops پاشەکەوتیان بکە و پێش کردنەوەی چارەسەر هەر یەکێکیان تاقی بکەرەوە.

ExerciseڕاهێنانLab 3, Example 6: while loopتاقیگەی 3، نموونەی 6: خولی while

Use a while loop to print the numbers 0 to 10, separated by commas. After the loop, print Count: and the final value of the counter on a new line.

بە ژمارەکانی 0 تا ⁦10⁩ بە فاریزە جیاکراوە چاپ بکە. دوای ، لەسەر دێڕێکی نوێ Count: و دوایین چاپ بکە.

Show answerوەڵامەکە پیشان بدە
PHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
    $count = 0;

    while ($count <= 10) {
        echo $count . ", ";
        $count++;
    }

    echo "<br /> Count: {$count}";
?>

The final value is 11: the loop stops when 11 <= 10 is false.

دوایین ⁦11⁩ـە: کاتێک دەوەستێت کە 11 <= 10 هەڵە بێت.

ExerciseڕاهێنانLab 3, Example 9: if statement inside a while loopتاقیگەی 3، نموونەی 9: ڕستەی if لەناو خولی while

Change the previous loop so that it prints the word FIVE instead of the number 5.

پێشوو بگۆڕە تا لە جیاتی ژمارە 5 وشەی FIVE چاپ بکات.

Show answerوەڵامەکە پیشان بدە
PHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
    $count = 0;

    while ($count <= 10) {
        if ($count == 5) {
            echo "FIVE, ";
        } else {
            echo $count . ", ";
        }
        $count++;
    }

    echo "<br /> Count: {$count}";
?>

If you print just "FIVE" (as the lab did), the output shows FIVE6.

ئەگەر تەنها "FIVE" چاپ بکەیت (وەک تاقیگەکە)، FIVE6 پیشان دەدات.

ExerciseڕاهێنانLab 3, Example 10: do...whileتاقیگەی 3، نموونەی ⁦10: do...while⁩

Print The number is: 1 up to The number is: 5, one per line, using a do...while loop.

بە ، لە The number is: 1 تا The number is: 5 چاپ بکە، هەر یەکەیان لەسەر دێڕێک.

Show answerوەڵامەکە پیشان بدە
PHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
    $x = 1;

    do {
        echo "The number is: $x <br>";
        $x++;
    } while ($x <= 5);
?>
ExerciseڕاهێنانLab 3, Example 11: for loopتاقیگەی 3، نموونەی ⁦11⁩: خولی for

Print 0 to 10 using a for loop.

بە 0 تا ⁦10⁩ چاپ بکە.

Show answerوەڵامەکە پیشان بدە
PHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
    for ($count = 0; $count <= 10; $count++) {
        echo $count . ", ";
    }
?>
ExerciseڕاهێنانLab 3, Example 12: foreach loopتاقیگەی 3، نموونەی ⁦12⁩: خولی foreach

Store the colours red, green, blue and yellow in an array and print each one on its own line with foreach.

ڕەنگەکانی red و green و blue و yellow لە هەڵبگرە و بە foreach هەر یەکەیان لەسەر دێڕێکی جیا چاپ بکە.

Show answerوەڵامەکە پیشان بدە
PHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
    $colors = array("red", "green", "blue", "yellow");

    foreach ($colors as $value) {
        echo "$value <br>";
    }
?>
ExerciseڕاهێنانPractice slide: a loop with continue and breakسلایدی ڕاهێنان: خولێک بە continue و break

Write three small loops: (a) print 0 to 9; (b) print 0 to 9 but skip 3 with continue; (c) print 0 to 9 but stop at 3 with break.

سێ بچووک بنووسە: (أ) 0 تا 9 چاپ بکە؛ (ب) 0 تا 9 چاپ بکە بەڵام 3 بە continue بازبدە؛ (ج) 0 تا 9 چاپ بکە بەڵام لە 3 بە break بوەستە.

Hintئاماژە

Use the same for header three times; only the if inside changes.

هەمان سەرەوەی for سێ جار بەکاربهێنە؛ تەنها ifـەکەی ناوەوە دەگۆڕێت.

Show answerوەڵامەکە پیشان بدە
PHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
    // a) print 0 to 9
    for ($i = 0; $i <= 9; $i++) {
        echo $i . " ";
    }
    echo "<br>";

    // b) print 0 to 9, but skip 3 with continue
    for ($i = 0; $i <= 9; $i++) {
        if ($i == 3) {
            continue;
        }
        echo $i . " ";
    }
    echo "<br>";

    // c) print 0 to 9, but stop at 3 with break
    for ($i = 0; $i <= 9; $i++) {
        if ($i == 3) {
            break;
        }
        echo $i . " ";
    }
?>
ExerciseڕاهێنانQuestion: break or continue?پرسیار: break یان continue؟

What is the difference between break and continue?

جیاوازیی نێوان break و continue چییە؟

Show answerوەڵامەکە پیشان بدە

break ends the loop completely; continue skips only the current turn and the loop goes on with the next one.

break بە تەواوی کۆتایی پێدێنێت؛ continue تەنها ئێستا بازدەدات و لەگەڵ داهاتوو بەردەوام دەبێت.

ExerciseڕاهێنانEven and odd numbersژمارە جووت و تاکەکان

Print all even numbers from 0 to 30 with their sum, then all odd numbers with their sum. Use while loops and the % operator.

هەموو ژمارە جووتەکانی 0 تا ⁦30⁩ لەگەڵ کۆکەیان چاپ بکە، پاشان هەموو ژمارە تاکەکان لەگەڵ کۆکەیان. و % بەکاربهێنە.

Show answerوەڵامەکە پیشان بدە

See the worked example. The sums are 240 (even) and 225 (odd).

سەیری نموونە چارەسەرکراوەکە بکە. کۆکەکان ⁦240⁩ (جووت) و ⁦225⁩ (تاک)ن.

ExerciseڕاهێنانExtra: multiplication tableزیادە: خشتەی لێکدان

Print the multiplication table of 7, from 7 x 1 = 7 to 7 x 10 = 70, one line each.

خشتەی لێکدانی 7 چاپ بکە، لە 7 x 1 = 7 تا 7 x 10 = 70، هەر یەکەیان لەسەر دێڕێک.

Show answerوەڵامەکە پیشان بدە
PHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
    $n = 7;

    for ($i = 1; $i <= 10; $i++) {
        echo "$n x $i = " . ($n * $i) . "<br>";
    }
?>
ExerciseڕاهێنانExtra: sum of 1 to 100زیادە: کۆی 1 تا ⁦100⁩

Add up all whole numbers from 1 to 100 with a while loop and print the result.

بە هەموو 1 تا ⁦100⁩ کۆ بکەرەوە و ئەنجامەکە چاپ بکە.

Hintئاماژە

Keep a $sum variable that starts at 0, and add $i to it in every turn.

$sum دابنێ کە لە 0ـەوە دەست پێدەکات و لە هەموو $iی بخەرە سەر.

Show answerوەڵامەکە پیشان بدە
PHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
    $i = 1;
    $sum = 0;

    while ($i <= 100) {
        $sum += $i;
        $i++;
    }

    echo "1 + 2 + ... + 100 = $sum";
?>
ExerciseڕاهێنانExtra: a list of namesزیادە: لیستێکی ناو

Store five names in an array and print them one under the other with foreach.

پێنج ناو لە هەڵبگرە و بە foreach یەک لە ژێر یەکتری چاپیان بکە.

Show answerوەڵامەکە پیشان بدە
PHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
    $names = array("Aram", "Shilan", "Karwan", "Dilan", "Rezan");

    foreach ($names as $name) {
        echo $name . "<br>";
    }
?>

Summary and reviewپوختە و پێداچوونەوە

  • A loop runs a block of code again and again, based on a condition.
  • while checks the condition before each turn; the body may run 0 times. Something in the body must change the condition.
  • do...while runs the body first and checks afterwards, so it always runs at least once. It ends with while (...);.
  • for (init; test; increment) puts the three counter jobs in one line, separated by semicolons.
  • foreach ($array as $value) and foreach ($array as $key => $value) go through every element of an array (or object).
  • break leaves the loop; continue skips to the next turn.
  • بەپێی چەند جار دووبارە جێبەجێ دەکات.
  • while پێش هەر دەپشکنێت؛ لەوانەیە ناوەڕۆکەکە 0 جار جێبەجێ بێت. شتێک لە ناوەڕۆکەکەدا دەبێت بگۆڕێت.
  • do...while سەرەتا ناوەڕۆکەکە جێبەجێ دەکات و پاشان دەپشکنێت، بۆیە هەمیشە لانیکەم یەک جار جێبەجێ دەبێت. بە while (...); کۆتایی دێت.
  • for (init; test; increment) سێ کاری لە یەک دێڕدا کۆ دەکاتەوە، بە جیاکراوە.
  • foreach ($array as $value) و foreach ($array as $key => $value) بە ناو هەموو (یان ) دەڕۆن.
  • break لە دەردەچێت؛ continue بازدەدات بۆ داهاتوو.
Review questionsپرسیارەکانی پێداچوونەوە
What are the four loops in PHP?چوار خولەکەی PHP کامانەن؟

while, do...while, for and foreach.

while، do...while، for و foreach.

What is the value of $count after $count = 0; while ($count <= 10) { $count++; }?دوای $count = 0; while ($count <= 10) { $count++; } نرخی $count چەندە؟

11. The loop stops only when the test 11 <= 10 is false.

⁦11⁩. تەنها کاتێک دەوەستێت کە پشکنینی 11 <= 10 هەڵە بێت.

What is the main difference between while and do...while?جیاوازیی سەرەکیی نێوان while و do...while چییە؟

while checks before the body (0 or more turns); do...while checks after the body (1 or more turns).

while پێش ناوەڕۆکەکە دەپشکنێت (⁦0⁩ یان زیاتر )؛ do...while دوای ناوەڕۆکەکە دەپشکنێت (⁦1⁩ یان زیاتر ).

Name and explain the three parts of a for loop header.سێ بەشەکەی سەرەوەی خولی for ناو ببە و ڕوونیان بکەرەوە.

init counter (runs once, first), test counter (checked before every turn; false ends the loop), increment counter (runs after every turn). They are separated by semicolons.

⁦init counter⁩ (یەک جار، یەکەم شت)، ⁦test counter⁩ (پێش هەموو دەپشکنرێت؛ ئەگەر false بێت کۆتایی دێت)، ⁦increment counter⁩ (دوای هەموو ). بە لە یەک جیا دەکرێنەوە.

Does foreach work only on arrays?ئایا foreach تەنها لەسەر ڕیزەک کار دەکات؟

No. It works on arrays and objects. For any other value PHP shows a warning.

نەخێر. لەسەر و کار دەکات. بۆ هەر تر PHP پیشان دەدات.

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

break ends the loop completely; continue skips only the current turn.

break بە تەواوی کۆتایی پێدێنێت؛ continue تەنها ئێستا بازدەدات.

What is an infinite loop, and how do you avoid it?خولی بێکۆتا چییە و چۆن خۆتی لێ دەپارێزیت؟

A loop whose condition never becomes false. Make sure the body changes the condition (for example $i++), also when continue is used.

کە هەرگیز ناگاتە false. دڵنیابە کە ناوەڕۆکەکە دەگۆڕێت (بۆ نموونە $i++)، تەنانەت کاتێک continue بەکاردێت.

Videoڤیدیۆ

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

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

PHP Loops Tutorial - Break & Continue Statements - Full PHP 8 TutorialProgram With Gio · Englishئینگلیزی · 60K viewsبینین · 2.1K likesلایک

One video for the whole lesson: while, do-while, for and foreach loops, plus break, continue, and how to avoid infinite loops.

یەک ڤیدیۆ بۆ هەموو وانەکە: while، do-while، for و foreach، لەگەڵ break و continue و چۆن خۆت لە بپارێزیت.

Overview of all four loops plus break and continue.

تێڕوانینێکی گشتی بۆ هەر چوار ، لەگەڵ break و continue.

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

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

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

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

Functionsکردارەکان

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

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