KwickAcademy Java · 6 min · free
Loops in Java: for, while and do-while
How loops repeat work: for loop anatomy, entry controlled while, exit controlled do-while, choosing between them, and the errors that break a loop.
Follows the syllabus of: ICSE Class 9 Computer Applications, ISC Class 11 Computer Science (868), GSEB Std 11 Computer Studies, GSEB Std 12 Computer Studies
On screen in this lesson
Why loops?
| A loop repeats a block of statements |
| Each repeat is called an iteration |
| A counter variable counts the iterations |
| A condition decides when to stop |
The counter in action
| Iteration | i <= 3? | Prints |
|---|---|---|
| 1st, i = 1 | true | 1 |
| 2nd, i = 2 | true | 2 |
| 3rd, i = 3 | true | 3 |
| i = 4 | false | loop ends |
while vs do-while
| Point | while | do-while |
|---|---|---|
| Condition checked | before the body | after the body |
| Type | entry controlled | exit controlled |
| Minimum runs | zero | one |
| Semicolon at end | no | yes |
Choosing the right loop
| Situation | Loop | Example |
|---|---|---|
| Count is known | for | tables 1 to 10 |
| Count not known | while | digits of a number |
| Run at least once | do-while | show a menu |
Common loop errors
| Mistake | Example | Result |
|---|---|---|
| ; after for( ) | for(...); { } | body runs once |
| No update | while (i<=5) { } | runs forever |
| Off by one | i < 10 for 1-10 | prints 1 to 9 |
| i out of scope | use i after loop | compile error |
Recap
| A loop repeats a body; a counter tracks it |
| for (initialisation; condition; update) |
| while is entry controlled, do-while exit controlled |
| Known count: for; unknown: while; at least once: do-while |
| Watch for stray ;, missing update and off by one |
Quick answers
Why did my do-while run when the condition was false?
It checks the condition at the end, so the body always runs at least once.
Why does a semicolon after for() break the loop?
It becomes an empty body, so the block below runs only once.
KwickClips from this lesson
Short clips, one idea each. Good for revision the night before.
Do you need 100 print lines for 1 to 100?44 sec
What are the three parts of a for loop?42 sec
How can a loop run when the condition is false?40 sec
Which loop suits printing a table 1 to 10?43 sec
Why did my loop add only once?41 secThe full lesson, in text
Hello students, welcome to Kwickprep. Can you print the numbers one to a hundred without writing a hundred print lines? Yes, with a loop. Today we learn the for loop, the while loop and the do while loop. We will also see which loop to choose, and the common mistakes that make a loop run forever.
Think of a teacher calling the roll number of every student in a class of forty. The same action repeats, only the number changes. A loop is a statement that repeats a block of code. Each single repeat is called an iteration. A counter is a variable that changes by one each time, and counts the iterations. A condition, checked again and again, decides when the loop stops.
Let us watch a counter called i, printing one to three. In the first iteration, i is one, the condition i less than or equal to three is true, and it prints one. Then i becomes two, the condition is still true, and it prints two. Then i becomes three, still true, and it prints three. Now i becomes four, the condition is false, and the loop ends.
Here is that counter as a for loop. The brackets after for hold three parts, separated by semicolons. Int i equals one is the initialisation. I less than or equal to three is the condition. I plus plus is the update, which adds one to i. The part in curly brackets is the body, the code that repeats. Change three to a hundred, and the same loop prints one to a hundred.
This flowchart shows the order. The initialisation runs only once, at the very start. Then the condition is checked, and if it is true, the body runs, the update adds one, and we go back to the condition. When the condition is false, the loop stops.
A while loop checks its condition before every iteration, so it is called an entry controlled loop. Here bal is a mobile balance of thirty rupees. While the balance is ten or more, a ten rupee pack is used. It runs three times, and then the balance is zero. If bal started at five, the body would not run even once.
Pause and predict. The variable n is five, and the condition asks, is n less than three? It is false, yet five is printed. A do while loop runs its body first and checks the condition at the end. So it is called an exit controlled loop, and it always runs at least once. Notice the semicolon after the while at the end.
Let us compare the two. A while loop checks the condition before the body, and a do while checks it after. So while is entry controlled, and do while is exit controlled. A while loop may run zero times, but a do while runs at least once. Finally, only the do while needs a semicolon after its condition.
So which loop should you choose? When you know how many times to repeat, like printing a table from one to ten, use a for loop. When you do not know the count in advance, like counting the digits of a number, use a while loop. When the body must run at least once, like showing a menu before reading a choice, use a do while loop.
Here is why while suits an unknown count. The number n is four zero nine six. Dividing an int by ten removes its last digit. So n becomes four zero nine, then forty, then four, then zero. Each time count goes up by one, so it prints four. We did not need to know the number of digits in advance.
Now the common mistakes. A semicolon right after the for brackets makes an empty loop, so the body below runs only once. Forgetting the update means the condition never becomes false, so the loop runs forever, which is an infinite loop. Using less than instead of less than or equal to misses the last value, which is an off by one error. And a variable declared inside the for brackets cannot be used after the loop, so Java gives a compile error.
Spot the error. We wanted sum to become three, but it prints one. Look closely at the end of the for line. The semicolon is an empty body, so the loop repeats nothing, three times. The block in curly brackets is then just normal code that runs once. Remove that semicolon, and it prints three.
Let us recap. A loop repeats a body, and a counter keeps track of the iterations. A for loop has initialisation, condition and update in its brackets. A while loop is entry controlled, and a do while loop is exit controlled, so it runs at least once. Use for when the count is known, while when it is not, and do while when the body must run once. And watch for a stray semicolon, a missing update, and off by one errors.
Courses that teach this
| Course | Unit |
|---|---|
| ICSE Class 9 Computer Applications | Iterative Constructs in Java |
| ISC Class 11 Computer Science (868) | Statements, Control Flow and Functions |
| GSEB Std 11 Computer Studies | Advanced Scripting |
| GSEB Std 12 Computer Studies | Object-Oriented Concepts and Java Basics |
| Programming All levels Java | Control Flow and Iteration |
| Programming All levels C | Control Structures |
Free to watch, no sign-up. The live classes are the paid course; these lessons stay free either way.
Disclaimer. KwickAcademy is free study material for general learning and revision. Parts of it, including the voice-over, are produced with the help of AI tools and may contain errors; if you spot one, please tell us and we will correct it. Syllabus, marks and exam details follow the latest official board publications available to us, and boards can change them at any time, so always confirm against your board's official website and your school. Using this material does not guarantee any marks or result. Board names and trademarks belong to their owners; Kwickprep is not affiliated with or endorsed by any examination board. We never ask for passwords, OTPs or ID numbers. Your progress is saved only in this browser. Full disclaimer · Privacy

