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

KwickAcademy Python · 8 min · free

while Loop, break and continue

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

Learn the Python while loop, avoiding infinite loops, break and continue, for vs while, and three exam programs. A while loop repeats its body as long as a condition is True, checked before every round.

Follows the syllabus of: CBSE Class 11 Computer Science (083)

On screen in this lesson

What is a while loop?

Repeats while a condition is True
Condition is checked before every round
If False at the start, body never runs
Something inside must change the condition

Avoiding infinite loops

Set the loop variable before the loop
Change it inside the loop body
Make sure the condition can become False
Check the update is inside the indent

Tracing break

Roundnum == 4?What happens
1Falseprints 1
2Falseprints 2
3Falseprints 3
4Truebreak, exit loop

Tracing continue

Roundnum == 3?What happens
1Falseprints 1
2Falseprints 2
3Trueskip the print
4Falseprints 4
5Falseprints 5

for vs while

LoopUse it whenExample
forcount is knowntable of 7
forgoing over itemsletters of a name
whilecount unknowndigits of a number
whilestop on an eventuntil guess right

Trace: reversing 1234

numdigitrev
123444
123343
122432
114321

Quick answers

What is the difference between break and continue?

break leaves the loop; continue skips only the current round.

When should you use while instead of for?

When the number of repeats is unknown, like digits of a number or a guessing game.

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. A number guessing game keeps asking until you get it right. Nobody knows how many tries that will take. So how does the program know when to stop? Today we will learn the while loop, break and continue, and three exam programs.

A loop is code that repeats, and each repeat is called an iteration. A while loop repeats its body as long as a condition is True. A condition is a question with a True or False answer. Python checks it before every round. If it is False at the very start, the body never runs even once. So something inside the loop must change, or the loop never ends.

One equals sign stores a value, so count starts at one. The while line asks, is count less than or equal to five? The line ends with a colon, and the body is indented. Inside, we print count with a space, and then add one to count. When count becomes six, the condition is False and the loop stops. Then Python prints Done.

Here is the same loop as a flowchart. First, count is set to one. The diamond asks, is count less than or equal to five? If yes, print count, add one, and the arrow goes back up to the diamond. If no, the flow goes down and prints Done.

Look carefully at this loop. It adds count to total, but count is never changed. Count stays one, so the condition is always True. This is called an infinite loop, a loop that never stops. The program seems to freeze. Press Control and C together to stop it.

Follow four checks to avoid infinite loops. Set the loop variable before the loop starts. Change it inside the loop body, every round. Make sure the change moves toward making the condition False. And check that the update line is indented inside the loop, not below it.

The word break stops the loop at once, even if values are left. Double equals asks, are these two values equal? Here range of one comma eight gives one to seven. When num is four, break runs. Python jumps out and prints Loop over. Break works the same way inside a while loop.

Let us trace it step by step. In round one, num is one, the check is False, so it prints one. In round two, it prints two. In round three, it prints three. In round four, the check is True, so break runs, and five, six and seven are never used.

The word continue skips the rest of the body for this round only. Then the loop goes on with the next value. Think of skipping one song in a playlist, not switching the music off. When num is three, continue runs, so three is not printed. The loop carries on with four and five.

Now trace continue. In round one, it prints one. In round two, it prints two. In round three, the check is True, so the print line is skipped. In round four, the loop is still running, so it prints four. In round five, it prints five, and the range ends.

Pause and predict. Num is exactly ten, and the condition is num greater than ten. How many times does the body run? Zero times. Ten is not greater than ten, so the condition is False at the start. Python skips the loop and prints only End.

So which loop should you use? Use for when you know how many times to repeat, like the table of seven. Use for when you go over items, like the letters of a name. Use while when the count is unknown, like the digits of a number of any length. Use while when you stop on an event, like a correct guess.

Now three exam programs. First, reverse a number. Two new operators here. The percent sign is modulus, which gives the remainder, so num modulus ten is the last digit. Double slash is floor division, which drops the decimal part, so num floor divided by ten removes the last digit. The star sign multiplies. Rev, short for reverse, collects the digits in the new order.

Let us trace it. Num is one two three four, the digit is four, and rev becomes four. Then num is one two three, the digit is three, and rev becomes forty three. Next the digit is two, and rev becomes four hundred thirty two. Finally the digit is one, rev is four three two one, and num becomes zero, so the loop stops. Pause and predict: what is the reverse of twelve hundred? It is twenty one, because leading zeros disappear.

Second, the sum of digits. The pattern is almost the same. Each round, we add the last digit to total, and then remove it. The digits of four zero seven two are two, seven, zero and four. Two plus seven plus zero plus four is thirteen.

Third, a number guessing game. While True makes a loop that would run forever. But break gives it a way out. Input asks the player to type a number, and int changes it into a whole number. In this sample run, the player types four, which is wrong. Then the player types seven, break runs, and it prints Correct. A real game picks the secret with the random module.

Let us revise what we learned today. A while loop repeats as long as its condition is True. Always change the loop variable inside, or you get an infinite loop. Break leaves the loop at once, and continue skips just one round. Use for when the count is known, and while when it is not. And for digit programs, modulus ten gives the last digit, and floor division by ten removes it.

Courses that teach this

CourseUnit
CBSE Class 10 Computer Applications (165)HTML
CBSE Class 11 Computer Science (083)Computational Thinking and Programming - I
CBSE Class 11 Computer Science Essentials (083)Computational Thinking and Programming - I
CBSE Class 11 Informatics Practices (065)Introduction to Python
ISC Class 12 Computer Science (868)Statements and Scope
Programming All levels PythonControl Flow
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 →