×

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

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.

How to use Typedef Struct in C

The “typedef” is a predefined keyword in C programming language which is used to declare the new name to an existing type of variable. For better understanding, if you declare a...

3 minutes read.

Malloc function in C

When the user or programmer does not know how much memory space is needed in the application, it a needs dynamic memory allocation during runtime. Dynamic memory allocation helps us to...

4 minutes read.

Types of Pointers in C

Pointers in C A pointer is a variable and this variable contains the address of another variable, i.e it’s a variable which has the address of another variable as its value....

4 minutes read.

Functions in C

What is function? Functions are defined as the set of announcements which take inputs, perform operations and provide results. The operation of a function only performs when the function gets called....

4 minutes read.

Hook() function in C

Use of hook () function in C Introduction:  The hook () is a function used in the C programming language. The basic concept of the hook() function is that it can remove the function...

3 minutes read.

C Program to Find Largest Number Using Dynamic Memory Allocation

This tutorial will teach us how to locate the highest number a user has ever input into a dynamically allocated memory. C Program: #include <stdio.h> #include <stdlib.h> int main ()  {   int no;   double *data_n;   printf...

1 minute read.

Big O Notation in C

Big O Notation in C: In the C programming language, we have so many algorithms and solutions to the problems that exist, which have different aspects and purposes to an...

3 minutes read.

fgets() function in C

fgets() function is present in standard input and output library i.e, stdio.h library. It is a built-in function or pre-define function. It is used to read the specified stream from...

4 minutes read.

Why C is a middle level language

The C programming language is generally referred to as a high level language but we glance through the backend of C, i.e the working of C as a programming language...

4 minutes read.

2f in C language

Float data type in c: Double-precision floating-point numbers with up to 17 significant digits are stored in the FLOAT data type. FLOAT is equivalent to C's double data type and IEEE...

3 minutes read.

Recursion in C

Recursion in C: In the C programming language, the concept known as recursion exists that is a technique in which a function calls itself either directly or indirectly. It allows...

4 minutes read.

Comments in C

Comments are used to comment on the line of code in the program. Comments are a way of inserting remarks and reminders into code without affecting its behavior. The compiler...

1 minute read.

Pseudo Code in C

Pseudo code in C can be referred to as a simple way or method to write programming code or program algorithm in English. A pseudocode is an informal representation of...

4 minutes read.

Pi() Function in C

Pi: Mathematic constants are not defined in the c or c++ programming languages. To use the Mathematical constants firstly, we have to define the _USE_MATH_DEFINES and then declare the cmath and...

3 minutes read.

Do-While Loop in C

Both 'While' and 'Do-While' loops in C mostly have a similar concept, and the code of both loops runs for mainly similar purposes in the same manner. But, there is...

3 minutes read.

Bit Stuffing Program in C

What is a Bit Stuffing? The process of inserting one or more extra bits (0) into data is known as bit stuffing. To provide signalling information to a recipient, these are...

3 minutes read.

Program to Find Mode of an Array in C

Mode is the highest occurring value or number in a given set of data elements. In a group of data values, a value that occurs most frequently is considered to...

3 minutes read.

Bigint (BIG INTEGERS) in C with Example

The maximum number of digits that a long, long int can have in C/C++ is 20. The issue is how to store the 22-digit number, which is difficult to do...

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