What if you could tell your program to keep doing something until you say stop, without writing the same code again and again?
Why While loop in C? - Purpose & Use Cases
Imagine you want to count from 1 to 10 by writing each number one by one in your program. You type 10 separate print statements to show each number.
This manual way is slow and boring. If you want to count to 100 or 1000, writing hundreds or thousands of lines is tiring and easy to make mistakes. Changing the count range means rewriting many lines.
The while loop lets you repeat a block of code as long as a condition is true. You write the counting code once, and the loop runs it again and again automatically until the count ends.
printf("1\n"); printf("2\n"); printf("3\n");
int i = 1; while (i <= 3) { printf("%d\n", i); i++; }
With while loops, you can easily repeat tasks many times without extra code, making programs shorter, clearer, and easier to change.
Think about a game where you want to keep asking the player to guess a number until they get it right. A while loop keeps asking until the correct answer is given.
Manual repetition is slow and error-prone.
While loops automate repeated actions based on a condition.
This makes code simpler, shorter, and flexible.