In C programming, loops are essential for performing repetitive tasks efficiently. Among the most common types are the for loop and the while loop. This tutorial will explain the key differences between the for and while loops in C, including syntax, use cases, and practical examples to help beginners understand which loop to use and when.
For Loop in C
The for loop in C is a control flow statement used to repeat a block of code a specific number of times. It is best suited when the number of iterations is known in advance.
Syntax of for Loop
for(initialization; condition; increment/decrement) { // Code to execute in each loop iteration }
Example:
#include <stdio.h> int main() { // Using a for loop to print "C Certification" for(int i = 1; i <= 4; i++) { printf("C Certification\n"); } return 0; }
Output:
C Certification C Certification C Certification C Certification
This C program prints “C Certification” four times using a for loop. It starts with i = 1 and runs the loop while i <= 4. Each time, it prints the message and adds a new line. After printing, it increases i by 1. When i becomes 5, the loop stops, and the program ends. This shows how to repeat a task using a loop in C.
While Loop in C
The while loop in C is used to repeatedly execute a block of code as long as a specified condition is true. It is known as a pre-test loop because the condition is checked before each iteration. If the condition is initially false, the loop body will not execute at all.
Syntax of while Loop
while(condition) { // Code to execute }
Example:
#include <stdio.h> int main() { int i = 1; // Initialization while (i <= 5) { // Condition printf("C Programming Quiz\n"); i++; // Increment } return 0; }
Output:
C Programming Quiz C Programming Quiz C Programming Quiz C Programming Quiz C Programming Quiz
This C program uses a while loop to print “C Programming Quiz” five times. It starts with i = 1 and runs the loop as long as i <= 5. In each loop, it prints the message and increases i by 1. Once i becomes 6, the loop stops. The program ends by returning 0. It’s a clear and simple example of using loops for repetition in C.
Difference between For and While Loop in C
Here’s the comparison between the for and while loops in C programming:
Feature | for Loop | while Loop |
---|---|---|
Use Case | When the number of iterations is known | When the number of iterations is unknown |
Syntax | for (init; condition; update) | while (condition) |
Initialization | Inside the loop header | Outside the loop |
Condition Check | At the beginning of each iteration | At the beginning of each iteration |
Update | Inside the loop header | Typically inside the loop body |
Flexibility | Less flexible; all parts in one line | More flexible for complex conditions |
Readability | More compact; good for counting loops | Clear for condition-based repetition |
Example | for (int i = 0; i < 5; i++) | int i = 0; while (i < 5) |
Frequently Asked Questions (FAQs)
1. What is the main difference between for and while loop in C?
The for loop is used when the number of iterations is known in advance. The while loop is used when the condition depends on a runtime scenario and is checked before every iteration.
2. Which is faster: for or while loop in C?
Both loops offer similar performance in most cases. The choice depends more on readability and clarity than on speed.
3. Can a for loop be replaced with a while loop in C?
Yes, any for loop can be rewritten using a while loop and vice versa. However, for loops are more compact for counting iterations.
Sanfoundry Global Education & Learning Series – 1000 C Tutorials.
- Check C Books
- Watch Advanced C Programming Videos
- Practice Computer Science MCQs
- Apply for C Internship
- Practice BCA MCQs