×

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

Caesar Cipher Program in C

What is Caesar Cipher? The Caesar Cipher is a type of substitution cipher that is named after Julius Caesar, who is said to have used it to encrypt messages sent to...

2 minutes read.

Derived Data Types in C

A variable in a program occupies some space in the computer's memory where some value is stored. Each variable in C has an associated data type. A value to be...

4 minutes read.

fopen() function in C

fopen() function, is one of the file handling functions. It is used to open the existing file and perform operations on the file. If the file does not exist, it...

3 minutes read.

Snake Game in C

Snake game in C is basic desktop console program which was created in 1970’s. It is an old classic game which was played by every child across the world. This...

4 minutes read.

Variable Declaration in C

What is a Variable? A Variable is nothing more than a name for a memory place where data/information can be stored. Any alphabet (from a to z or A to Z), the...

4 minutes read.

Typecast vs. typedef in C

Typecast vs. typedef in C Typecast In the C programming language, converting the data type from one form to another is known as type casting or the type conversion. It is a...

5 minutes read.

Booleans in C

Introduction to Boolean With all the complexities of programming, it can take time to understand the basics. One concept, in particular, that confuses many beginners is the use of Booleans in...

6 minutes read.

abs() function in C

abs() function is a built-in function present in the stdlib.h header file. It returns an integer value. abs() function converts a negative value into a positive value. For example, if...

4 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.

DSA Program in C

How is a Data Structure Implemented? The foundational terms of a data structure are the following terms. Data may be arranged using data structures to make it easier to use. Each data...

20 minutes read.

Matrix Multiplication in C

Matrix Multiplication in C Matrix multiplication in C: Two matrices can be added, subtracted, multiplied, and divided. To do so, we take input from the consumer for row number, column number, first element matrix,...

3 minutes read.

Flow chart of While loop in C

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

3 minutes read.

Simple hash() function in C

Introduction In this context, we briefly discuss HASH FUNCTION, HASHING or HASH TABLE in C. It is a function used to map data and mapped arbitrary sizes to the fixed-size values. The...

7 minutes read.

Associativity of Operators in C

What is an Associativity operator? Two operators with equal priority are connected by employing their association when they are present in an expression. There are two different types of associativity: left to rightright...

4 minutes read.

Exit control loop in C

To examine the termination condition for an exit, we usually use an exit control loop in C. If the evaluation for termination condition results in true, then the control would...

3 minutes read.

Length of an Array Function in C

In C, there is no built-in function to get the length of an array. However, there are a few ways you can determine the length of an array. It is necessary...

15 minutes read.

C/C++ Program to Find the Size of int, float, double and char

In this tutorial, we will learn how to use the sizeof operator to determine the size of each variable. Program to Determine Variable Size Write a C or C++ program to determine the...

2 minutes read.

First Fit Program in C

This is one of the easiest ways to allocate memory. The main goal is to divide the memory into several fixed sizes. In C, there is only one process for...

3 minutes read.

C pre-processor

The C processor is a macro processor which is used to compile the source code of the program (step by step) . It is not a part of the compiler....

4 minutes read.

If else Programs in C Programming

If else Programs in C programming The if-else statement in C is based on some particular conditions to perform the operations. If and only if the given condition is valid, the operations listed in...

4 minutes read.