C Loop Statements: for loop

In this article after discussing the basics of C language, Conditional statement and Switch statement we are going to discuss how we can execute a block of code or statement/expression several times using C loop statements.

Furthermore, we will discuss what is a loop statement, its types and which loop is to be used in certain conditions.

Programming languages provide various control structures that allow for more complicated execution paths.

c loop statements

A loop statement allows us to execute a statement or group of statements multiple times. Given below is the general form of a loop statement in most of the programming languages −

What is Loop in C?

  • C loop statements execute the block of code or statement/expression several times until the stated condition is true.
  • C- loop statements have two parts, (i)loop body and (ii) control statement.
  • The control statement is a condition checkpoint that directs the loop body to execute until the specified condition is true.
  • The purpose of the C- loop statements is to repeat the same block of code or statement/expression several times.

Types of C Loop Statements

In many books and online articles, you may have seen that c- loop statements are of 3 types but that’s not correct because these three types are loop constructs but not types of loops

C loop statements can be classified into two ways depending upon the position of the control statement.

1. Entry controlled loop

2. Exit controlled loop

Entry controlled loop

In this loop, the condition is checked before executing the loop body.

Exit controlled loop

In this loop, the condition is checked after executing the body of a loop.

Control Conditions

  • Conditions must be well defined and specified.
  • If not, the loop will execute an infinite number of times.
  • The loop that does not stop executing the statements is called an infinite loop or Endless loop
  • Following are some characteristics of an infinite loop:
    • No termination condition is specified.
    • The specified conditions were never met.
  • The specified condition determines whether to execute the loop body or not.

‘C’ programming language provides us with three types of loop constructs:

1. The for loop

2. The while loop

3. The do-while loop

For loop in C

So let’s discuss what is for loop in C Loop Statements by a real-life example for better understanding.

Suppose you are in a situation where you would have to print your name 1000 times. What would you do? Will you type in the printf command a thousand times or try to copy/paste it?

This task may take an eternity if you will do it manually but using a for loop, this action can be achieved in three statements. This is the most basic example of the for a loop.

So we can say that a for loop enables a particular set of conditions to be executed repeatedly until a condition is satisfied.

for loop flow chart

Syntax of For Loop in C:

for (initializations; condition/expressions; increment/decrement) 
{
  statement1;
  statement2;
  statement3;
  statement4;
  statement n;
}

Explanation

  • The initializations of the for loop is performed only once.
  • The condition/expression is a Boolean expression that tests and compares the counter to a fixed value after each iteration and stops the control from entering into the loop when false is returned.
  • The increment/decrement increases or decreases the counter by a set value(default is 1).

The following program illustrates the for loop in C programming example:

#include<stdio.h>
int main()
{
    int i;
    for(i=1;i<=10;i++)       //for loop to print 1-100 numbers
    {
        printf("%d\n",i);      //to print the number
    }
    return 0;
}

Output:

1
2
3
4
5
6
7
8
9
10

The above program prints the number series from 1-10 using for loop.

loop explanation

Codes Explanation

  1. We have declared a variable of an int data type to store values.
  2. In the initialization, declared variable ‘i’ is being initialized and assigned value 1.
  3. In condition/expression part, we have specified our condition that for how many times the loop will executes to print the desired output.
  4. After condition/expression part, increment part is there which is set to be one.
  5. In the loop body part , we have a printf function to print the numbers on a new line(\n).
  6. Initial value one is stored in i which will get printed in first iteration(loop).
  7. After the first iteration the value will be incremented due to increment function( Click here for operators), and it will become 2 which will become the current value of i variable.
  8. The condition will be rechecked with i<=10 and as the value of i is 2 the condition is true and the loop will be executed, and it will print two on the screen.
  9. Process will be continue till the value 10 is printed on the screen and after that value of i will be incresed to 11 and conditions will be checked again and return false as 11 is neither less nor equa to 10 and loop will be terminated.

In C loop statements, the for loop can have multiple expressions separated by commas in each part.

For example:

for (x = 0, y = num; x &lt; y; i++, y--) 
{
   statements
}
  • We can write the for-loop by skipping the initial value expression, condition and/or increment by adding a semicolon.
  • For example
int i=0;
int max = 10;
for (; i < max; i++) 
{
  printf("%d\n", i);
}

Nested for Loops in C

  • C loop statements can be used in nesting too.
  • Nesting of loops means loop inside a loop.
  • n number of loops can be defined inside another loop.
  • Any type of loop can be defined inside any loop for nesting of loops

Syntax of Nested loop

Outer_loop  
{  
    Inner_loop  
   {  
         // inner loop statements.  
   }  
       // outer loop statements.  
}  

Outer_loop and Inner_loop are the valid loops and can be any of the loop constructs i.e., for loop, while loop or do-while loop.

  • Nested for loops just simply means any loop can be used inside for loops
for (initialization; condition; update)   
{  
    for(initialization; condition; update)  
    {  
           // inner loop statements.  
    }  
    // outer loop statements.  
}  

Example of nested for loop. Here we are writing a table using nested for loop.

#include <stdio.h>  
int main()  
{  
   int n;// variable declaration  
   printf("Enter the value of n :");  

   // Outer Loop Displaying the n tables.  
   for(int i=1;i<=n;i++)    
   {  
       for(int j=1;j<=10;j++)  // inner loop  
       {  
           printf("%d\t",(i*j)); // printing the value of table.  
       }  
       printf("\n");  
   }

  Explanation of the above code

  • First, the ‘i’ variable is initialized to 1 and then program control passes to the i<=n.
  • The program control checks whether the condition ‘i<=n’ is true or not.
  • If the condition is true, then the program control passes to the inner loop.
  • The inner loop will get executed until the condition is true.
  • After the execution of the inner loop, the control moves back to the update of the outer loop, i.e., i++.
  • After incrementing the value of the loop counter, the condition is checked again, i.e., i<=n.
  • If the condition is true, then the inner loop will be executed again.
  • This process will continue until the condition of the outer loop is true.

Summary

  • Define loop in C: A Loop is one of the key concepts on any Programming language.
  • A block of loop control statements in C are executed for n number of times until the condition is true and stops when the condition becomes false.
  • C loop statements are of 2 types: entry-controlled and exit-controlled.
  • C loop statements provides 3 types of loop constructs 1) for loop 2) while and 3) do-while loop.
  • for and while loop C programming are entry-controlled loops and Do-while is an exit control loop in C.

You Might Like:

5 thoughts on “C Loop Statements: for loop”

Leave a Comment

Ads Blocker Image Powered by Code Help Pro
Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Please Reload After Disabling Ad\'s