C Program for Loop is used to repeat a particular block statement a number of times. In the C program, we have two main loops namely for and while loops, which repeat a particular number of times until a condition is satisfied.Â
In this article, we are about to learn more about the C program for loop with examples. The for loop matches different conditions before running a program.
What is For Loop In C Program?
The For Loop in the C program is used to iterate over a particular block of statements a number of times until the given condition matches. Data is traversed a particular number of times. The three main conditions in the C program for loop are condition matching, statement, and increment and decrement operators.
We will understand the C program for loop using some example statements, and we have to verify the condition before running the loop and executing the program. The for loop and while loop are the most widely used loops in all programming languages. In this tutorial, we will understand C program for loop syntax, working using different conditions and examples of C program.
Also, Check The For Loop Program in Java Language
C Program For Loop SyntaxÂ
Let us get an overview of the syntax for C for loop below.
for (initialize ; condition_checking ; updation)
{ //Code Execution } |
The three condition matching in the C program for the loop is used to match before the loop runs.
- Initialize: The loop control variable in the C program for loop starts with an initialization statement.
- Condition_checking: This statement is used to match the condition and only run the loop for a certain point of time until the condition evaluates to true and then the body of the loop is executed.Â
- Updation: The updation statement consists of increment and decrement operators, which are used to update the value of variables in the loop after the execution of the body program.Â
Also, Check Operators in Java with Examples
Control Flow & Working of C Program For LoopÂ
The control flow of the C program for loop is simple to execute and consists of three main conditions for execution. Let us check step by step how to execute the workflow of the C program for loop.
- Initialize the counter variable with a value before execution. The beginning of the loop starts with an initial value before starting.
- Now verify if the condition of the for loop is true only then can you run the expression. If the condition fails you have to go back again to the initializing condition.Â
- If the statement condition comes to be true you have to execute the code statement in the block.Â
- Update the counter after execution; it can be an increment and decrement operator.
- Move to condition checking to execute the code statement once again until the condition is evaluated as true.
C Program For Loop ExampleÂ
Let us understand the C program for loop along with an example to print the code statement again and again until the condition is satisfied. Let us print even numbers from 1 to 10.
#include <stdio.h>
int main() {     printf(“Even numbers from 1 to 10:\n”);     for (int i = 1; i <= 10; i++) {         if (i % 2 == 0) { // Check if number is even             printf(“%d “, i);         }     }     return 0; } |
In this program, we are running a For loop from 1 to 10 and also checking whether the number is divisible by 2 using the modulus operator. If the condition satisfies we print the number i,e. Even numbers.Â
- The i variable is used to keep track of the value which is initialized from 1.
- The test condition here is whether the number (i<=10), if yes, the counter prints the number and executes the if statement ahead.
- The next condition checks for whether the number is even or not using the modulus operator.
- In every iteration the variable i increases by 1.
- As the value of i goes beyond 10, the loop breaks and we get the even numbers on the screen as output.
Infinite C Program For Loop Using No Condition
The infinite condition in the C program loop can be executed using the no condition in the loop condition checking. If in the for loop construct there are no test conditions the loop is assumed to run for infinite time as it is true by default.Â
Due to this reason, the loop never becomes false and it keeps running forever until you apply an external condition to stop the program. Let us check a code example for a C program for loop under infinite conditions.
#include <stdio.h>
int main() {     for (;;) { // No condition, infinite loop         printf(“This is an infinite loop!\n”);     }     return 0; // This line will never be reached } |
The above loop in the example keeps running forever until given any external condition to stop the for loop.
Learn DSA with C++ Program with PW Skills
Prepare for interviews with C programming and DSA with the Decode DSA With C++ Course. Master C++ programming with practice exercises, module assignments, real world projects, and certification exams.Â
Solve real world examples with C++ programs and gain knowledge of programming with C++ with interactive classes and in-depth live sessions with dedicated mentors at PW Skills.Â
C Program For Loop FAQs
Q1. What is C Program for Loop?
Ans: C Program for Loop is used to repeat a particular block statement for a number of times. In the C program, we have two main loops, namely for and while loops which repeat for a particular number of time until a condition is satisfied.
Q2. How to do an iteration in a C program?
Ans: Iteration can be used in C programming using for and while loops which can run until the condition evaluates to true. Every element is traversed in the loop until the condition matches with the counter updating after each traverse.
Q3. Can we use multiple variables in a C program for a loop?
Ans: We can use multiple variables in the C program for loop for checking conditions and other criteria based on certain statements.
Q4. How to make the C program for loops infinite?
Ans: We can make the C program for loop infinite by eliminating the condition checking in the program. This will evaluate the true condition for every iteration which makes the loop run for infinite time.