Introduction
In the previous chapter, we learned about the basics of loops in C. In this chapter, we will focus on the for
loop, one of the most commonly used control flow structures in C. The for
loop is particularly useful when you know in advance how many times you need to execute a block of code.
What is a for Loop?
A for
loop is used to repeat a block of code a specific number of times. It consists of three main parts:
- Initialization: Sets the initial value of the loop counter.
- Condition: Tests the loop counter; if true, the loop continues; if false, the loop exits.
- Update: Updates the loop counter after each iteration.
Syntax
The basic syntax of a for
loop in C is as follows:
for (initialization; condition; update) {
// Code to be executed repeatedly
}
initialization
: This statement is executed once at the beginning of the loop. It is typically used to initialize the loop counter.condition
: This expression is evaluated before each iteration of the loop. If it evaluates to true, the loop body is executed. If it evaluates to false, the loop terminates.update
: This statement is executed after each iteration of the loop. It is typically used to update the loop counter.
Example: Simple for Loop
Let’s look at a simple example to understand how the for
loop works.
Example: Printing Numbers from 1 to 5
#include <stdio.h>
int main() {
for (int i = 1; i <= 5; i++) { // Initialization: int i = 1; Condition: i <= 5; Update: i++
printf("%d\n", i); // Printing the value of i
}
return 0; // Returning 0 to indicate successful execution
}
Output:
1
2
3
4
5
Detailed Steps
- Initialization: The loop counter
i
is initialized to 1. - Condition: The condition
i <= 5
is checked. Since it is true, the loop body is executed. - Execution: The
printf
statement prints the value ofi
. - Update: The loop counter
i
is incremented by 1. - Repeat: Steps 2-4 are repeated until the condition
i <= 5
becomes false.
Nested for Loops
You can use nested for
loops to handle more complex scenarios, such as iterating over a 2D array or performing matrix operations.
Example: Nested For Loop
#include <stdio.h>
int main() {
for (int i = 1; i <= 3; i++) { // Outer loop
for (int j = 1; j <= 2; j++) { // Inner loop
printf("i = %d, j = %d\n", i, j); // Printing the values of i and j
}
}
return 0; // Returning 0 to indicate successful execution
}
Output:
i = 1, j = 1
i = 1, j = 2
i = 2, j = 1
i = 2, j = 2
i = 3, j = 1
i = 3, j = 2
for Loop with Multiple Variables
You can also use multiple variables in the initialization and update statements of a for
loop.
Example: Multiple Variables in For Loop
#include <stdio.h>
int main() {
for (int i = 1, j = 5; i <= 5 && j >= 1; i++, j--) { // Using multiple variables
printf("i = %d, j = %d\n", i, j); // Printing the values of i and j
}
return 0; // Returning 0 to indicate successful execution
}
Output:
i = 1, j = 5
i = 2, j = 4
i = 3, j = 3
i = 4, j = 2
i = 5, j = 1
Infinite for Loop
A for
loop can run indefinitely if the condition always evaluates to true. This is known as an infinite loop.
Example: Infinite For Loop
#include <stdio.h>
int main() {
for (;;) { // Infinite loop
printf("This is an infinite loop.\n");
break; // To break the loop, you can use a break statement
}
return 0; // Returning 0 to indicate successful execution
}
Output:
This is an infinite loop.
Using break and continue in for Loops
Break Statement
The break
statement is used to exit the loop prematurely, regardless of the condition.
Example: Using break in For Loop
#include <stdio.h>
int main() {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
break; // Exit the loop when i is 3
}
printf("%d\n", i);
}
return 0; // Returning 0 to indicate successful execution
}
Output:
1
2
Continue Statement
The continue
statement skips the current iteration of the loop and proceeds to the next iteration.
Example: Using continue in For Loop
#include <stdio.h>
int main() {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue; // Skip the iteration when i is 3
}
printf("%d\n", i);
}
return 0; // Returning 0 to indicate successful execution
}
Output:
1
2
4
5
Simple C Programs to Demonstrate for Loop
Program 1: Sum of First N Natural Numbers
Example:
#include <stdio.h>
int main() {
int n, sum = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
sum += i; // Adding i to sum
}
printf("Sum of first %d natural numbers is: %d\n", n, sum);
return 0; // Returning 0 to indicate successful execution
}
Output:
Enter a positive integer: 5
Sum of first 5 natural numbers is: 15
Program 2: Factorial of a Number
Example:
#include <stdio.h>
int main() {
int n, factorial = 1;
printf("Enter a positive integer: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
factorial *= i; // Multiplying i to factorial
}
printf("Factorial of %d is: %d\n", n, factorial);
return 0; // Returning 0 to indicate successful execution
}
Output:
Enter a positive integer: 5
Factorial of 5 is: 120
Program 3: Multiplication Table
Example:
#include <stdio.h>
int main() {
int n;
printf("Enter an integer: ");
scanf("%d", &n);
for (int i = 1; i <= 10; i++) {
printf("%d x %d = %d\n", n, i, n * i); // Printing multiplication table
}
return 0; // Returning 0 to indicate successful execution
}
Output:
Enter an integer: 5
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
Program 4: Printing an Array
Example:
#include <stdio.h>
int main() {
int arr[] = {10, 20, 30, 40, 50};
int n = sizeof(arr) / sizeof(arr[0]);
for (int i = 0; i < n; i++) {
printf("Element %d: %d\n", i, arr[i]); // Printing each element of the array
}
return 0; // Returning 0 to indicate successful execution
}
Output:
Element 0: 10
Element 1: 20
Element 2: 30
Element 3: 40
Element 4: 50
Conclusion
The for
loop is a powerful and flexible control flow statement in C. It allows you to execute a block of code repeatedly with a specified number of iterations. By understanding and using the for
loop, you can write more efficient and concise code. You can also use nested for
loops, multiple variables, and control statements like break
and continue
to handle more complex scenarios and achieve greater control over your program’s execution flow.