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

KwickAcademy Java · 7 min · free

Converting Between for, while and do-while, and Finite vs Infinite Loops

7 min4 KwickClipsFull text belowFree
Kajal Ma'am (MCA), teaching since 2004Remembered in this browser

Learn to rewrite the same loop as for, while and do-while, when a loop runs zero times, and what finite, infinite and empty loops are. Converting a loop just means moving its four parts: initialisation, condition, body and update.

Follows the syllabus of: ICSE Class 9 Computer Applications

On screen in this lesson

The four parts of a loop

Initialisation: give the loop variable a start value
Condition: checked to decide whether to repeat
Body: the statements that repeat
Update: change the loop variable each round

Where each part goes

Partforwhile, do-while
Initialisationinside bracketsbefore the loop
Conditioninside bracketswhile (...)
Updateinside bracketsend of the body

Entry vs exit controlled

for and while: entry controlled, test first
do-while: exit controlled, test after the body
Entry controlled loops can run zero times
do-while always runs at least once

Finite, infinite and empty

LoopMeaningExample
Finiteends after N roundsi = 1 to 5
Infinitenever endswhile (true)
Emptybody is only ;for (...);

Trace: k and sum

Roundksum
11010
2717
3421
4122
stop-222

Board exam tips

Keep the same start, condition and update
Put the update after the body statements
Never forget the ; after do-while's condition
Check a false start: do-while still runs once
Always trace to count the rounds

Quick answers

Can a loop run zero times?

Yes. for and while test first, so they can. A do-while always runs at least once.

Where does the update go when you convert a for loop to while?

At the end of the loop body, after the other statements.

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 every for loop be written as a while loop, and as a do while loop? Almost always, yes. But there is one case where the output changes. Today we will convert the same loop three ways, and find loops that run zero times. Then we learn finite, infinite and empty loops, with board questions.

Every counting loop has four parts, and converting a loop simply means moving these parts. Initialisation gives the loop variable its starting value. The condition is a true or false test, which decides whether to repeat. The body is the set of statements that repeat. The update changes the loop variable each round, so the loop can end.

Here is our loop, which prints the odd numbers from one to nine. In a for loop, all three control parts sit together in the brackets. The initialisation is i equals one. The condition is i less than or equal to nine. The update is i plus equals two. It prints one, three, five, seven, nine.

Now the same loop as a while loop. The initialisation moves above the loop, as int i equals one. The condition stays inside the brackets of while. The update moves to the last line of the body. The output is exactly the same.

And now as a do while loop. The initialisation is again above the loop. The body comes first, after the word do. The condition moves to the end, after the closing brace. Notice the semicolon after the while brackets, which students often forget. The output is the same again.

Here is the conversion rule in one table. For the initialisation, a for loop keeps it inside the brackets, while the other two put it before the loop. The condition goes in the for brackets, or in the while brackets. The update goes in the for brackets, or at the end of the body.

Now the one difference that matters. For and while are entry controlled loops, which means the condition is tested before the body. Do while is an exit controlled loop, which means the body runs first, and the test comes after. So an entry controlled loop can run zero times. But a do while loop always runs at least once.

Here i starts at ten. The while loop first asks, is ten less than or equal to nine? No. So the body never runs, not even once. The loop runs zero times, and the program only prints Done.

Pause and predict. With the same start value, what does the do while version print? It prints ten, and then Done. The body runs first and prints ten, and only then is the condition checked. So when the condition is false at the start, the two loops are not equal. Board papers test exactly this.

Loops are also named by how they run. A finite loop ends after a fixed number of rounds, like i from one to five. An infinite loop never ends, because its condition never becomes false. An empty loop has no body at all, only a semicolon, and some textbooks call it a delay loop.

Here are three separate examples of infinite loops. A for loop with an empty condition repeats forever. While true also repeats forever. The last one is the kind students write by mistake. It has no update, so i stays one, and one prints again and again. To stop a running program, press Ctrl+C, or the stop button in your editor.

Look closely at the semicolon at the end of the for line. That semicolon is the whole body, so this is an empty loop. It only counts i up, while i is five or less. When i becomes six, the loop stops. The print line is outside the loop, so it runs once and prints six.

Here is a typical board question. Rewrite this for loop using while, and state how many times it runs. The loop starts k at ten, runs while k is greater than zero, and subtracts three each time. Each round adds k to sum. Let us first trace it, then convert it.

We trace it round by round. In round one, k is ten, and sum becomes ten. In round two, k is seven, and sum is seventeen. In round three, k is four, and sum is twenty one. In round four, k is one, and sum is twenty two. Then k becomes minus two, the condition is false, and the loop stops after four rounds.

Here is the answer. The initialisation, k equals ten, moves before the loop. The condition, k greater than zero, goes into the while brackets. The update, k minus equals three, comes last in the body, after sum plus equals k. It still prints twenty two, and it still runs four times.

Five tips for loop conversion questions. Keep the same start value, condition and update, and only move them. Put the update after the other body statements, or the values will change too early. Never forget the semicolon after the do while condition. Check what happens if the condition is false at the start, because do while still runs once. And always trace the loop, to count the rounds correctly.

Let us revise. Every counting loop has an initialisation, a condition, a body and an update. Converting a loop from one form to another only means moving these parts. For and while test first, so they can run zero times, but do while always runs at least once. A finite loop ends, an infinite loop never ends, and an empty loop has only a semicolon as its body.

Courses that teach this

CourseUnit
ICSE Class 9 Computer ApplicationsIterative Constructs in Java

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 →