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 the number of executions.

This will print name 5 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.

This code will execute one time, no matter the condition is false. But after first iteration, it will check the condition. If the condition is true, loop will run. If the condition is false, loop will be terminated and the controll will move out of the 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