For Loop in C Programming: Syntax, Flowchart and Examples
For Loop in C Programming
The for loop in C programming is used to execute a block of code repeatedly for a known number of times until a specified condition is met. It is a fundamental control structure commonly used for iteration in C programs.
A for loop is especially useful when you need to repeat a task a specific number of times. It helps in writing efficient, clean and organized code.
Below are some common use cases of the for loop in C:
1.Generating number series (e.g., even numbers, Fibonacci series, etc.)
2.Traversing arrays, linked lists or other data structures
3.Looping through student records to calculate grades
4.Implementing repetitive tasks in game development
5.Searching for a value in an array
6.Sorting, searching, and traversing array elements
7.Generating CAPTCHA codes for user verification
8.Validating password attempts within limited tries
9.Calculating the total number of products and price in an online shopping cart
Using the for loop efficiently can improve both performance and readability of your C programs.
For Loop Syntax:
for (initialization; test_condition; update_expression)
{
/* statements or body of the loop */
}
Explanation:
Initialization: The initialization step is used to declare and initialize variables in the for loop. These variables have local scope and are accessible only within the loop body.
This step is executed only once, before the loop starts. Multiple variables can be declared and initialized in the initialization section.
It defines the starting point of the loop.
Test Condition (Expression): This condition determines whether the loop should continue executing.
It is a boolean expression that evaluates to either true or false. If true, the loop body is executed. If false, the loop terminates, and control passes to the next statement after the loop.
The body of the loop continues to execute as long as the test condition is true.
Update Expression: This is the final part of the loop definition. It is executed after each iteration and typically updates the loop control variable.
The update expression helps control the number of iterations. If not handled correctly, the loop may become infinite.
If the initialization, test condition, and update expression are all omitted, the loop becomes an infinite loop.
For example, the syntax of an infinite for loop is:
Infinite For Loop Syntax:
for ( ; ; )
{
/* statements or body of the loop */
}
The above loop will run infinitely because no initialization, test condition, or update expression is specified.
Understanding For Loop Control Flow in C Programming
Let’s learn how the for loop works in the C programming language.
The diagram illustrates the step-by-step control flow of a for loop in C.
Control Flow of a For Loop in C:
The operation of a for loop in C follows this structured sequence:
1. Initialization (Declaration): This step executes only once, before the loop starts. In this section, the loop control variable is declared and initialized (e.g., int i = 0;). These variables have local scope within the loop.
2. Test Expression (Condition Check): The test condition is evaluated. If it returns true, the loop continues to the next step. If it returns false, the loop terminates, and control moves to the statement following the loop.
3. Loop Body Execution: If the test condition is satisfied, the statements inside the loop body are executed.
4. Update Expression: After the loop body executes, the control variable is updated (e.g., i++;). This step is crucial for progressing toward the loop termination condition.
5. Repeat: Control returns to step 2 (test expression). This cycle continues until the test condition evaluates to false.
Key Point: If the initialization, test condition, or update expression is omitted, the loop may become an infinite loop. For example:
for( ; ; ) {
// Infinite loop body
}
This syntax creates an infinite for loop because no condition ever stops it.
C Program to Demonstrate the For Loop
/* This C program prints the numbers from 1 to 10 using a for loop */
#include <stdio.h>
int main() {
int i;
printf("\nFor loop to display numbers from 1 to 10\n");
for(i = 1; i <= 10; i++) {
printf(" %d", i);
}
return 0;
}
Output:
For loop to display numbers from 1 to 10
1 2 3 4 5 6 7 8 9 10
Explanation:
The variable i is declared as an integer and initialized to 1.
The program first prints the message: For loop to display numbers from 1 to 10.
Then the for loop begins:
- The loop starts by setting i = 1.
- It checks if i <= 10. Since 1 is less than or equal to 10, the loop body executes, printing the current value of i.
- After printing, i increments by 1 (i++).
- The condition is checked again with the updated value of i.
- This process repeats, printing numbers from 1 through 10.
- When i becomes 11, the condition i <= 10 fails, and the loop stops.
The program then ends, having displayed the numbers 1 to 10 on the screen.
C Program to Illustrate Use of for(;;) Loop
/* C program below displays a series of numbers from 1 to 10 using a for(;;) loop */
#include <stdio.h>
int main()
{
int i = 1;
for(;;)
{
printf(" %d", i);
i++;
if(i > 10)
break;
}
return 0;
}
Output: 1 2 3 4 5 6 7 8 9 10
Explanation:
In the above C program, the for(;;) loop is used — which is a type of infinite loop.
We first declare and initialize i = 1.
Now let’s go step-by-step through the loop execution:
- The control enters the for(;;) loop. Since there is no initialization, condition, or update inside the loop header, the loop doesn't check any condition and directly enters the body.
- The statement printf(" %d", i); is executed, which prints the value of i — in the first iteration, it's 1.
- Then, i++; increments the value of i by 1. Now, i becomes 2.
- The if(i > 10) condition is checked. Since i = 2, the condition is false, so the break statement is skipped.
- Control goes back to the top of the loop. Again, since the loop header doesn’t have any test condition, it continues and enters the loop body again.
- This process repeats: i is printed, incremented, and then checked against the condition if(i > 10).
- When i becomes 11, the condition if(i > 10) becomes true. So, the break statement executes, which stops the loop.
- The control comes out of the loop, and since there are no more statements in the program, it ends.
So even though for(;;) is an infinite loop, we’ve controlled its execution using the if and break statements.