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

KwickAcademy C · 7 min · free

Loops in C: while, do-while and for

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

A loop repeats a block of code; while and for check the condition first, and do-while always runs at least once.

Follows the syllabus of: GSEB Std 10 Computer Studies

On screen in this lesson

What is a loop?

A loop repeats a block of code
Each repeat is called an iteration
A condition decides when to stop
A loop variable counts the rounds

while vs do-while vs for

Pointwhile / fordo-while
Condition checkedbefore the bodyafter the body
Type of loopentry controlledexit controlled
Runs at least oncenot alwaysalways
Semicolon at endnoyes, after while

Which loop when?

for: you know how many times, like 10 tables
while: repeat until something happens
do-while: must run once, like a menu

Quick recap

while and for check first; do-while runs once
for: start; condition; update
Nested loops: outer = rows, inner = columns
break leaves the loop; continue skips a round
Start sum at 0 and product at 1

Quick answers

Which loop runs at least once?

do-while, because it checks after the body.

Why must fact start at 1?

Anything multiplied by 0 stays 0.

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. Suppose your teacher asks you to print your roll number one hundred times. Will you write one hundred printf lines? No. Today we will learn loops, which repeat work for us. We will compare while, do while and for, draw star patterns, use break and continue, and write exam programs.

First, some new words. A loop is a structure that repeats a block of code again and again. Each single repeat is called an iteration, like one lap around a school ground. A condition decides whether to go for one more round, or stop. Usually a loop variable counts the rounds, and it must change each time, or the loop never ends.

Here is a loop drawn as a flowchart. First, the loop variable i is set to one. The diamond asks, is i less than or equal to five? While the answer is yes, the side box prints i and adds one to it, again and again. When the answer becomes no, we leave the loop and stop.

This is the same idea as a while loop. We set i to one before the loop. While i is less than or equal to five, the body runs. It prints i and then i plus plus adds one. When i becomes six, the condition is false, and the loop ends. Forget i plus plus, and it prints one forever, which is called an infinite loop.

A do while loop runs the body first, and checks the condition after. So its body always runs at least once. Here the balance is zero, so the condition is false from the start. Still, the body runs once and prints Balance zero. Notice the semicolon after the while at the end, which is compulsory.

A for loop puts three parts in one line, separated by semicolons. The first part, int i equals one, is the start. The second, i less than or equal to five, is the condition. The third, i plus plus, is the update, which runs after each round. The body prints i into two, so we get the first five even numbers.

Exams often ask you to compare the three loops. While and for check the condition before the body, but do while checks it after. So while and for are called entry controlled loops, and do while is exit controlled. That is why a do while body always runs at least once. And only do while needs a semicolon at the end.

So which loop should you pick? Use for when you know how many times to repeat, like printing ten lines of a table. Use while when you repeat until something happens, like reading numbers until the user types zero. Use do while when the body must run at least once, like showing a menu before asking for a choice.

A nested loop is a loop inside another loop. The outer loop, with r, counts the rows. The inner loop, with c, counts the columns. For each row, the inner loop prints r stars, and then we move to a new line. So row one has one star, row two has two, and row three has three.

Pause and predict. This is the same program, but now we print c instead of a star. What will the pattern look like? The inner loop always starts again from one. So row one prints one, row two prints one two, and row three prints one two three.

Break stops a loop immediately, and the program continues after the loop. This loop is set to go up to ten. But when i double equals four, break runs. So the loop stops before printing four, and the output is one, two, three.

Continue skips only the rest of the current round, and the loop goes on. When i is three, continue jumps straight to the update, i plus plus. So three is never printed. The output is one, two, four, five. Remember, break leaves the loop, but continue only skips one round.

Now, programs that exams ask often. First, the sum of the numbers from one to ten. We start sum at zero, which is very important. Each round adds i to sum. After all ten rounds, the sum is fifty five.

Second, the multiplication table of seven. To keep the screen short, we print only up to three. Each round prints seven, then i, then seven into i. You get seven ones are seven, seven twos are fourteen, and seven threes are twenty one. Change three to ten for the full table.

Third, reverse the digits of a number, using while, because we do not know how many digits there are. N percent ten gives the last digit. We add it to rev times ten, which shifts the old digits left. Then n divided by ten removes the last digit. When n becomes zero, the loop stops, and rev is four three two one.

Fourth, the factorial of five. Factorial means multiplying all numbers from one up to that number. Here fact must start at one, not zero, because anything multiplied by zero stays zero. One into two into three into four into five gives one hundred twenty.

Let us revise what we learned today. While and for check the condition first, but do while always runs at least once. A for loop has a start, a condition and an update. In nested loops, the outer loop handles rows and the inner loop handles columns. Break leaves the loop, while continue skips just one round. And start a sum at zero, but a product at one.

Courses that teach this

CourseUnit
CBSE Class 9 Information Technology (402)Digital Documentation
GSEB Std 10 Computer StudiesProgramming Fundamentals & C Language

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 →