JavaJava · Chapterبەش 7 · Lectureوانە 7.1
The while and do-while Loopsخولی while و خولی do...while
Why programs repeat work and Java's three loops; the while loop with its condition, body and update; a counter-controlled loop, counting down, an accumulator that adds 1 to 20 (210, checked with ) and even numbers two ways; infinite loops, Ctrl+C and a semicolon after while; the do-while loop that runs at least once, a sentinel loop that reads until 0, the largest-number exercise, and pretest against posttest loops.
بۆچی کار دووپاتە دەکەنەوە و سێ Java؛ لەگەڵ ، و نوێکردنەوە؛ ، ژماردنی پێچەوانە، کە 1 تا 20 کۆ دەکاتەوە (210، بە پشکنراوە) و ژمارە جووتەکان بە دوو ڕێگا؛ ، Ctrl+C و لە دوای while؛ کە لانیکەم یەک جار جێبەجێ دەبێت، پاسەوان کە تا 0 دەخوێنێتەوە، ڕاهێنانی گەورەترین ژمارە و بەرامبەر .
What you will learnچی فێر دەبیت
- Write a while loop with a start value, a condition and an update, and say how many times its body runs.
- Use counters, accumulators and sentinel values, and find and stop infinite loops.
- Write a do-while loop, explain why it runs at least once, and end it with
;. - Tell a pretest loop from a posttest loop and choose between while and do-while.
- بە سەرەتا، و نوێکردنەوە بنووسیت و بڵێیت جەستەکەی چەند جار جێبەجێ دەبێت.
- ، و بەکار بهێنیت و بدۆزیتەوە و ڕایانبگریت.
- بنووسیت، ڕوونی بکەیتەوە بۆچی لانیکەم یەک جار جێبەجێ دەبێت و بە
;کۆتایی پێ بهێنیت. - لە جیا بکەیتەوە و لە نێوان while و do-whileـدا هەڵبژێریت.
Why loops?بۆچی خول؟
A tells a program to run the same statements again and again. To print Welcome to Java! ten times you do not copy the line ten times: you write it once inside a loop, and for a thousand times you change only one number. One pass through the loop is one , and the loop goes on as long as a condition stays true.
بە دەڵێت هەمان دووپاتە و دووپاتە جێبەجێ بکات. بۆ ئەوەی Welcome to Java! دە جار چاپ بکەیت، دێڕەکە دە جار کۆپی ناکەیت: یەک جار لە ناو دەینووسیت و بۆ هەزار جاریش تەنیا ژمارەیەک دەگۆڕیت. هەر جارێک تێپەڕبوون بە ناو و بەردەوام دەبێت تا ڕاست بمێنێتەوە.
Java has three loops. They do the same basic job but are written differently and test their condition at different moments: while and do … while in this lesson, for in The for loop. A fourth form, the , walks through arrays and lists (a first look). Repetition is one of the three building blocks of every algorithm (Three building blocks).
Java سێ هەیە. هەمان کاری بنەڕەتی دەکەن، بەڵام جیاواز دەنووسرێن و لە کاتی جیاوازدا دەپشکنن: while و do … while لەم وانەیەدا، for لە خولی for. شێوەی چوارەم، ، بە ناو و لیستەکاندا دەڕوات (یەکەم سەیرکردن). دووپاتەکردنەوە یەکێکە لە سێ بەردی بناغەی هەموو (سێ بەردی بناغە).
The while loopخولی while
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 repeats its body while its condition is true. Java tests the condition before every round: true means run the body once more; false means jump past the loop to the next statement.
دووپاتە دەکاتەوە تا ڕاست بێت. Java پێش هەموو دەپشکنێت: ڕاست واتە جارێکی تر جێبەجێ بکە؛ هەڵە واتە بە سەر باز بدە بۆ دواتر.
while (loop-continuation-condition) {
// loop body
statement(s);
}- The is a boolean expression in brackets:
trueorfalse(Booleans). - The is the block in braces
{ }. - Something in the body must change the condition, or the loop never ends (Infinite loops).
- لە ناو کەوانەدا:
trueیانfalse(بوولیانەکان). - ئەو کە لە ناو
{ }ـدایە. - شتێک لە ناو دەبێت بگۆڕێت، ئەگینا هەرگیز کۆتایی نایەت (خولە بێکۆتاکان).
Why is it called “while”?بۆچی پێی دەوترێت «while»؟
In English, while means as long as: “while it is raining, stay inside”. A while loop reads the same way. while (x < 100) means “as long as x is less than 100, keep doing this”. The condition is checked before every round, and when it becomes false the loop stops.
لە ئینگلیزیدا while واتە «تا ئەو کاتەی»: «تا ئەو کاتەی باران دەبارێت، لە ماڵەوە بمێنەوە». بە هەمان شێوە دەخوێنرێتەوە. while (x < 100) واتە «تا ئەو کاتەی x لە 100 بچووکترە، ئەمە دووپاتە بکەرەوە». پێش هەموو دەپشکنرێت و کاتێک بوو بە false، دەوەستێت.
The word entered programming with ALGOL 60 (1960), where while was part of the for statement. ALGOL W, designed by Niklaus Wirth and Tony Hoare in 1966, made while a loop of its own, and Wirth kept it in Pascal (1970). C, Java, PHP, Python and MATLAB use the same word today. The whole story of the word is in the Honame section of the while loop term page.
وشەکە لەگەڵ ALGOL 60 (1960) هاتە ناو بەرنامەسازییەوە، کە تێیدا while بەشێک بوو لە for. ALGOL W، کە نیکلاوس ڤیرت (Niklaus Wirth) و تۆنی هۆر (Tony Hoare) لە ساڵی 1966دا دیزاینیان کرد، whileـی کرد بە سەربەخۆ و ڤیرت لە Pascalـدا (1970) هێشتییەوە. ئەمڕۆ C، Java، PHP، Python و MATLAB هەمان وشە بەکاردەهێنن. هەموو چیرۆکی وشەکە لە بەشی «هۆنامە»ی پەڕەی زاراوەی خولی whileدایە.
do...while reads “do this, while the condition holds”: the body runs first and the check comes after it, so the body always runs at least once.
do...while بەم شێوەیە دەخوێنرێتەوە: «ئەمە بکە، تا ئەو کاتەی ڕاستە»: سەرەتا لەشی جێبەجێ دەبێت و پشکنین دوای ئەو دێت، بۆیە لەشەکە هەمیشە لانیکەم یەک جار جێبەجێ دەبێت.
Ten times: a counter-controlled loopدە جار: خولی کۆنترۆڵکراو بە ژمێرەر
This loop prints Welcome to Java! ten times. The variable count counts the rounds: it is declared and set to 0 before the loop and increased by one inside it (count++, see Increment and decrement). You know in advance how many times the body runs, so this is a , and count is its control variable.
ئەم دە جار Welcome to Java! چاپ دەکات. count دەژمێرێت: پێش دەناسێنرێت و دەکرێتە 0 و لە ناو ئەودا یەکێکی بۆ زیاد دەکرێت (count++، بڕوانە زیادکردن و کەمکردنەوە). پێشتر دەزانیت چەند جار جێبەجێ دەبێت، بۆیە ئەمە و count .
public class WelcomeLoop {
public static void main(String[] args) {
int count = 0;
while (count < 10) {
System.out.println("Welcome to Java!");
count++;
}
}
}$ java WelcomeLoop Welcome to Java! Welcome to Java! Welcome to Java! Welcome to Java! Welcome to Java! Welcome to Java! Welcome to Java! Welcome to Java! Welcome to Java! Welcome to Java!
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3
The counter starts at 0, before the loop.
پێش لە 0ـەوە دەست پێ دەکات.
- Lineدێڕی 4
Tested before every round: 10 times true, then false.
پێش هەموو دەپشکنرێت: 10 جار true، پاشان false.
- Lineدێڕی 6
The update: without it the loop would never end.
نوێکردنەوەکە: بەبێ ئەو هەرگیز کۆتایی نایەت.


Counting downژماردنی پێچەوانە
A loop can count down too. num starts at 6, the condition is num >= 0 and num-- takes one away in every round (decrement). The body runs 7 times, for 6, 5, 4, 3, 2, 1 and 0. Then num is −1, the test is false and the loop ends.
دەتوانێت بە پێچەوانەش بژمێرێت. num لە 6ـەوە دەست پێ دەکات، num >= 0ـە و num-- لە هەموو یەکێک کەم دەکاتەوە (کەمکردنەوە). 7 جار جێبەجێ دەبێت، بۆ 6، 5، 4، 3، 2، 1 و 0. پاشان num دەبێتە −1، پشکنینەکە هەڵەیە و کۆتایی دێت.
public class Countdown {
public static void main(String[] args) {
int num = 6;
while (num >= 0) {
System.out.println("The value of num = " + num);
num--;
}
}
}$ java Countdown The value of num = 6 The value of num = 5 The value of num = 4 The value of num = 3 The value of num = 2 The value of num = 1 The value of num = 0
Adding 1 to 20: an accumulatorکۆکردنەوەی 1 تا 20: گۆڕاوی کۆکردنەوە
To add numbers in a loop, keep a running total in a variable that starts at 0 before the loop: an . Every round adds the current number: sum = sum + num (short form sum += num, compound assignment). This program adds 1 + 2 + … + 20:
بۆ کۆکردنەوەی ژمارەکان لە ، کۆی گشتی لە هەڵبگرە کە پێش لە 0ـەوە دەست پێ دەکات: . هەموو ژمارەی ئێستا زیاد دەکات: sum = sum + num (بە کورتی sum += num، کارپێکەرەکانی دانانی نرخ). ئەم 1 + 2 + … + 20 کۆ دەکاتەوە:
public class SumOneToTwenty {
public static void main(String[] args) {
int num = 1;
int sum = 0;
while (num <= 20) {
sum = sum + num;
num++;
}
System.out.println("Sum from 1 to 20 = " + sum);
}
}$ java SumOneToTwenty Sum from 1 to 20 = 210
The formula for the sum of the first n whole numbers gives the same 210, so the loop is right.
کۆی یەکەم n ژمارە هەمان 210 دەدات، بۆیە دروستە.
Adding 1 to 20: an accumulatorکۆکردنەوەی 1 تا 20: گۆڕاوی کۆکردنەوە
Follow the sum by handsum بە دەست بەدوادا بچۆ
A shows every step: one column per variable, one row per round. Here is the same loop with 4 instead of 20; a real run that prints both variables in each round gives num = 1, sum = 1 up to After the loop: num = 5, sum = 10. The method is taught in Tracing a loop with a trace table and Checking with a trace table, and the trace lab traces Java loops step by step.
هەموو هەنگاوێک نیشان دەدات: بۆ هەر ستوونێک و بۆ هەر ڕیزێک. ئەمە هەمان بە 4 لە جیاتی 20؛ جێبەجێکردنێکی ڕاستەقینە کە لە هەر هەردوو چاپ دەکات، لە num = 1, sum = 1ـەوە تا After the loop: num = 5, sum = 10 دەدات. ڕێگاکە لە خشتەی بەدواداچوون: خولێک بە دەست جێبەجێ بکە و پشکنین بە خشتەی بەدواداچوون فێر دەکرێت و تاقیگەی بەدواداچوون Java هەنگاو بە هەنگاو بەدوادا دەچێت.
| Test numberژمارەی پشکنین | num at the testnum لە کاتی پشکنین | num <= 4 | sum after the bodysum دوای |
|---|---|---|---|
| 1 | 1 | true | 1 |
| 2 | 2 | true | 3 |
| 3 | 3 | true | 6 |
| 4 | 4 | true | 10 |
| 5 | 5 | false | 10 (the loop ends)10 ( کۆتایی دێت) |
Even numbers, two waysژمارە جووتەکان، بە دوو ڕێگا
This loop prints the even numbers from 1 to 20. It visits every number and prints only those that leave no remainder when divided by 2: i % 2 == 0 (remainder).
ئەم ژمارە جووتەکان لە 1 تا 20 چاپ دەکات. سەردانی هەموو ژمارەیەک دەکات و تەنیا ئەوانە چاپ دەکات کە کاتێک دابەشی 2 دەکرێن هیچ نامێنێتەوە: i % 2 == 0 (پاشماوە).
public class EvenNumbers {
public static void main(String[] args) {
int i = 1;
System.out.println("Even numbers from 1 to 20:");
while (i <= 20) {
if (i % 2 == 0) {
System.out.print(i + " ");
}
i++;
}
System.out.println();
}
}$ java EvenNumbers Even numbers from 1 to 20: 2 4 6 8 10 12 14 16 18 20
With println instead of print, each number would stand on its own line. Simpler still: start at 2 and jump by 2. No if is needed, and the loop makes 10 rounds instead of 20:
ئەگەر لە جیاتی print نووسرابا println، هەر ژمارەیەک لە دێڕی خۆیدا دەبوو. ڕێگایەکی سادەتر: لە 2ـەوە دەست پێ بکە و دوو دوو باز بدە. پێویست بە if ناکات و لە جیاتی 20، 10 دەکات:
public class EvenStep {
public static void main(String[] args) {
int i = 2;
System.out.println("Even numbers from 1 to 20:");
while (i <= 20) {
System.out.print(i + " ");
i += 2;
}
System.out.println();
}
}$ java EvenStep Even numbers from 1 to 20: 2 4 6 8 10 12 14 16 18 20
Infinite loops and Ctrl+Cخولە بێکۆتاکان و Ctrl+C
If nothing in the body changes the condition, it stays true for ever: an . Forget count++ and count stays 0, so count < 10 is never false. The program prints the same line again and again until you stop it:
ئەگەر هیچ شتێک لە ناو نەگۆڕێت، بۆ هەمیشە ڕاست دەمێنێتەوە: . count++ لەبیر بکە و count هەر 0 دەمێنێتەوە، بۆیە count < 10 هەرگیز هەڵە نابێت. هەمان دێڕ دووپاتە و دووپاتە چاپ دەکات تا ڕای دەگریت:
public class Forgot {
public static void main(String[] args) {
int count = 0;
while (count < 10) {
System.out.println("count is " + count);
}
}
}$ java Forgot count is 0 count is 0 count is 0 count is 0 …
Shortened. In our test it printed 1,373,676 lines in 1.5 seconds before Ctrl+C stopped it.
کورتکراوەتەوە. لە تاقیکردنەوەکەماندا لە 1.5 چرکەدا 1,373,676 دێڕی چاپ کرد پێش ئەوەی Ctrl+C ڕایبگرێت.
Infinite loops and Ctrl+Cخولە بێکۆتاکان و Ctrl+C
A semicolon after while (…)خاڵبۆر لە دوای while (…)
A ; straight after while (…) is a complete statement that does nothing (an empty statement), and it becomes the whole loop body. The block below it is no longer part of the loop. Here i stays 0, so while (i < 3); runs for ever and prints nothing, not even Done:
;ـێک ڕاستەوخۆ لە دوای while (…) تەواوە کە هیچ ناکات ( بەتاڵ) و دەبێتە هەموو . بلۆکەکەی ژێرەوەی ئیتر بەشێک نییە لە . لێرەدا i هەر 0 دەمێنێتەوە، بۆیە while (i < 3); بۆ هەمیشە کار دەکات و هیچ چاپ ناکات، تەنانەت Doneـیش نا:
public class EmptyWhile {
public static void main(String[] args) {
int i = 0;
while (i < 3); {
System.out.println(i);
i++;
}
System.out.println("Done");
}
}$ javac -Xlint:all EmptyWhile.java $ java EmptyWhile ^C
javac accepts it without a word. The program printed nothing; after 3 seconds we pressed Ctrl+C (the ^C).
javac بێ هیچ قسەیەک وەریدەگرێت. هیچی چاپ نەکرد؛ دوای 3 چرکە Ctrl+Cـمان داگرت (^C).
javac warns about an empty statement only after if, not after while, even with -Xlint:all (A semicolon after if). Never put ; after while (…) when a block follows; the one ; that belongs to a loop header ends a do-while loop (next screen).
javac تەنیا لە دوای if ئاگادارت دەکاتەوە لە بەتاڵ، نەک لە دوای while، تەنانەت بە -Xlint:all (خاڵبۆر لە دوای if). هەرگیز لە دوای while (…) ; مەنووسە کاتێک بلۆکێک بەدوایدا دێت؛ تاکە ;ـێک کە سەر بە کۆتایی (شاشەی دواتر).
The do-while loopخولی do...while
A is a while loop turned upside down: it runs the body first and tests the condition afterwards. If the test is true, the body runs again; if it is false, the loop ends.
سەرەوخوارە: سەرەتا جێبەجێ دەکات و پاشان دەپشکنێت. ئەگەر پشکنینەکە ڕاست بێت، دیسان جێبەجێ دەبێت؛ ئەگەر هەڵە بێت، کۆتایی دێت.
do {
// loop body
statement(s);
} while (loop-continuation-condition);public class DoWhileCount {
public static void main(String[] args) {
int i = 1;
do {
System.out.println(i);
i++;
} while (i <= 10);
}
}$ java DoWhileCount 1 2 3 4 5 6 7 8 9 10
Mind the ; after while (i <= 10): it ends the do-while statement, and without it javac stops with ';' expected. A plain while loop must not have one there.
ئاگاداری ;ـەکەی دوای while (i <= 10) بە: do-while تەواو دەکات و بەبێ ئەو javac بە ';' expected دەوەستێت. ئاسایی نابێت لەو شوێنەدا هیچ ;ـێکی هەبێت.
At least onceلانیکەم یەک جار
The difference shows when the condition is false from the start. With the start value 100, while (i <= 10) tests first and never runs its body. The do-while loop runs its body once and only then finds that 101 <= 10 is false:
جیاوازییەکە کاتێک دەردەکەوێت کە لە سەرەتاوە هەڵە بێت. بە سەرەتای 100، while (i <= 10) سەرەتا دەپشکنێت و هەرگیز جێبەجێ ناکات. یەک جار جێبەجێ دەکات و تەنیا پاشان دەبینێت کە 101 <= 10 هەڵەیە:
public class AtLeastOnce {
public static void main(String[] args) {
int i = 100;
while (i <= 10) {
System.out.println("while: " + i);
i++;
}
int j = 100;
do {
System.out.println("do-while: " + j);
j++;
} while (j <= 10);
System.out.println("Finished: i = " + i + ", j = " + j);
}
}$ java AtLeastOnce do-while: 100 Finished: i = 100, j = 101


Python has no do-while loop; it imitates one with while True and break (do … while: Java only).
Python نییە؛ بە while True و break لاسایی دەکاتەوە (do … while: تەنیا لە Javaـدا).
Read until 0: a sentinel valueتا 0 بخوێنەوە: نرخی پاسەوان
Often nobody knows in advance how many numbers will be typed. Then a special value means stop: a . This program adds integers until the input is 0. A do-while fits, because a number must be read before it can be tested (Scanner setup):
زۆرجار کەس پێشتر نازانێت چەند ژمارە دەنووسرێت. ئەوکات تایبەت واتای بوەستە دەدات: . ئەم کۆ دەکاتەوە تا دەبێتە 0. گونجاوە، چونکە دەبێت ژمارەیەک بخوێندرێتەوە پێش ئەوەی بپشکنرێت (ئامادەکردنی Scanner):
import java.util.Scanner;
public class TestDoWhile {
public static void main(String[] args) {
int data;
int sum = 0;
Scanner input = new Scanner(System.in);
do {
System.out.print("Enter an integer (the input ends if it is 0): ");
data = input.nextInt();
sum += data;
} while (data != 0);
System.out.println("The sum is " + sum);
}
}$ java TestDoWhile Enter an integer (the input ends if it is 0): 3 Enter an integer (the input ends if it is 0): 5 Enter an integer (the input ends if it is 0): 6 Enter an integer (the input ends if it is 0): 0 The sum is 14 $ java TestDoWhile Enter an integer (the input ends if it is 0): 0 The sum is 0
The 0 is added too, which is harmless: adding 0 changes nothing. A sentinel must be a value that can never be real data. To test without typing, pipe the numbers in (Testing with a pipe); the prompts then run together on one line:
سفریش کۆ دەکرێتەوە و زیانی نییە: کۆکردنەوەی 0 هیچ ناگۆڕێت. دەبێت بێت کە هەرگیز ڕاستەقینە نەبێت. بۆ تاقیکردنەوە بەبێ نووسین، ژمارەکان بە pipe بنێرە ناو (تاقیکردنەوە بە pipe)؛ ئەوکات لە یەک دێڕدا بەدوای یەکدا دێن:
printf '3\n5\n6\n0\n' | java TestDoWhile$ printf '3\n5\n6\n0\n' | java TestDoWhile Enter an integer (the input ends if it is 0): Enter an integer (the input ends if it is 0): Enter an integer (the input ends if it is 0): Enter an integer (the input ends if it is 0): The sum is 14
Exercise: the largest numberڕاهێنان: گەورەترین ژمارە
Suppose the input is 2 3 4 5 0. What does this program print? Trace number and max before you look at the answer.
وا دابنێ 2 3 4 5 0ـە. ئەم چی چاپ دەکات؟ پێش سەیرکردنی وەڵامەکە بە دوای number و maxـدا بچۆ.
import java.util.Scanner;
public class MaxUntilZero {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int number, max;
number = input.nextInt();
max = number;
do {
number = input.nextInt();
if (number > max) {
max = number;
}
} while (number != 0);
System.out.println("max is " + max);
System.out.println("number " + number);
}
}Hintئاماژە
number is read before the loop; the loop reads the rest.
number پێش دەخوێندرێتەوە؛ ئەوانی تر دەخوێنێتەوە.
Show answerوەڵامەکە پیشان بدە
$ java MaxUntilZero 2 3 4 5 0 max is 5 number 0
| Readخوێندراوە | number | number > max | max | number != 0 |
|---|---|---|---|---|
| before the loopپێش | 2 | — | 2 | — |
| round 1 1 | 3 | true | 3 | true |
| round 2 2 | 4 | true | 4 | true |
| round 3 3 | 5 | true | 5 | true |
| round 4 4 | 0 | false | 5 | false |
The first number, 2, is read before the loop and becomes max. The loop reads 3, 4, 5 and 0, and max grows to 5. The 0 ends the loop, but it is compared first; it is not larger than 5. All five numbers can be typed on one line because Scanner reads token by token (Tokens).
یەکەم ژمارە، 2، پێش دەخوێندرێتەوە و دەبێتە max. 3، 4، 5 و 0 دەخوێنێتەوە و max گەورە دەبێت تا 5. 0 کۆتایی پێ دەهێنێت، بەڵام سەرەتا بەراورد دەکرێت؛ لە 5 گەورەتر نییە. هەر پێنج ژمارەکە دەتوانرێت لە یەک دێڕدا بنووسرێن، چونکە Scanner پارچە بە پارچە دەخوێنێتەوە (پارچەکان).
Test first or test lastپشکنین لە سەرەتا یان لە کۆتایی
while tests its condition before the body: it is a . do … while tests after the body: it is a . A pretest loop may run its body zero times; a posttest loop always runs it at least once. Choose do-while when the body must run before the test makes sense, such as reading a number or showing a menu, and while otherwise. for is a pretest loop too (Which loop?).
while پێش دەپشکنێت: ـە. do … while دوای دەپشکنێت: ـە. لەوانەیە سفر جار جێبەجێ بکات؛ هەمیشە لانیکەم یەک جار جێبەجێی دەکات. کاتێک دەبێت پێش ئەوەی پشکنین واتای هەبێت جێبەجێ بێت، وەک خوێندنەوەی ژمارەیەک یان پیشاندانی ، do-while هەڵبژێرە؛ ئەگینا while. forـیش (کام خول؟).
Common mistakesهەڵە باوەکان
| You wroteنووسیت | What happensچی ڕوو دەدات | Fixچارەسەر |
|---|---|---|
no count++ in the bodyهیچ count++ـێک لە نییە | infinite loop: the same line for ever: هەمان دێڕ بۆ هەمیشە | Update the variable in the body. لە ناو نوێ بکەرەوە. |
while (i < 3); { … } | runs for ever, prints nothing, no warningبۆ هەمیشە کار دەکات، هیچ چاپ ناکات، بێ | Delete the ;.;ـەکە بسڕەوە. |
} while (i <= 10) | ';' expected | A do-while ends with );. بە ); کۆتایی دێت. |
while (count = 10) | incompatible types: int cannot be converted to boolean | Compare: count < 10.بەراورد بکە: count < 10. |
count = 0; while (count <= 10) | 11 rounds instead of 10 (real run: Rounds: 11)11 لە جیاتی 10 (جێبەجێکردنی ڕاستەقینە: Rounds: 11) | count < 10, or start at 1.count < 10، یان لە 1ـەوە دەست پێ بکە. |
sum = 0; inside the loopsum = 0; لە ناو | the total restarts every roundکۆکە لە هەموو لە سەرەتاوە دەست پێ دەکاتەوە | Set the accumulator before the loop. پێش دابنێ. |
do { int n = …; } while (n != 0); | cannot find symbol | Declare int n; before do.int n; پێش do بناسێنە. |
| the sentinel compared as data وەک بەراورد کراوە | max is 0 for negative inputmax is 0 بۆ نەرێنی | Test number != 0 first.سەرەتا number != 0 بپشکنە. |
The real javac messages of rows 3, 4 and 7:
پەیامە ڕاستەقینەکانی javac بۆ ڕیزەکانی 3، 4 و 7:
$ javac MissingSemicolon.java
MissingSemicolon.java:7: error: ';' expected
} while (i <= 10)
^
1 error
$ javac AssignInCondition.java
AssignInCondition.java:4: error: incompatible types: int cannot be converted to boolean
while (count = 10) {
^
1 error
$ javac DoScope.java
DoScope.java:10: error: cannot find symbol
} while (n != 0);
^
symbol: variable n
location: class DoScope
1 errorPracticeڕاهێنان
Use a while loop to print 10 20 30 … 100 on one line.
بە 10 20 30 … 100 لە یەک دێڕدا چاپ بکە.
Hintئاماژە
Start at 10 and add 10 each round.
لە 10ـەوە دەست پێ بکە و هەر 10 زیاد بکە.
Show answerوەڵامەکە پیشان بدە
public class Tens {
public static void main(String[] args) {
int n = 10;
while (n <= 100) {
System.out.print(n + " ");
n += 10;
}
System.out.println();
}
}$ java Tens 10 20 30 40 50 60 70 80 90 100
Read a whole number and print the sum of its digits (2026 gives 2 + 0 + 2 + 6 = 10).
بخوێنەوە و کۆی چاپ بکە (2026 دەبێتە 2 + 0 + 2 + 6 = 10).
Hintئاماژە
Repeat while n > 0: add n % 10, then n = n / 10.
تا n > 0 بێت دووپاتە بکەرەوە: n % 10 زیاد بکە، پاشان n = n / 10.
Show answerوەڵامەکە پیشان بدە
import java.util.Scanner;
public class DigitSum {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Whole number: ");
int n = input.nextInt();
int sum = 0;
while (n > 0) {
sum += n % 10;
n = n / 10;
}
System.out.println("Sum of the digits: " + sum);
}
}$ java DigitSum Whole number: 2026 Sum of the digits: 10 $ java DigitSum Whole number: 7 Sum of the digits: 7
n % 10 is the last digit and n / 10 drops it (integer division). The loop stops when n reaches 0.
n % 10 دوا و n / 10 لای دەبات (دابەشکردنی ژمارەی تەواو). کاتێک دەوەستێت کە n بگاتە 0.
Ask for a mark from 0 to 100 and ask again until it is valid. Test with 150, -3 and 87.
داوای نمرەیەک لە 0 تا 100 بکە و دیسان بپرسە تا دروست بێت. بە 150، -3 و 87 تاقی بکەرەوە.
Show answerوەڵامەکە پیشان بدە
import java.util.Scanner;
public class AskMark {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int mark;
do {
System.out.print("Mark (0-100): ");
mark = input.nextInt();
if (mark < 0 || mark > 100) {
System.out.println("Not a valid mark.");
}
} while (mark < 0 || mark > 100);
System.out.println("Mark accepted: " + mark);
}
}$ java AskMark Mark (0-100): 150 Not a valid mark. Mark (0-100): -3 Not a valid mark. Mark (0-100): 87 Mark accepted: 87
A do-while fits: the question must be asked once before there is anything to check.
گونجاوە: دەبێت پرسیارەکە یەک جار بکرێت پێش ئەوەی شتێک هەبێت بۆ پشکنین.
Find the smallest n for which 1 + 2 + … + n is more than 100.
بچووکترین n بدۆزەرەوە کە 1 + 2 + … + n لە 100 زیاتر بێت.
Show answerوەڵامەکە پیشان بدە
public class PassHundred {
public static void main(String[] args) {
int n = 0;
int sum = 0;
while (sum <= 100) {
n++;
sum += n;
}
System.out.println("n = " + n + ", sum = " + sum);
}
}$ java PassHundred n = 14, sum = 105
The formula agrees: 13 is not enough, 14 is.
هاوڕایە: 13 بەس نییە، 14 بەسە.
What does this print? Write down x after every round.
ئەمە چی چاپ دەکات؟ x لە دوای هەموو بنووسە.
int x = 1;
while (x < 50) {
x = x * 3;
}
System.out.println(x);Show answerوەڵامەکە پیشان بدە
$ java TimesThree 81
x goes 1, 3, 9, 27, 81. The test 81 < 50 is false, so 81 is printed.
x بەم شێوەیە دەڕوات: 1، 3، 9، 27، 81. پشکنینی 81 < 50 هەڵەیە، بۆیە 81 چاپ دەکرێت.
Rewrite TestDoWhile with a while loop. Where must the first number be read?
TestDoWhile بە بنووسەرەوە. دەبێت یەکەم ژمارە لە کوێ بخوێندرێتەوە؟
Hintئاماژە
Read before the loop, and again as the last line of the body.
پێش بخوێنەوە و دیسان وەک دوا دێڕی .
Show answerوەڵامەکە پیشان بدە
import java.util.Scanner;
public class SumWhile {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int sum = 0;
System.out.print("Enter an integer (the input ends if it is 0): ");
int data = input.nextInt();
while (data != 0) {
sum += data;
System.out.print("Enter an integer (the input ends if it is 0): ");
data = input.nextInt();
}
System.out.println("The sum is " + sum);
}
}$ java SumWhile Enter an integer (the input ends if it is 0): 3 Enter an integer (the input ends if it is 0): 5 Enter an integer (the input ends if it is 0): 6 Enter an integer (the input ends if it is 0): 0 The sum is 14 $ java SumWhile Enter an integer (the input ends if it is 0): 0 The sum is 0
The number is read once before the loop and again at the end of the body, so the test always sees a fresh number. Unlike the do-while version, the 0 is never added.
ژمارەکە یەک جار پێش و دیسان لە کۆتایی دەخوێندرێتەوە، بۆیە پشکنینەکە هەمیشە ژمارەیەکی نوێ دەبینێت. بە پێچەوانەی وەشانی do-while، 0 هەرگیز کۆ ناکرێتەوە.
Summaryکورتە
- A loop repeats statements; one pass is one iteration. Java has
while,do … whileandfor. while (condition) { body }tests before every round; the body needs an update, or the loop never ends (stop it with Ctrl+C).- Counters count rounds; accumulators collect a total and start at 0 before the loop.
do { body } while (condition);runs the body first, at least once, and ends with;.- A sentinel value such as 0 ends the input; it must never be real data.
whileis a pretest loop (0 or more rounds);do … whileis a posttest loop (1 or more).
- دووپاتە دەکاتەوە؛ هەر تێپەڕبوونێک یەک . Java
while،do … whileوforـی هەیە. while (condition) { body }پێش هەموو دەپشکنێت؛ پێویستی بە نوێکردنەوە هەیە، ئەگینا هەرگیز کۆتایی نایەت (بە Ctrl+C ڕای بگرە).- دەژمێرن؛ کۆیەک کۆ دەکەنەوە و پێش لە 0ـەوە دەست پێ دەکەن.
do { body } while (condition);سەرەتا جێبەجێ دەکات (لانیکەم یەک جار) و بە;کۆتایی دێت.- وەک 0 کۆتایی بە دەهێنێت؛ نابێت هەرگیز ڕاستەقینە بێت.
while(0 یان زیاتر)؛do … while(1 یان زیاتر).
How many times does while (count < 10) test its condition when count goes from 0 to 10?کاتێک count لە 0ـەوە تا 10 دەڕوات، while (count < 10) چەند جار مەرجەکەی دەپشکنێت؟
11 times: ten times true and a last time false.
11 جار: دە جار ڕاست و دوا جار هەڵە.
Why does while (i < 3); hang?بۆچی while (i < 3); گیر دەخوات و هیچ ناکات؟
The ; is an empty loop body; nothing changes i, so the condition stays true for ever.
;ـەکە خۆی و بەتاڵە؛ هیچ شتێک i ناگۆڕێت، بۆیە بۆ هەمیشە ڕاست دەمێنێتەوە.
When is do-while the better choice?کەی خولی do...while هەڵبژاردنی باشترە؟
When the body must run once before the test means anything: reading input, showing a menu.
کاتێک دەبێت یەک جار جێبەجێ بێت پێش ئەوەی پشکنین واتای هەبێت: خوێندنەوەی ، پیشاندانی .
Lesson complete!وانەکە تەواو بوو!
Well done. Review the key terms, then go on to the next lesson.دەستخۆش. سەیرێکی زاراوە سەرەکییەکان بکەرەوە، پاشان بڕۆ بۆ وانەی داهاتوو.
Key termsزاراوە سەرەکییەکان
The for Loop and Nested Loopsخولی for و خولە ناوەکییەکان
Related lecturesوانە پەیوەندیدارەکان
- The for Loop and Nested Loopsخولی for و خولە ناوەکییەکان
- break and continueڕستەی break و ڕستەی continue
- The while Loopخولی while
- Python Next to JavaPython لە تەنیشت Javaـەوە
- while Loops, break and continueخولەکانی while، break و continue
- Algorithms, Pseudocode and Flowchartsئەلگۆریزمەکان، نیمچەکۆد و هێڵکارییەکانی ڕەوت
References and further readingسەرچاوەکان و خوێندنەوەی زیاتر
- W3Schools: Java While Loop
- W3Schools: Java Do/While Loop
- The Java Tutorials: The while and do-while Statements
- Java Language Specification (Java SE 17), §14.6 The Empty Statement, §14.12 The while Statement, §14.13 The do Statement
- Wikipedia: For loop (the keyword's history: Superplan's für, ALGOL 58)
- Wikipedia: Superplan (Heinz Rutishauser, 1949–1951)
- Wikipedia: ALGOL W (the while statement; Wirth and Hoare, 1966)
This lecture is a first edition. The Kurdish text and terminology are waiting for review by Kurdish IT teachers.ئەم وانەیە چاپی یەکەمە. دەقە کوردییەکە و زاراوەکانی چاوەڕێی پێداچوونەوەی مامۆستایانی کوردی بواری IT ن.