for Loop in C
What is "for loop in C"
A for
loop is a control flow statement that allows you to repeatedly execute a block of code a certain number of times.
The basic structure of a for
loop in C is as follows:
for (initialization; condition; increment) {
// code to be executed
}
The initialization statement is executed only once, before the loop starts. It is typically used to initialize a loop counter variable.
The condition is evaluated before each iteration of the loop, and if it evaluates to true, the code within the loop is executed. If the condition is false, the loop is terminated.
The increment statement is executed after each iteration of the loop and is typically used to update the loop counter variable.
Where can we use "for Loop in C"
Here are some of the places where for
loop can be use:
Iterating over a range of numbers: For loops can be used to repeatedly execute a block of code a specific number of times, such as counting from 1 to 10, or counting down from 10 to 1.
Iterating over arrays and other data structures: For loops can be used to iterate over elements of an array or other data structure, such as a list or a string, making it easy to perform operations on all elements of the data structure.
Performing operations on sequences of data: For loops can be used to perform operations on sequences of data such as generating sequences of numbers, working with dates and times, or iterating over a set of data.
Implementing algorithms: For loops can be used as a fundamental building block for implementing complex algorithms, such as sorting algorithms and search algorithms.
Creating repetitive patterns: For loops can be used to create repetitive patterns, such as drawing shapes or generating music, by executing a block of code multiple times with different parameters.
Examples of using "for loop" in C
Example 1
The following code uses a for
loop to print the numbers from 1 to 10:
#include <stdio.h>
int main()
{
int i;
for (i = 1; i <= 10; i++)
{
printf("%d ", i);
}
return 0;
}
Output:
Explanation:
In this example, the for loop is set up with the following parts:
The initialization statement
int i = 1;
initializes the loop variablei
to the value 1. This statement is executed only once, before the loop starts.The condition
i <= 10;
is a test that is performed before each iteration of the loop. If the condition evaluates to true, the loop continues, otherwise the loop ends. In this case, the loop will continue as long asi
is less than or equal to 10.The increment statement
i++
increases the value ofi
by 1 after each iteration of the loop.The code inside the curly braces
{}
is the body of the loop, and it is executed once for each iteration of the loop. In this case, the body of the loop consists of a single statement that usesprintf
to print the current value ofi
followed by a space.
Output:
1 2 3 4 5 6 7 8 9 10
The output will be the numbers from 1 to 10 separated by a space.
Example 2
Here is another example of using a for
loop to find the sum of the numbers from 1 to 100:
#include <stdio.h>
int main()
{
int sum = 0;
for (int i = 1; i <= 100; i++)
{
sum += i;
}
printf("Sum of numbers from 1 to 100 is: %d", sum);
return 0;
}
Output:
Explanation:
In this example, the for
loop is set up with the following parts:
The initialization statement
int i = 1;
initializes the loop variablei
to the value 1. This statement is executed only once, before the loop starts.The condition
i <= 100;
is a test that is performed before each iteration of the loop. If the condition evaluates to true, the loop continues, otherwise the loop ends. In this case, the loop will continue as long asi
is less than or equal to 100.The increment statement
i++
increases the value ofi
by 1 after each iteration of the loop. The variable sum is defined and initialised to 0, which is used to accumulate the sum of numbers in the loop.The code inside the curly braces {} is the body of the loop, and it is executed once for each iteration of the loop. In this case, the body of the loop consists of a single statement that uses
sum += i
; to add the current value of i to the sum after each iteration.
After the loop, we are using the printf
statement to print the final value of sum
.
Output:
Sum of numbers from 1 to 100 is: 5050
The output will be the sum of all the numbers from 1 to 100, which is 5050.
Example 3
Example of using a for
loop to find the factorial of a given number:
#include <stdio.h>
int main()
{
int num = 5;
int factorial = 1;
for (int i = 1; i <= num; i++)
{
factorial *= i;
}
printf("Factorial of %d is: %d", num, factorial);
return 0;
}
Output:
Explanation:
In this example, the for
loop is set up with the following three parts:
The initialization statement
int i = 1;
initializes the loop variablei
to the value 1. This statement is executed only once, before the loop starts.The condition
i <= num;
is a test that is performed before each iteration of the loop. If the condition evaluates to true, the loop continues, otherwise the loop ends. In this case, the loop will continue as long as i is less than or equal to the number entered by the user.The increment statement
i++
increases the value ofi
by 1 after each iteration of the loop.In this example, before the loop, we are using the scanf statement to get the input from the user, which is the number for which we are going to find the factorial. The variable factorial is defined and initialised to 1, which is used to store the factorial value.
The code inside the curly braces {} is the body of the loop, and it is executed once for each iteration of the loop. In this case, the body of the loop consists of a single statement that uses factorial *= i; to multiply the current value of i to the factorial after each iteration.
After the loop, we are using the printf
statement to print the final value of factorial.
Output:
Enter a number: 5
Factorial of 5 is: 120
If the user enters 5, the output will be the factorial of 5, which is 120.
Overall, for
for Loop in C provide a convenient way to perform repetitive tasks, and are very important to keep your code or application logic smaller, organized, easy to write and easy to debug.