For loop in C Language
Introduction to For Loop in C Programming Language
C Programming language is a widely used and popular programming language in computer science. It provides many control structures that make it easier to write efficient and concise programs. The For Loop is one such control structure that allows you to execute a block of code repeatedly based on a specific condition. In this blog, we will explore the For Loop in C Programming Language, along with its syntax, working, and examples.
Syntax of For Loop in C Programming Language
The syntax of the For Loop in C Programming Language is as follows:
for (initialization; condition; increment/decrement) {
// Code block to be executed repeatedly
}
Here, the initialization step is executed only once before the loop starts. It initializes the loop variable to a specific value. The condition is checked before each iteration of the loop. If the condition is true, the code block inside the loop is executed; otherwise, the loop terminates. The increment/decrement step is executed after each iteration of the loop. It updates the loop variable to a new value.
Working of For Loop in C Programming Language
The For Loop works by repeating a block of code based on a specific condition. The loop variable is initialized to a specific value in the initialization step. The condition is checked before each iteration of the loop. If the condition is true, the code block inside the loop is executed; otherwise, the loop terminates. The loop variable is updated to a new value in the increment/decrement step after each iteration of the loop.
Examples of For Loop in C Programming Language
Let's look at some examples to understand the For Loop in C Programming Language better:
Example 1: Printing Numbers from 1 to 10
#include <stdio.h>int main() {
int i;
for (i = 1; i <= 10; i++) {
printf("%d ", i);
}
return 0;
}
Output:
1 2 3 4 5 6 7 8 9 10
In this example, the For Loop is used to print the numbers from 1 to 10. The loop variable i is initialized to 1, and the condition checks whether i is less than or equal to 10. The code block inside the loop prints the value of i, and the increment step updates i to the next value.
Example 2: Calculating the Sum of Numbers from 1 to n
#include <stdio.h>
int main() {
int i, n, sum = 0;
printf("Enter the value of n: ");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
sum += i;
}
printf("The sum of numbers from 1 to %d is %d", n, sum);
return 0;
}
Output:
Enter the value of n: 5
The sum of numbers from 1 to 5 is 15
In this example, the For Loop is used to calculate the sum of numbers from 1 to n, where n is a user input. The loop variable i is initialized to 1, and the condition checks whether i is less than or equal to n. The code block inside the loop adds the value of i to the sum variable, and the increment step updates i to the next value.
Conclusion
The For Loop is a powerful control structure in C Programming Language that allows you to execute a block of code repeatedly based on a specific condition. It provides an efficient way to write concise programs that perform repetitive tasks. In this blog, we have discussed the syntax, working
Share Your Feedback Here !!