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

break and continueڕستەی break و ڕستەی continue

Leaving a loop early with break and skipping one round with continue, in for, while and do-while: where control jumps, the useless i++ before break, while (true) with break, a search that stops at the first match, why while loops must update before continue (and the endless loop when they do not), break in nested loops with a labelled break as an extra, and break in a switch inside a loop.

دەرچوونی زوو لە بە break و جێهێشتنی یەک بە continue، لە for، while و ⁦do-while⁩ـدا: کۆنترۆڵ بۆ کوێ باز دەدات، ⁦i++⁩ـی بێسوود پێش break، ⁦while (true)⁩ لەگەڵ break، گەڕانێک کە لە یەکەم دۆزینەوەدا دەوەستێت، بۆچی دەبێت پێش continue نوێ بکەنەوە (و ئەو ئەگەر نەیکەن)، break لە لەگەڵ breakـی ناونراو وەک زیادە و break لە switchـێکی ناو .

11 min read10 خولەک خوێندنەوە · 13 stepsهەنگاو

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

  • Use break to leave a loop and continue to skip one round, in for, while and do-while.
  • Predict the output of loops with break and continue, and say where control jumps.
  • Update the counter before continue in while loops, and stop a search at the first match.
  • Explain why break leaves only the innermost loop or switch.
  • break بۆ دەرچوون لە و continue بۆ جێهێشتنی یەک بەکار بهێنیت، لە for، while و ⁦do-while⁩ـدا.
  • ئەو پێشبینی بکەیت کە break و continueـیان تێدایە و بڵێیت کۆنترۆڵ بۆ کوێ باز دەدات.
  • لە پێش continue نوێ بکەیتەوە و گەڕانێک لە یەکەم دۆزینەوەدا ڕابگریت.
  • ڕوونی بکەیتەوە بۆچی break تەنیا لە ناوەوەترین یان switch دەردەچێت.

Leaving early or skipping a roundزوو دەرچوون یان جێهێشتنی خولانەوەیەک

Normally a loop stops only when its condition becomes false. Sometimes you want to leave it at once, or to skip the rest of one round without testing anything. Java has two statements for this, and both almost always stand inside an if: break leaves the loop; continue jumps to the next round. Python and MATLAB have the same pair (Python, MATLAB).

بە ئاسایی تەنیا کاتێک دەوەستێت کە هەڵە بێت. هەندێک جار دەتەوێت یەکسەر لێی دەربچیت، یان باقی جێبهێڵیت بێ ئەوەی هیچ بپشکنیت. Java دوو بۆ ئەمە هەیە و هەردووکیان نزیکەی هەمیشە لە ناو ifـێکدان: break لە دەردەچێت؛ continue باز دەدات بۆ دواتر. Python و MATLAB هەمان جووتیان هەیە (Python، MATLAB).

Two runners on two tracks with the marks 0 to 6. In the top lane, with if (i == 3) break;, the runner prints 0, 1 and 2 and at 3 runs off the track, out of the loop: the output stays 0 1 2. In the bottom lane, with if (i == 3) continue;, the runner prints 0, 1 and 2, jumps over 3 and goes on: 4, 5, 6. Final outputs: 0 1 2 and 0 1 2 4 5 6.
Two runners on two tracks with the marks 0 to 6. In the top lane, with if (i == 3) break;, the runner prints 0, 1 and 2 and at 3 runs off the track, out of the loop: the output stays 0 1 2. In the bottom lane, with if (i == 3) continue;, the runner prints 0, 1 and 2, jumps over 3 and goes on: 4, 5, 6. Final outputs: 0 1 2 and 0 1 2 4 5 6.
break leaves the track; continue jumps over one markbreak ڕێڕەوەکە جێدەهێڵێت؛ continue بە سەر یەک نیشانەدا باز دەداتCêIT

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 دەتوانێت دوو خولی ناوەکی جێبهێڵێت.

break ends the loop at once, without testing the condition again, and the program goes on with the first statement after the loop. It works in all three loops: for, while and do … while.

break یەکسەر کۆتایی پێ دەهێنێت (بێ ئەوەی دیسان بپشکنێت) و لەگەڵ یەکەم لە دوای بەردەوام دەبێت. لە هەر سێ کار دەکات: for، while و do … while.

BreakAfter.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
public class BreakAfter {
    public static void main(String[] args) {
        for (int i = 0; i <= 6; i++) {
            if (i == 3) {
                break;
            }
            System.out.println(i);
        }
        System.out.println("The first statement after the loop");
    }
}
A flowchart of for (int i = 0; i <= 6; i++) with the test i <= 6, then a second test i == 3. False leads to println(i) and i++, back to the test. True leads straight out along a thick arrow labelled break to after the loop. The terminal shows 0, 1, 2. At i == 3 the loop ends at once: no i++, no new test, no 3 4 5 6.هێڵکاریی ڕەوت بۆ for (int i = 0; i <= 6; i++) بە پشکنینی i <= 6، پاشان پشکنینی دووەمی i == 3. false دەچێت بۆ println(i) و i++ و دەگەڕێتەوە بۆ پشکنین. true ڕاستەوخۆ بە تیرێکی ئەستووری break دەچێتە دوای خولەکە. تێرمیناڵەکە 0، 1، 2 پیشان دەدات. لە i == 3ـدا خولەکە یەکسەر کۆتایی دێت: نە i++، نە پشکنینی نوێ، نە 3 4 5 6.
break jumps to the first statement after the loopbreak باز دەدات بۆ یەکەم دوای CêIT

break in while and do-whilebreak لە خولی while و خولی ⁦do...while⁩ـدا

The same stop works in the other two loops. These versions increase i themselves, at the end of the body:

هەمان وەستان لە دوو تریشدا کار دەکات. ئەم وەشانانە خۆیان i زیاد دەکەن، لە کۆتایی :

BreakWhile.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
public class BreakWhile {
    public static void main(String[] args) {
        int i = 0;
        while (i <= 6) {
            if (i == 3) {
                i++;
                break;
            }
            System.out.println(i);
            i++;
        }
        System.out.println("After the loop, i = " + i);
    }
}
BreakDoWhile.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
public class BreakDoWhile {
    public static void main(String[] args) {
        int i = 0;
        do {
            if (i == 3) {
                i++;
                break;
            }
            System.out.println(i);
            i++;
        } while (i <= 6);
    }
}

The i++ just before break does not change the output: the loop ends anyway. It changes only i, which is 4 after the loop instead of 3; that matters only if you use i later.

i++ـەکەی ڕاستەوخۆ پێش break ناگۆڕێت: هەر کۆتایی دێت. تەنیا i دەگۆڕێت، کە دوای دەبێتە 4 لە جیاتی 3؛ ئەمە تەنیا گرنگە ئەگەر دواتر i بەکار بهێنیت.

Two versions of the while loop side by side, one with i++ before break and one without it. Both terminals show 0, 1, 2. Under them: After the loop, i = 4 and After the loop, i = 3. The output is the same; the extra i++ only changes i.دوو وەشانی خولی while لە تەنیشت یەک، یەکێکیان بە i++ پێش break و ئەوی تر بەبێ ئەو. هەردوو تێرمیناڵ 0، 1، 2 پیشان دەدەن. لە خوارەوەیان: After the loop, i = 4 و After the loop, i = 3. دەرخستەکە وەک یەکە؛ i++ـی زیادە تەنیا i دەگۆڕێت.
Same output; only the final i differsهەمان ؛ تەنیا iـی کۆتایی جیاوازەCêIT

break in while and do-whilebreak لە خولی while و خولی ⁦do...while⁩ـدا

while (true): a loop that only break can end⁦while (true)⁩: خولێک کە تەنیا break کۆتایی پێ دەهێنێت

while (true) never ends by itself; a break inside decides when to leave. It fits a check that must come in the middle of a round, such as asking until the answer is valid (&&: Combining conditions):

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

AskAge.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
import java.util.Scanner;

public class AskAge {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int age;
        while (true) {
            System.out.print("Age (1-120): ");
            age = input.nextInt();
            if (age >= 1 && age <= 120) {
                break;
            }
            System.out.println("Please type an age from 1 to 120.");
        }
        System.out.println("Thank you. Age = " + age);
    }
}
A flowchart: while (true) at the top, then age = input.nextInt();, then the test age >= 1 && age <= 120. True leads along a thick arrow labelled break to after the loop. False leads to print a message and back to the top. The test at the top is always true, so the exit is the break in the middle; for (;;) is the same loop.هێڵکاریی ڕەوت: while (true) لە سەرەوە، پاشان age = input.nextInt();، پاشان پشکنینی age >= 1 && age <= 120. true بە تیرێکی ئەستووری break دەچێت بۆ دوای خولەکە. false دەچێت بۆ چاپکردنی پەیامێک و دەگەڕێتەوە سەرەوە. پشکنینی سەرەوە هەمیشە ڕاستە، بۆیە دەرچوون breakـەکەی ناوەڕاستە؛ for (;;) هەمان خولە.
The exit is in the middleدەرچوون لە ناوەڕاستدایەCêIT

for (;;) is the same endless loop. The do-while way of asking again is in Practice.

for (;;) هەمان . ڕێگای ⁦do-while⁩ بۆ دیسان پرسیارکردن لە ڕاهێنانـدایە.

Search and stop at the first matchگەڕان و وەستان لە یەکەم دۆزینەوەدا

break is perfect for a search: check the items one by one and stop at the first one you want. This is a . The program reads up to seven night temperatures and stops at the first frost:

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

FirstFrost.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
import java.util.Scanner;

public class FirstFrost {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        boolean found = false;
        for (int night = 1; night <= 7; night++) {
            System.out.print("Night " + night + " temperature: ");
            int t = input.nextInt();
            if (t < 0) {
                System.out.println("First frost on night " + night + ": " + t + " degrees");
                found = true;
                break;
            }
        }
        if (!found) {
            System.out.println("No frost this week.");
        }
    }
}
Seven cards for nights 1 to 7: 6°, 3°, -2°, 4°, -5°, 1°, 2°. The first two are checked and crossed, the third, -2°, is ticked as the first match, and the last four are greyed out: not read. An arrow shows the cards checked one by one; t < 0 -> break. After the first match there is no reason to look at the rest.حەوت کارت بۆ شەوەکانی 1 تا 7: 6°، 3°، -2°، 4°، -5°، 1°، 2°. دوو کارتی یەکەم پشکنراون و نیشانەی هەڵەیان لێدراوە، سێیەم، -2°، وەک یەکەم دۆزینەوە نیشانەی ڕاستی لێدراوە و چوار کارتی کۆتایی کاڵن: نەخوێندراونەتەوە. تیرێک نیشانی دەدات کە کارتەکان یەک بە یەک پشکنراون؛ t < 0 -> break. دوای یەکەم دۆزینەوە هیچ هۆیەک نییە بۆ سەیرکردنی ئەوانی تر.
Stop at the first match; the rest is never readلە یەکەم دۆزینەوەدا بوەستە؛ ئەوانی تر هەرگیز ناخوێندرێنەوەCêIT

The variable found remembers whether the loop ended with break or ran to the end; only then can the program say No frost this week.

found لە بیری دەمێنێت کە ئایا بە break کۆتایی هات یان تا کۆتایی ڕۆیشت؛ تەنیا ئەوکات دەتوانێت بڵێت No frost this week.

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، بەردەوام دەبێت.

continue ends only the current round: the rest of the body is skipped and the loop goes on with its next round. It works in all three loops and almost always sits inside an if.

continue تەنیا ئێستا تەواو دەکات: باقی تێدەپەڕێنرێت و لەگەڵ دواتر بەردەوام دەبێت. لە هەر سێ کار دەکات و نزیکەی هەمیشە لە ناو ifـێکدایە.

ContinueDemo.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
public class ContinueDemo {
    public static void main(String[] args) {
        for (int i = 0; i <= 6; i++) {
            if (i == 3) {
                continue;
            }
            System.out.println(i);
        }
    }
}
The same flowchart as for break, but the true branch of i == 3 is a thick arrow labelled continue that goes to i++ and back to the test. The terminal shows 0, 1, 2, 4, 5, 6. At i == 3 println is skipped, but in a for loop i++ still runs.هەمان هێڵکاریی ڕەوتی break، بەڵام لقی trueـی i == 3 تیرێکی ئەستووری continueـە کە دەچێت بۆ i++ و دەگەڕێتەوە بۆ پشکنین. تێرمیناڵەکە 0، 1، 2، 4، 5، 6 پیشان دەدات. لە i == 3ـدا println تێدەپەڕێنرێت، بەڵام لە خولی forـدا i++ هەر جێبەجێ دەبێت.
continue skips the rest of the body; for still runs the updatecontinue باقی تێدەپەڕێنێت؛ for هەر نوێکردنەوەکە جێبەجێ دەکاتCêIT

In a for loop, continue jumps to the update i++ and then to the test, so the loop moves on normally.

لە continue باز دەدات بۆ نوێکردنەوەی i++ و پاشان بۆ پشکنین، بۆیە بە ئاسایی بەردەوام دەبێت.

continue in while and do-while: update firstcontinue لە while و ⁦do-while⁩ـدا: سەرەتا نوێکردنەوە

In a while or do … while loop, continue jumps straight to the condition test, so an update at the bottom of the body is skipped. That is why these versions increase i before continue:

لە یان continue ڕاستەوخۆ باز دەدات بۆ پشکنینی ، بۆیە نوێکردنەوەیەک لە خوارەوەی تێدەپەڕێنرێت. لەبەر ئەمە ئەم وەشانانە i پێش continue زیاد دەکەن:

ContinueWhile.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
public class ContinueWhile {
    public static void main(String[] args) {
        int i = 0;
        while (i <= 6) {
            if (i == 3) {
                i++;
                continue;
            }
            System.out.println(i);
            i++;
        }
    }
}
ContinueDoWhile.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
public class ContinueDoWhile {
    public static void main(String[] args) {
        int i = 0;
        do {
            if (i == 3) {
                i++;
                continue;
            }
            System.out.println(i);
            i++;
        } while (i <= 6);
    }
}
Two while loops. Left, wrong: i++ only at the bottom; at i == 3 continue jumps back to the test, i++ is skipped and i stays 3 for ever; output 0 1 2, then nothing, and it never ends. Right: i++ before continue, so i becomes 4 before the jump; output 0 1 2 4 5 6.دوو خولی while. چەپ، هەڵە: i++ تەنیا لە خوارەوە؛ لە i == 3ـدا continue دەگەڕێتەوە بۆ پشکنین، i++ تێدەپەڕێنرێت و i بۆ هەمیشە 3 دەمێنێتەوە؛ دەرخستە 0 1 2، پاشان هیچ و هەرگیز کۆتایی نایەت. ڕاست: i++ پێش continue، بۆیە i پێش بازدانەکە دەبێتە 4؛ دەرخستە 0 1 2 4 5 6.
Update before continue, or the loop never endsپێش continue نوێ بکەرەوە، ئەگینا هەرگیز کۆتایی نایەتCêIT

continue in while and do-while: update firstcontinue لە while و ⁦do-while⁩ـدا: سەرەتا نوێکردنەوە

The trap, liveتەڵەکە، بە زیندوویی

Move the i++ after the if and run it. It prints 0, 1 and 2, then nothing more, and never ends: at i == 3, continue skips the only i++, so the loop tests 3 <= 6 again and again:

i++ بگوازەرەوە بۆ دوای if و جێبەجێی بکە. 0، 1 و 2 چاپ دەکات، پاشان هیچی تر و هەرگیز کۆتایی نایەت: لە i == 3ـدا continue تاکە i++ تێدەپەڕێنێت، بۆیە دووپاتە و دووپاتە 3 <= 6 دەپشکنێت:

ContinueTrap.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
public class ContinueTrap {
    public static void main(String[] args) {
        int i = 0;
        while (i <= 6) {
            if (i == 3) {
                continue;
            }
            System.out.println(i);
            i++;
        }
    }
}

A print at the top of the body shows what happens inside: the same line for ever.

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

ContinueTrapWatch.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
public class ContinueTrapWatch {
    public static void main(String[] args) {
        int i = 0;
        while (i <= 6) {
            System.out.println("checking i = " + i);
            if (i == 3) {
                continue;
            }
            i++;
        }
    }
}

break in nested loopsbreak لە خولە ناوەکییەکاندا

break leaves only the loop it is in. Inside a nested loop it ends the inner loop, and the outer loop goes on with its next round:

break تەنیا لەو دەردەچێت کە تێیدایە. لە ناو ناوەوە کۆتایی پێ دەهێنێت و دەرەوە لەگەڵ دواتری بەردەوام دەبێت:

NestedBreak.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
public class NestedBreak {
    public static void main(String[] args) {
        for (int i = 1; i <= 3; i++) {
            for (int j = 1; j <= 3; j++) {
                if (j == 2) {
                    break;
                }
                System.out.println("i = " + i + ", j = " + j);
            }
            System.out.println("end of outer round " + i);
        }
    }
}
An outer for loop box with an inner for loop box inside it. In the inner box, if (j == 2) { break; }. A thick arrow from break leaves the inner box and lands on the line System.out.println("end of outer round " + i), still inside the outer loop. The terminal shows i = 1, j = 1, end of outer round 1, and the same for 2 and 3: the outer loop still runs 3 times.سندووقی خولی forـی دەرەوە و سندووقی خولی forـی ناوەوە لە ناویدا. لە سندووقی ناوەوەدا if (j == 2) { break; }. تیرێکی ئەستوور لە breakـەوە لە سندووقی ناوەوە دەردەچێت و دەگاتە دێڕی System.out.println("end of outer round " + i)، هێشتا لە ناو خولی دەرەوەدا. تێرمیناڵەکە i = 1, j = 1، end of outer round 1 و هەمان شت بۆ 2 و 3 پیشان دەدات: خولی دەرەوە هەر 3 جار جێبەجێ دەبێت.
break ends the inner loop onlybreak تەنیا ناوەوە کۆتایی پێ دەهێنێتCêIT

break in a switch and in a loopbreak لە switch و لە خولدا

You met break in The switch statement, where it ends a case so that the next case does not run as well (fall-through). The rule is the same everywhere: break leaves the innermost switch or loop around it. So a break in a switch inside a loop ends the switch, not the loop. This menu does not stop at 0:

breakـت لە ڕستەی switch بینی، کە بەشێک تەواو دەکات بۆ ئەوەی بەشی دواتریش جێبەجێ نەبێت (تێپەڕین). یاساکە لە هەموو شوێنێک وەک یەکە: break لە نزیکترین switch یان دەردەچێت کە دەوری داوە. بۆیە breakـێک لە ناو switchـێکی ناو switchـەکە کۆتایی پێ دەهێنێت، نەک . ئەم لە 0ـدا ناوەستێت:

MenuTrap.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
import java.util.Scanner;

public class MenuTrap {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        while (true) {
            System.out.print("1 = tea, 2 = coffee, 0 = exit: ");
            int choice = input.nextInt();
            switch (choice) {
                case 1:
                    System.out.println("Tea");
                    break;
                case 2:
                    System.out.println("Coffee");
                    break;
                case 0:
                    System.out.println("Bye");
                    break;
                default:
                    System.out.println("No such item");
            }
        }
    }
}
A while (true) box with a switch box inside it. In case 0 the program prints Bye and runs break; a thick arrow from break leaves only the switch box and lands at the end of the loop, and a dashed arrow goes back to while (true). The terminal: 1 then Tea, 0 then Bye, 2 then Coffee: after Bye the loop asks again, because break left only the switch.سندووقی while (true) و سندووقی switch لە ناویدا. لە case 0ـدا بەرنامەکە Bye چاپ دەکات و break جێبەجێ دەکات؛ تیرێکی ئەستوور لە breakـەوە تەنیا لە سندووقی switch دەردەچێت و دەگاتە کۆتایی خولەکە و تیرێکی پچڕپچڕ دەگەڕێتەوە بۆ while (true). تێرمیناڵەکە: 1 پاشان Tea، 0 پاشان Bye، 2 پاشان Coffee: دوای Bye خولەکە دیسان دەپرسێت، چونکە break تەنیا لە switch دەرچوو.
Inside a switch, break ends the switch onlyلە ناو switchـدا break تەنیا switch کۆتایی پێ دەهێنێتCêIT

After Bye the loop asks again; when the input runs out, nextInt() throws a NoSuchElementException (Bad input). Let the loop condition end the menu instead:

دوای Bye دیسان دەپرسێت؛ کاتێک تەواو دەبێت، nextInt() NoSuchElementException فڕێ دەدات (نێوخستەی خراپ). لە جیاتی ئەوە با کۆتایی پێ بهێنێت:

MenuFixed.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
import java.util.Scanner;

public class MenuFixed {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int choice;
        do {
            System.out.print("1 = tea, 2 = coffee, 0 = exit: ");
            choice = input.nextInt();
            switch (choice) {
                case 1:
                    System.out.println("Tea");
                    break;
                case 2:
                    System.out.println("Coffee");
                    break;
                case 0:
                    System.out.println("Bye");
                    break;
                default:
                    System.out.println("No such item");
            }
        } while (choice != 0);
    }
}

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

You wroteنووسیتjavac or the run saysjavac یان جێبەجێکردن دەڵێتFixچارەسەر
break; with no loop or switch around itbreak; بێ یان switch لە دەوریداbreak outside switch or loopbreak only inside a loop or a switch.break تەنیا لە ناو یان switchـدا.
continue; with no loop around itcontinue; بێ لە دەوریداcontinue outside of loopcontinue only inside a loop.continue تەنیا لە ناو .
a statement right after break; ڕاستەوخۆ لە دوای break;unreachable statementPut it before break, or delete it.بیخەرە پێش break، یان بیسڕەوە.
continue before the update in a while loopcontinue پێش نوێکردنەوە لە never ends, no messageهەرگیز کۆتایی نایەت، بێ پەیامUpdate first, or use for.سەرەتا نوێ بکەرەوە، یان for بەکار بهێنە.
break expected to leave two nested loopsچاوەڕوانی ئەوە کە break لە دوو دەربچێتthe outer loop goes on دەرەوە بەردەوام دەبێتA labelled break, or a boolean flag.breakـی ناونراو، یان وەک نیشانە.
break in a switch inside a loopbreak لە switchـێکی ناو the loop goes on بەردەوام دەبێتEnd the loop with its condition.با کۆتایی پێ بهێنێت.

The real javac messages of the first three rows:

پەیامە ڕاستەقینەکانی javac بۆ سێ ڕیزی یەکەم:

Outputدەرئەنجام
$ javac BreakOutside.java
BreakOutside.java:5: error: break outside switch or loop
            break;
            ^
1 error

$ javac ContinueOutside.java
ContinueOutside.java:5: error: continue outside of loop
            continue;
            ^
1 error

$ javac Unreachable.java
Unreachable.java:5: error: unreachable statement
            System.out.println(i);
            ^
1 error

Practiceڕاهێنان

ExerciseڕاهێنانSkip the threesسێیەکان جێبهێڵە

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

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

Hintئاماژە

i % 3 == 0 tells you a multiple of 3.

i % 3 == 0 چەندجارەکانی 3 دیاری دەکات.

Show answerوەڵامەکە پیشان بدە
SkipThrees.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
public class SkipThrees {
    public static void main(String[] args) {
        for (int i = 1; i <= 20; i++) {
            if (i % 3 == 0) {
                continue;
            }
            System.out.print(i + " ");
        }
        System.out.println();
    }
}
ExerciseڕاهێنانPositive totalکۆی ژمارە ئەرێنییەکان

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

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

Show answerوەڵامەکە پیشان بدە
SumPositive.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
import java.util.Scanner;

public class SumPositive {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int total = 0;
        while (true) {
            System.out.print("Number (0 to stop): ");
            int n = input.nextInt();
            if (n == 0) {
                break;
            }
            if (n < 0) {
                System.out.println("Skipped " + n);
                continue;
            }
            total += n;
        }
        System.out.println("Total of the positive numbers: " + total);
    }
}
ExerciseڕاهێنانThe smallest divisorبچووکترین دابەشکەر

Read a whole number above 1 and print its smallest divisor above 1, or say that it is prime. Stop at the first divisor.

لە 1 گەورەتر بخوێنەوە و بچووکترین دابەشکەری لە 1 گەورەتری چاپ بکە، یان بڵێ کە ژمارەیەکی سەرەتاییە. لە یەکەم دابەشکەردا بوەستە.

Show answerوەڵامەکە پیشان بدە
SmallestDivisor.javaJAVAType in a terminalلە تێرمیناڵدا بینووسە
import java.util.Scanner;

public class SmallestDivisor {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Whole number above 1: ");
        int n = input.nextInt();
        int d = 2;
        while (d < n) {
            if (n % d == 0) {
                break;
            }
            d++;
        }
        if (d == n) {
            System.out.println(n + " is prime");
        } else {
            System.out.println("Smallest divisor of " + n + ": " + d);
        }
    }
}

If no divisor is found, the loop ends with d == n: the number is prime.

ئەگەر هیچ دابەشکەرێک نەدۆزرایەوە، بە d == n کۆتایی دێت: ژمارەکە سەرەتاییە.

ExerciseڕاهێنانPredictپێشبینی بکە

What does this loop print? Explain each continue and the break.

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

JAVAType in a terminalلە تێرمیناڵدا بینووسە
for (int i = 1; i <= 10; i++) {
    if (i % 4 == 0) {
        continue;
    }
    if (i == 7) {
        break;
    }
    System.out.print(i + " ");
}
System.out.println();
Show answerوەڵامەکە پیشان بدە
Outputدەرئەنجام
$ java BreakContinueMix
1 2 3 5 6 

4 is skipped by continue; at 7 break ends the loop before the print, so 8 is never even tested.

4 بە continue تێدەپەڕێنرێت؛ لە 7ـدا break پێش چاپکردن کۆتایی پێ دەهێنێت، بۆیە 8 هەرگیز ناپشکنرێت.

ExerciseڕاهێنانBreak in the inner loopbreak لە خولی ناوەوەدا

Predict the output. Does break stop both loops?

پێشبینی بکە. ئایا break هەردوو دەوەستێنێت؟

JAVAType in a terminalلە تێرمیناڵدا بینووسە
for (int i = 1; i <= 3; i++) {
    for (int j = 1; j <= 3; j++) {
        if (j > i) {
            break;
        }
        System.out.print(i + "" + j + " ");
    }
    System.out.println();
}
Show answerوەڵامەکە پیشان بدە
Outputدەرئەنجام
$ java NestedPredict
11 
21 22 
31 32 33 

No: in outer round i the inner loop breaks when j > i, so row i has i pairs, and every outer round still runs.

نەخێر: لە دەرەوەی iـدا ناوەوە کاتێک j > i دەبێت دەوەستێت، بۆیە ڕیزی i ژمارەی i جووتی تێدایە و هەموو دەرەوە هەر جێبەجێ دەبن.

Summaryکورتە

  • break ends the loop at once; the program goes on after the loop. It works in for, while and do-while.
  • continue skips the rest of one round. In for, the update still runs; in while and do-while it is skipped, so update before continue.
  • Use break to stop a search at the first match, and while (true) + break to ask until the input is valid.
  • break leaves only the innermost loop or switch; a labelled break leaves an outer loop too.
  • break یەکسەر کۆتایی پێ دەهێنێت؛ لە دوای بەردەوام دەبێت. لە for، while و ⁦do-while⁩ـدا کار دەکات.
  • continue باقی یەک تێدەپەڕێنێت. لە forـدا نوێکردنەوەکە هەر جێبەجێ دەبێت؛ لە while و ⁦do-while⁩ـدا تێدەپەڕێنرێت، بۆیە پێش continue نوێ بکەرەوە.
  • break بۆ وەستاندنی گەڕان لە یەکەم دۆزینەوەدا و while (true) + break بۆ پرسیارکردن تا دروست دەبێت بەکار بهێنە.
  • break تەنیا لە ناوەوەترین یان switch دەردەچێت؛ breakـی ناونراو لە دەرەوەش دەردەچێت.
Review questionsپرسیارەکانی پێداچوونەوە
What is the difference between break and continue?جیاوازیی نێوان break و continue چییە؟

break ends the whole loop; continue ends only the current round.

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

Why can continue make a while loop endless?بۆچی continue دەتوانێت خولێکی while بێکۆتا بکات؟

It jumps to the test and skips an update written after it.

باز دەدات بۆ پشکنین و نوێکردنەوەیەک کە لە دوای ئەو نووسراوە تێدەپەڕێنێت.

A menu loop contains a switch. Why does break in case 0 not end the menu?خولێکی مێنیو switchـێکی تێدایە. بۆچی break لە case 0ـدا مێنیوەکە کۆتایی پێ ناهێنێت؟

It leaves the switch, the innermost statement around it, not the loop.

لە switchـەکە دەردەچێت، نزیکترین لە دەوریدا، نەک لە .

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

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

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

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

Methods, Parameters and Return Valuesمیتۆدەکان، پارامیتەرەکان و نرخە گەڕاوەکان

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

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

Appearanceڕووکار

Modeدۆخ
Styleشێواز
Coloursڕەنگەکان

Saved on this device only.تەنیا لەسەر ئەم ئامێرە پاشەکەوت دەکرێت.