While Loop

Loop executes the same block of code again and again until the given condition is true. We use while loop when we do not know how many numbers of iterations will be there.

This will print name 10 times.

Program to print numbers from 1 to 5

Note: If we write true in condition, this while loop will become infinite. You can use ctrl + c on your keyboard to stop an infinite loop.

do-while Loop

While and do while loops are almost the same. The only difference is that in do while loop, first iteration is done without checking condition. After first iteration, it will check condition like while loop.

Practice Exercises

Complete these exercises to reinforce your learning and earn XP

Sign in to track your progress and earn XP!
Exercise 1 of 2Easy

What will be the output?

15 XP~4 min
for(int i = 0; i < 3; i++) {
    printf("%d ", i);
}
Exercise 2 of 2Easy

To exit a loop immediately, we use the ___ statement.

10 XP~2 min