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
What will be the output?
for(int i = 0; i < 3; i++) {
printf("%d ", i);
}