For Loop vs While Loop in C



Loops are one of the most important concepts in programming. They allow developers to execute a block of code multiple times without having to rewrite the same codes. Among the commonly used loops, the for loop and the while loop are the most widely used. Both help in iteration, but they differ in syntax, control, and their specific use-cases.

Read this chapter to learn how the for loop is different from the while loop. We will use real-world examples to show when you should choose a for loop over a while loop and vice versa.

What is "for" Loop?

The for loop is used when the number of iterations is already known in advance. It has three parts in its syntax: initialization, condition, and increment/decrement.

for(initialization; condition; update) {
   // Code to execute
}

Example: Print Number from 1 to 5 using For Loop

In this example, we demonstrate the use of a for loop and how we can use it in C programming to print number from 1 to 5 −

#include <stdio.h>

int main() {
   for(int i = 1; i <= 5; i++) {
      printf("%d\n", i);
   }
return 0;
}

What is "while" Loop?

A while loop is useful when the number of iteration is not known in advance. The loop continues until the given condition becomes false. Its syntax is as follows −

while(condition){
   // code to execute
}

Example: Print Number from 1 to 5 using While Loop

The following example demonstrates how to use a while loop in C programming to print the numbers from 1 to 5 −

#include <stdio.h>

int main() {
   int i = 1;
   while(i <= 5) {
      printf("%d\n", i);
      i++;
   }
   return 0;
}

When to Use a "for" Loop?

Programmers use for loops in tasks such as performing a fixed set of operations where the start and end conditions are clearly defined. Here, we have highlighted some scenarios where a for loop can be applied −

  • Iterating over arrays, strings, or lists
  • When the code executes a fix number of times
  • In simple counting problems

In addition, we can use nested for loops in sorting algorithms.

Example: Sum of First 5 Numbers

In this example, we use a for loop to calculate the total of numbers up to 5. If the number exceeds 5, the code will stop and display the output.

#include <stdio.h>

int main() {
   int sum = 0;
   for(int i = 1; i <= 5; i++) {
      sum += i;
   }
   printf("Sum = %d", sum);
}

Output

The following is the sum of all the numbers up to 5 −

Sum = 15

When to Use a "while" Loop?

Programmers use while loops in situations where the execution depends on user input, external conditions, or events, and continues until a specific condition is met.

Here are some specific scenarios where you can use a while loop -

  • When the termination condition depends on the user input
  • When looping until a certain event occurs
  • Reading files or streams until the end of function
  • Waiting for specific state in programs

Example: Take Input Until User Enters 0

In this example, we use a while loop to display the user input until the user enters 0.

#include <stdio.h>

int main() {
   int num;
   printf("Enter numbers (0 to stop):\n");

   scanf("%d", &num);
   while (num != 0) {
      printf("You entered: %d\n", num);
      scanf("%d", &num);
   }

   return 0;
}

Output

The program will terminate its execution when you enter "0". Here is the output of the code −

Enter numbers (0 to stop):
1
You entered: 1
2
You entered: 2
0
=== Code Execution Successful ===

Difference between "for" Loop and "while" Loop

The following table compares and contrasts the important features of for and while loops −

Feature for Loop while Loop
Initialization Declared within the loop structure and executed once at the beginning. Declared outside the loop and must be done explicitly before the loop starts.
Condition Checked before each iteration. Checked before each iteration.
Update Executed automatically after each iteration. Must be executed inside the loop and handled explicitly.
Use Cases Useful when the number of iterations is known or when looping over a range. Useful when the number of iterations is unknown or depends on conditions
Initialization and Update Scope Limited to the loop body. Scope extends beyond the loop and must be handle explicitly.
Advertisements