Month End Sale: Get Extra 10% OFF on Job-oriented Training! Offer Ending in
D
H
M
S
Get Now
Understanding for loop in C

Understanding for loop in C

18 Jun 2024
Beginner
598 Views
10 min read
Learn via Video Course & by Doing Hands-on Labs

Free C Programming Online Course With Certificate

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

Q1. What is a for loop in C?

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

Q2. What is loop () used for?

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

Q3. What is type of loop in C?

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


Share Article

Live Classes Schedule

Our learn-by-building-project method enables you to build practical/coding experience that sticks. 95% of our learners say they have confidence and remember more when they learn by building real world projects.
Software Architecture and Design Training Jul 28 SAT, SUN
Filling Fast
05:30PM to 07:30PM (IST)
Get Details
.NET Solution Architect Certification Training Jul 28 SAT, SUN
Filling Fast
05:30PM to 07:30PM (IST)
Get Details
Azure Developer Certification Training Jul 28 SAT, SUN
Filling Fast
10:00AM to 12:00PM (IST)
Get Details
Advanced Full-Stack .NET Developer Certification Training Jul 28 SAT, SUN
Filling Fast
07:00AM to 09:00AM (IST)
Get Details
ASP.NET Core Certification Training Jul 28 SAT, SUN
Filling Fast
07:00AM to 09:00AM (IST)
Get Details
Data Structures and Algorithms Training with C# Jul 28 SAT, SUN
Filling Fast
08:30PM to 10:30PM (IST)
Get Details
Microsoft Azure Cloud Architect Aug 11 SAT, SUN
Filling Fast
03:00PM to 05:00PM (IST)
Get Details
Angular Certification Course Aug 11 SAT, SUN
Filling Fast
09:30AM to 11:30AM (IST)
Get Details
ASP.NET Core Project Aug 24 SAT, SUN
Filling Fast
07:00AM to 09:00AM (IST)
Get Details

Can't find convenient schedule? Let us know

About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 8th time in a row (2016-2023). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Accept cookies & close this