CBSE 2026 results are out, Mukul scored a perfect 100/100 in Computer ScienceSee all toppers →

KwickAcademy Java · 6 min · free

Loops in Java: for, while and do-while

6 min5 KwickClipsFull text belowFree
Next lesson →Kajal Ma'am (MCA), teaching since 2004Remembered in this browser

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

Iterationi <= 3?Prints
1st, i = 1true1
2nd, i = 2true2
3rd, i = 3true3
i = 4falseloop ends

while vs do-while

Pointwhiledo-while
Condition checkedbefore the bodyafter the body
Typeentry controlledexit controlled
Minimum runszeroone
Semicolon at endnoyes

Choosing the right loop

SituationLoopExample
Count is knownfortables 1 to 10
Count not knownwhiledigits of a number
Run at least oncedo-whileshow a menu

Common loop errors

MistakeExampleResult
; after for( )for(...); { }body runs once
No updatewhile (i<=5) { }runs forever
Off by onei < 10 for 1-10prints 1 to 9
i out of scopeuse i after loopcompile 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.

The 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

CourseUnit
ICSE Class 9 Computer ApplicationsIterative Constructs in Java
ISC Class 11 Computer Science (868)Statements, Control Flow and Functions
GSEB Std 11 Computer StudiesAdvanced Scripting
GSEB Std 12 Computer StudiesObject-Oriented Concepts and Java Basics
Programming All levels JavaControl Flow and Iteration
Programming All levels CControl Structures

Voice-over in this lesson is AI-generated. The script is written and checked by Kajal Ma'am. Boards can revise a syllabus mid-year, so confirm anything you plan around against the official board circular. Keep your passwords, OTPs and ID numbers to yourself — we never ask for them. To reach Kajal Ma'am, use the WhatsApp button; sharing your number there is how we call you back.

Free to watch, no sign-up. Live classes with Kajal Ma'am are the paid course; these lessons stay free either way.

Want a plan that actually fits your board dates?

Ask Kajal Ma'am directly, 20+ years teaching computer science. Free demo class first, no payment.

Talk to Kajal Ma'am on WhatsApp

Or see the Class 12 Computer Science course →

Studying outside India?

We coach CBSE, IGCSE & international students across the globe, one-to-one, in your local time zone.

Visit International →