• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » C » Structures » C Programming for Loop
Next →
← Prev

C Programming for Loop

By Dinesh Thakur

The for loop is used for repetitive execution of a statement or a group of statements. It is a very powerful and flexible statement of the C language. It is generally used in situations where the number of iterations of the loop statement is known or can be determined in advance.

It can also be used in situations where the number of iterations is not known or cannot be determined in advance. However, the while loop is more convenient in such situations. The general form of the for loop is given below.

for ( initial_ expr ; final_ expr ; update_ expr )

statement;

This loop executes the contained statement repeatedly as decided by three control expressions (initial_expr, final_expr and update_expr). The initial_expr is usually an assignment expression, whereas the update_expr is an increment/decrement or compound assignment expression. The final_expr expression is usually a relational expression but may also be a Boolean expression or any valid C expression. Observe that the control expressions are written within a pair of parentheses and are separated by semicolons.

The most commonly used form of the for loop uses a loop control variable (also called loop variable, counter variable or index variable). It is a variable of any arithmetic type such as integer, floating-point, character, etc. Most programmers prefer short names for loop variables, e. g., variables i, j and k are commonly used in situations where integer loop variables are required.

The loop variable assumes a sequence of values as decided by the three control expressions. For each value of the loop variable, the statement included in the for loop is executed once. The initial_ expr initializes the loop variable, the update_expr updates (increment, decrement, etc.) its value and the final_expr is a test expression that compares its value with the desired final value to decide whether the execution of the contained statement should be continued or not.

Consider the for loop given below used to print the string “Hello world\n” four times.

for (i = l; i <= 4; i++)

      printf(“Hello world\n”);

In this for loop, variable i (assumed to be of type int) is used as a loop variable. The initial expression, i = 1 , initializes the i to 1. The final expression, i <= 4 , compares value of i with the desired final value, i.e., 4. The update expression, i++, increments the value of i by 1. Thus, the loop variable i assumes values 1, 2, 3 and 4. For each of these values, the printf statement prints the string “Hello world” followed by a newline. Thus, the output is as follows:

Hello world

Hello world

Hello world

To understand the execution of the for loop, refer control flow diagram shown in Fig and the flowcharts in Fig. The execution of the for loop proceeds as follows: Initially, expression initial_ expr is evaluated. It performs the initialization of the loop variable. Then final_expr is evaluated. If it is true, the statement included in the loop is executed followed by evaluation of update_expr.

           Control flow diagram of the for loop

           Flowcharts of the for loop

Then final expr is evaluated again. The execution of statement within the loop followed bythe evaluation of update_expr and final_expr continues as long as final_expr evaluates true; otherwise, the control leaves the for loop and continues with the execution of the next statement in the program. Note that the execution of the contained statement followed by the evaluation of update expression and final expression is commonly referred as an iteration of the loop. Also, the statement contained in the loop is commonly referred to as the body of the loop. ·

Although the values of loop variables used in the above for loop are quite natural, it is a common practice to use zero as the initial value for the loop variable. Moreover, as the array subscripts start from zero, the for loops used to process arrays also use zero as the initial value of loop variable. Hence, the style of using zero as the initial value of loop variables, wherever appropriate, is strongly recommended. Such a for loop to perform n iterations is given below.

      for (i = 0; i < n; i++)

Note the use of the ‘<‘ operator in the final expression (i < n). Now the program segment to print Hello world five times can be rewritten as follows: ·

      for (i = 0; i < 5; i++)

           printf(“Hello world\n”);

Illustrates the application of a for loop for calculating factorial of a given number

#include<stdio.h> 
void main()
{
    int i, n , Factorial=1;
    clrscr();
    printf("Enter the number for finding factorial.");
    scanf("%d", &n);
    printf("n\tFactorial\n" );
    for ( i= 1;i<=n; i++)
    Factorial *= i;
    printf( "%d\t%d\n", n, Factorial);
}

In one execution of the above program, the number entered is 5 and value of 5! is given by the program.

     

In the above program you would observe that the loop starts from the line for (int i=1; i<=n; i++) and ends at the statement Factorial *=i;. For the next iteration, the i is incremented, tested by the middle expression of the for loop and the iteration is carried out if the test is not false. So, in every iteration, the value of i is multiplied to the previous value of the factorial. At the end of the loop, we obtain Factorial= 1×2 x 3 x 4 x 5 =120.

You’ll also like:

  1. Programming Practices with Top-Down, Bottom-Up, Structured Programming, and Information Hiding
  2. Discuss how and when the values of the loop index change throughout the processing of the loop
  3. Explain differences between Do/Loop and For-Next Loop.
  4. Explain control statements those are used in C programming language
  5. Write a program in C programming language to print weekdays using switch statement
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

C Programming

C Programming Tutorials

  • C - History
  • C - Anatomy
  • C - Constants
  • C - Identifiers
  • C - Data Types
  • C - Libraries File
  • C - Header Files
  • C - Basic Language
  • C - Data Types Sizes
  • C - Header Files Importance
  • C - Escape Sequences
  • C - Main() Purpose
  • C - Program Procedure
  • C - Control Statements
  • C - Enumeration Constant
  • C - Add numbers
  • C - Return Statement
  • C - Avoid Goto
  • C - Command Line Arguments
  • C - Switch Case
  • C - Switch Case Limitations
  • C - getchar() and putchar()
  • C - Iteration Statements
  • C - Pass by Value and Reference
  • C - Structures and Unions
  • C - Structure
  • C - Dynamic Memory
  • C - Fgets and Fputs Functions
  • C - Gets() and Puts() Functions
  • C - Armstrong Number
  • C - Storage Classes
  • C - Fibonacci Series
  • C - Precision Setting
  • C - const Parameters

C - Variable & It's Type

  • C - Variables
  • C - Variable Lifetime
  • C - Static Variable
  • C - Register Variable
  • C - Global Variables
  • C - Auto Variables
  • C - Local Variables

C - Operator & Expressions

  • C - Operator
  • C - Boolean Operators
  • C - Bitwise Operator
  • C - Arithmetic Operators
  • C - Modulus Operator
  • C - Ternary Operator
  • C - Expressions
  • C - Arithmetic Expressions

C - Array

  • C - Arrays
  • C - Array Types
  • C - Array Characteristics
  • C - Static Arrays
  • C - Global Arrays
  • C - 3D Arrays
  • C - Dynamic Arrays
  • C - Pointer to 3D Arrays
  • C - Array Elements Hold
  • C - Arrays as Function Parameters
  • C - Accessing Matrix Elements
  • C - File Handling
  • C - Matrix Multiplication
  • C - Dynamic Memory Allocation

C - Searching & Sorting

  • C - Data Structures
  • C - Linear Search
  • C - Bubble Sort
  • C - Merge Sort
  • C - Linked List
  • C - Insertion Sort
  • C - Binary Search
  • C - Selection Sort
  • C - Quick Sort

C - Functions

  • C - Functions
  • C - Functions Advantages
  • C - Void Functions
  • C - Function Call
  • C - Default Return Value
  • C - String functions

C - Pointer

  • C - Pointers
  • C - Type Casting Of Pointers
  • C - Pointer Advantages
  • C - Pointers Initialization
  • C - Vectors and Pointers

C - Differences

  • C - C Vs C++
  • C - Formal Args. Vs Actual Args.
  • C - Keywords Vs Identifiers
  • C - Strings Vs Character Arrays
  • C - Address Vs Dereference Operator
  • C - Goto Vs longjmp
  • C - Declaring Vs Defining Variable
  • C - String Vs Array
  • C - Call by Value Vs Reference
  • C - Structure Vs Union
  • C - For Vs While loops
  • C - Compiler Vs Interpreter

C - Programs

  • C Program Standard Deviation
  • C Program Calculate Tax
  • C Program Sum Series
  • C Program Merge Arrays
  • C Program Euclid’s Algorithm
  • C Program Print Weekdays
  • C Program Sum of Digits
  • C Program Print a list
  • C Program Print Pythagorean
  • C Program Quiz program
  • C Program Display Table
  • C Program Print Comma-Separated
  • C Program Prints Prime Numbers
  • C Program for Print Integer
  • C Program Count Number
  • C Program Print Color Name
  • C Program Print Odd Numbers
  • C Program Calculate area
  • C Program for a Menu
  • C Program Add Two Vectors
  • C Program Array Addresses
  • C Program Division by Zero Error
  • C Program Compare two Dates
  • C Program Tower of Hanoi
  • C Program return 3 Numbers
  • C Program for Prime Numbers
  • C Program for Factorial
  • C Program for Palindrome

Other Links

  • C Programming - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW