×

For loop in C Programming

In the C programming language, the for loop is used to repeatedly iterate over a set of instructions or a section of code. It is frequently used to traverse array-based and linked list-based data structures.

Instead of using a while and do-while loop, the for loop provides better results.

Syntax of For Loop

for (initialize Statement; Conditional Function; Increment Expression) 
{


// Statements inside of the loop's body //


}

Working process in for loop

  1. One single initialization assertion is made at a time.
  2. The check expression is then assessed. The for loop is ended if the check expression is interpreted as false.
  3. However, statements inside the for loop's frame are carried out, and the update expression is updated. On the other hand, if the check expression is evaluated as true, statements inside the for loop's frame are carried out, and the update expression is changed.
  4. The check expression is tested once again.

Flowchart of for loop in C

The below Flowchart of For loop representing a way to find the factorial of a number.

For loop in C Programming

Algorithm for the above flow chart:

Step 1: Start.

Step 2: Initialization of the variables.

Step 3: Checking for condition.

Step 4: If the condition returns to be true, go to step 5; otherwise, go to step 7.

Step 5: f = f * i.

Step 6: Go to step 3.

Step 7: Print the factorial value.

Step 8: Stop.

For loop Examples in C

1. Program to Print Numbers from 1 to 10

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


int main() 
{
  // Initializing integer variable //
  int i;


  for (i = 1; i < 11; ++i)
  {
    printf(" %d ", i);
  }
  return 0;
}

OUTPUT

For loop in C Programming

2. C Program: Print table for the given number using C for loop

#include<stdio.h>  
int main()
{  
// Initialize the integer values //    
int i=1, n=0;


printf( " Enter a integer value : ");    
scanf("%d",&n);    
 for(i=1;i<=10;i++)
 {      
 printf(" %d \n ",(n*i));    
 }    
return 0;  
}    

OUTPUT

For loop in C Programming

Characteristics of initialize Statement:

  1. The initialize Statement represents the initialization of the loop variable.
  2. In the initialization Statement, we can initialize a couple of variables.
  3. Initialize Statement is not required.
  4. Initialize Statement's variables cannot be declared in C. In certain compilers, it could be an exception, though.

3. // Program to show that multiple statements can be initialized at once //

#include <stdio.h>  
int main()  
{  
    // initializing integer variables //
    int a, b, c;  
    // multiple integer values are initialized at once //
    for(a=0,b=24,c=23; a<8; a++)  
    {  
        printf( "%d " , a+b+c);  
    }  
    return 0;
}  

OUTPUT

For loop in C Programming

Characteristics of Condition Statement:

  1. A conditional Statement checks to see whether a chosen condition is true. In the absence of true conditions, the loop is ended.
  2. Condition Statements will be subjected to many conditions. The loop will continue iterating until the final condition is met. Alternative circumstances could be treated as statements.
  3. Condition Statement is not necessary.
  4. The Initialization statement and the Updation statement can be replaced with expression 2, which can complete their tasks. That is, in addition to replacing the loop variable in expression 2 itself, we can initialize the variable.
  5. In expression 2, we will disregard zero or non-zero values. In C, however, a non-zero number is true by default, while a zero is false.

4. // Program to show that multiple conditional statements can be given at once //

#include <stdio.h>  
int main()  
{  
    // initializing integer variables //
    int i, j, k;  
    for(i=0, j=0, k=0; i<5, k<7, j<19; i++)  
    {  
        // printing the values //
        printf(" %d %d %d \n " , i,j,k);  
        j+=2;  
        k+=3;  
    }  
    return 0;
}  

OUTPUT

For loop in C Programming

Characteristics of Increment Expression

  1. The loop variable is changed using the Increment Expression.
  2. More than a single variable can be updated at once.
  3. Increment Expression is not compulsory.

5. // Program for Increment expression //

#include<stdio.h>  
void main ()  
{  
    // Declaring the Integer variables //
    int i=0, j=2; 
    
    for(i = 0; i<5; i++, j=j+2)  
    {  
        // Printing the Integer variables //
        printf(" %d %d \n " , i,j);  
    }  
    return 0;
} 

OUTPUT

For loop in C Programming

Advantages of For Loop

  1. It is possible to initialize within or outside of a loop statement.
  2. After the Statement (s) are executed, the increment is carried out.
  3. It is commonly used when the number of iterations is known.
  4. It is used when easy initialization and increment are possible.
  5. The for loop is an entry-controlled loop.
  6. Used to reach the desired outcome is most successful when the variety of iterations is known.

Related Topics

Factorial Program in C Using While Loop

Before looking into the mechanism of factorial program, first, you have to understand about the working of a while loop. While Loop The syntax that has been used for the “while” loop...

4 minutes read.

Format Specifier in C

Format Specifier in C Format specifiers can be described as an operator that is used to print the data referred by any object or variable in combination with the printf ()...

3 minutes read.

Remove an element from an array in C

A collection of objects or pieces of the same data type stored in a single memory block is known as an array. A data structure called an array is used...

3 minutes read.

String literals in C

Constants refer to the fixed values of the program that may not alter during the compiling ad execution. These fixed values are known as literals. In other words, the values...

3 minutes read.

Dynamic memory allocation

Dynamic memory allocation is used to allocate the memory at runtime. It has following function within <stdlib.h> header file. Function Syntax malloc() malloc (number *sizeof(int)); calloc() calloc (number, sizeof(int)); realloc() realloc (pointer_name, number * sizeof(int)); free() free (pointer_name); malloc() function The process...

2 minutes read.

Classification of Programming language

 Classification of Programming language  The programming language represents a set of instructions compiled along with each other to perform a specific task provided by the CPU (Central Processing Unit). A programming...

3 minutes read.

Bar3d() function in C Graphics

The bar3d function is used to create a 2-dimensional filled-in rectangular bar. We can also create three-dimensional shapes in C using helpful function. The first step in creating this 3D...

3 minutes read.

What is algorithm in C?

What is an algorithm? In computer programming languages, an algorithm is set of statement that solves a particular problem. In C, first step of every algorithm is Start and last step of...

3 minutes read.

Bit Fields in C

The size of a structure in C is specified in bits. The primary goal of it is to use memory efficiently, once we understand that a bit's value must fall...

3 minutes read.

Static in C

Static is a keyword that is used in the C programming language. It can be used both as variables and as functions. In other words, it can be declared both...

3 minutes read.

Scope of variables in C

Introduction The scope of variables in C can be defined as the scope of reach of a variable, the term scope is used to determine the visible range of an object....

4 minutes read.

Infinite loop in C

Definition of infinite loop The infinite loop is a loop that does not get dismissed because of its looping construct. A continuous output or no output are the two possible outcomes...

3 minutes read.

Compilation Process in C language

What is the Compilation Process?  The compilation is a method whereby the source code is converted into object code. It is achieved with compiler assistance. The compiler tests the source code for syntactic...

3 minutes read.

Tower of Hanoi in C

What is Tower of Hanoi? The Tower of Hanoi is a gaming problem that was created in 1883 by a French mathematician named Édouard Lucas. The Tower of Hanoi temple in...

4 minutes read.

C Program to find the Roots of a Quadratic Equation

What is Quadratic Equation: Equations of degree 2 in polynomial form are called quadratic equations. A, B, and C are the coefficient variables in the equation, which is written as ax2...

3 minutes read.

Flow Chart of For loop in C

This is a flowchart that represents the process of executing the for loop in the C programming language. Generally, as we know there are three main components of for loop:1. The...

3 minutes read.

Multilevel Feedback Queue Scheduling (MLFQ) CPU Scheduling

In this article, you will learn how to initialise an array to 0 in C In C, an array is declared as: char ZEROARRAY[2022]; The global scope changes at runtime to all zeros....

3 minutes read.

Array to function in C

Array to function in C We can create functions that receive an array as an argument. To pass an array into a function, we need to write the name of the...

4 minutes read.

C Program Swap Numbers in cyclic order Using Call by Reference

The three integers that the user entered in cyclical sequence are swapped in this tutorial using call by reference. C Program: #include <stdio.h> void cyclic_Swap ( int *X, int *Y, int *Z ); int...

2 minutes read.

Assert() Function in C

Assert (): In C, the statements are executed with the exit statement. In C language declare, and it tests the condition parameters. If the statement executed will be false, it...

3 minutes read.