Year End Sale: Get Upto 40% OFF on Live Training! Offer Ending in
D
H
M
S
Get Now
Understanding for loop in C

Understanding for loop in C

18 Jun 2024
Beginner
1.42K Views
10 min read
Learn with an interactive course and practical hands-on labs

C Programming Free Course with Certificate: Learn C In 21 Days

for Loop in C: An Overview

The for loop in C provides functionality. It is nothing but a repetition control structure that allows us to efficiently write a loop that needs to be executed a specific number of times. It contains the initialization, condition, and updating statements as part of its syntax. The for loop is used to traverse arrays, vectors, and other data structures.

In this C Tutorial, we will explore more about for Loop which will include for loop in C, what is for loop in C, for loop in C programming example, and the syntax of for loop in C.  First, we will see the Type of loops in C.

Read More - Top 50 C Interview Questions and Answers

Types of Loop in C

Let’s get into the three types of loops used in C programming.
  1. For Loop
  2. While Loop
  3. Do While Loop

But right now, we are going to focus only on Loop, So let's discuss it.

What is a for Loop?

  • A for loop is a control structure that enables a set of instructions to get executed for a specified number of iterations.
  • It is an entry-controlled loop.
  • In the for loop, the initialization statement is executed only once.
  • After that, the test expression is evaluated.
  • If the test expression is evaluated as false, the for loop is terminated.
  • However, if the test expression is evaluated as true. The statements inside the body of the for loop are executed, and the update expression is updated.

Flowchart

For Loop in C

Syntax

for(initialization; test condition; update expression){
//code to be executed
}
  • Here, the initialization statement is executed first and only once.
  • The test condition is checked, if false the loop terminates
  • If the test condition is true, the body of the loop executes
  • The update expression gets updated
  • Again the test condition is evaluated
  • The process repeats until the test condition becomes false.

Example: For loop in C Compiler

// Program to print numbers from 1 to 10
#include <stdio.h> 

int main() {
 int i;
 for (i = 0; i < 10; i++)
 {
 printf("%d\n", i+1);
 }
 return 0;
}

Output

1
2
3
4
5
6
7
8
9
10

Explanation

The above code prints the numbers from 1 to 10 using a for loop in C.

  • We know it will take 10 iterations to print 10 numbers so, we have used the for loop.
  • i is initialized to 0.
  • The condition i<10 will be checked. It is true, therefore i+1 i.e. 1 gets printed.
  • Then i increments to 1 again the condition, i<10 is evaluated.
  • The process will repeat until i become 10.

Infinite for loop:

  • When the loop never stops and keeps running forever, it is called an infinite loop.
  • In short, in the for loop, if we don't specify the test condition (check_condition), it's assumed to be true by default, As a result, the condition never becomes false.
  • And the loop will keep running forever until we do force-stop the program.
  • To avoid all this Don't forget to specify a condition.
  • There are two possibilities in the infinite for loop When the given condition is not mentioned and when the given condition is always true. Let's see one by one.

1. When the condition is not mentioned:

Example


#include<stdio.h>

int main()
{
    
    for(int i = 0; ; i++) // When condition is not mentioned
    {
        printf("%d ",i);
    }
    
    return 0;
}

Explanation:

When we initialize the counter variable i to 10. And also "i" increases by 1 after every iteration.
Observe how the test condition is i > 0.
Will the value of i be always greater than 0?

2. When the condition is always true:

Example

#include<stdio.h> 

int main()
{
    
    for(int i = 10; i > 0 ; i++) //When test condition is always TRUE
    {
        printf("%d ",i);
    }
    
    return 0;
}

Explanation:

In the above code, the counter variable i is initialized to 0.But it decreases by 1 with every single iteration, As a result,"I" always has less than 10. So the condition i < 10 is always true, and you'll get an infinite loop. To avoid this kind of infinite loop, make sure that you define the looping condition correctly in the code.
Conclusion:
So we explored for loop in c, what is for loop in c, for loop in c programming example, and the syntax of for loop in c.The For Loop in C has a broad range of applications, from loop-driven algorithms to iterative problem-solving. Take your proficiency in C programming to the next level by pursuing a C Programming Course, which will validate your expertise in loops and other crucial concepts.

FAQs

 It is used to iterate the statements or a part of the program several times.

It is used to execute a group of instructions or a block of code multiple times, without writing it repeatedly.

The C language uses the following types of loops: 
While loop
Nested loop
For loop.
Do-While loop


Share Article
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at ScholarHat)

Shailendra Chauhan, Founder and CEO of ScholarHat by DotNetTricks, is a renowned expert in System Design, Software Architecture, Azure Cloud, .NET, Angular, React, Node.js, Microservices, DevOps, and Cross-Platform Mobile App Development. His skill set extends into emerging fields like Data Science, Python, Azure AI/ML, and Generative AI, making him a well-rounded expert who bridges traditional development frameworks with cutting-edge advancements. Recognized as a Microsoft Most Valuable Professional (MVP) for an impressive 9 consecutive years (2016–2024), he has consistently demonstrated excellence in delivering impactful solutions and inspiring learners.

Shailendra’s unique, hands-on training programs and bestselling books have empowered thousands of professionals to excel in their careers and crack tough interviews. A visionary leader, he continues to revolutionize technology education with his innovative approach.
Accept cookies & close this