For Loop in C Programming
  • Twitter
  • Facebook
  • Snapchat
  • Instagram

For Loop in C Programming – Syntax, Flowchart and Examples

📑 On this page:
  • Introduction to For Loop
  • Use Cases of For Loop
  • For Loop Syntax
  • For Loop Flowchart
  • Standard For Loop Example
  • Infinite For Loop Example (for;;)
  • Frequently Asked Questions
📚 In this tutorial, you will learn:
  • What is a for loop and why it is used in C programming
  • The syntax of for loop with initialization, condition, and update
  • How the for loop works with step-by-step flowchart explanation
  • How to write programs using standard for loop
  • How to use infinite for loop (for;;) with break statement

Introduction to For Loop in C

The for loop in C programming is used to execute a block of code repeatedly for a known number of times until a specified condition is met. It is a fundamental control structure commonly used for iteration in C programs.

A for loop is especially useful when you need to repeat a task a specific number of times. It helps in writing efficient, clean, and organized code.

Common Use Cases of For Loop

  • Generating number series — Even numbers, Fibonacci series, etc.
  • Traversing arrays — Accessing elements in arrays, linked lists, or other data structures
  • Processing student records — Looping through records to calculate grades
  • Game development — Implementing repetitive tasks in games
  • Searching — Searching for a value in an array
  • Sorting — Sorting, searching, and traversing array elements
  • CAPTCHA generation — Generating CAPTCHA codes for user verification
  • Password validation — Validating password attempts within limited tries
  • E-commerce — Calculating the total number of products and price in a shopping cart

💡 Key Point: Using the for loop efficiently can improve both performance and readability of your C programs.

For Loop Syntax in C

for (initialization; test_condition; update_expression) {
    /* statements or body of the loop */
}

Let's break down each part:

🔹 Initialization: The initialization step is used to declare and initialize variables in the for loop. These variables have local scope and are accessible only within the loop body. This step is executed only once, before the loop starts. Multiple variables can be declared and initialized in the initialization section. It defines the starting point of the loop.

🔹 Test Condition: This condition determines whether the loop should continue executing. It is a boolean expression that evaluates to either true or false. If true, the loop body is executed. If false, the loop terminates, and control passes to the next statement after the loop. The body of the loop continues to execute as long as the test condition is true.

🔹 Update Expression: This is the final part of the loop definition. It is executed after each iteration and typically updates the loop control variable. The update expression helps control the number of iterations. If not handled correctly, the loop may become infinite.

Infinite For Loop Syntax

If the initialization, test condition, and update expression are all omitted, the loop becomes an infinite loop.

for ( ; ; ) {
    /* statements or body of the loop */
}

⚠️ Note: The above loop will run infinitely because no initialization, test condition, or update expression is specified. To stop it, you need to use a break statement inside the loop body.

For Loop Flowchart

The diagram below shows the step-by-step control flow of a for loop in C programming.

Control flow diagram of for loop in C programming with initialization, condition check, loop body, and update steps shown visually.

Control Flow of a For Loop in C:

  1. Initialization (Declaration): This step executes only once, before the loop starts. In this section, the loop control variable is declared and initialized (e.g., int i = 0;). These variables have local scope within the loop.
  2. Test Expression (Condition Check): The test condition is evaluated. If it returns true, the loop continues to the next step. If it returns false, the loop terminates, and control moves to the statement following the loop.
  3. Loop Body Execution: If the test condition is satisfied, the statements inside the loop body are executed.
  4. Update Expression: After the loop body executes, the control variable is updated (e.g., i++;). This step is crucial for progressing toward the loop termination condition.
  5. Repeat: Control returns to step 2 (test expression). This cycle continues until the test condition evaluates to false.

⚠️ Important: If the initialization, test condition, or update expression is omitted, the loop may become an infinite loop. Always ensure the update expression eventually makes the condition false.

C Program to Demonstrate Standard For Loop

The program below prints numbers from 1 to 10 using a standard for loop.

/* This C program prints the numbers from 1 to 10 using a for loop */

#include <stdio.h>

int main() {
    int i;
    
    printf("\nFor loop to display numbers from 1 to 10\n");
    
    for(i = 1; i <= 10; i++) {
        printf(" %d", i);
    }
    
    return 0;
}

Sample Output:

For loop to display numbers from 1 to 10
1 2 3 4 5 6 7 8 9 10

Explanation:

  1. The variable i is declared as an integer.
  2. The program first prints the message: For loop to display numbers from 1 to 10.
  3. The for loop begins:
    • The loop starts by setting i = 1.
    • It checks if i <= 10. Since 1 is less than or equal to 10, the loop body executes, printing the current value of i.
    • After printing, i increments by 1 (i++).
    • The condition is checked again with the updated value of i.
    • This process repeats, printing numbers from 1 through 10.
    • When i becomes 11, the condition i <= 10 fails, and the loop stops.
  4. The program then ends, having displayed the numbers 1 to 10 on the screen.

C Program to Illustrate Infinite For Loop (for;;)

The program below displays numbers from 1 to 10 using a for(;;) infinite loop with a break statement.

/* C program to display numbers from 1 to 10 using for(;;) loop */

#include <stdio.h>

int main() {
    int i = 1;
    
    for(;;) {
        printf(" %d", i);
        i++;
        
        if(i > 10)
            break;
    }
    
    return 0;
}

Sample Output:

1 2 3 4 5 6 7 8 9 10

Explanation:

  1. We first declare and initialize i = 1.
  2. The control enters the for(;;) loop. Since there is no initialization, condition, or update inside the loop header, the loop doesn't check any condition and directly enters the body.
  3. The statement printf(" %d", i); is executed, which prints the value of i — in the first iteration, it's 1.
  4. Then, i++; increments the value of i by 1. Now, i becomes 2.
  5. The if(i > 10) condition is checked. Since i = 2, the condition is false, so the break statement is skipped.
  6. Control goes back to the top of the loop. Again, since the loop header doesn't have any test condition, it continues and enters the loop body again.
  7. This process repeats: i is printed, incremented, and then checked against the condition if(i > 10).
  8. When i becomes 11, the condition if(i > 10) becomes true. So, the break statement executes, which stops the loop.
  9. The control comes out of the loop, and since there are no more statements in the program, it ends.

📝 Note: Even though for(;;) is an infinite loop, we've controlled its execution using the if and break statements. This pattern is sometimes used when the exit condition is complex or needs to be checked in the middle of the loop.

💻 Practice Exercise

Challenge: Write a program that prints the sum of all even numbers from 1 to 100 using a for loop.

🔍 Click to Show Solution
#include <stdio.h>

int main() {
    int i, sum = 0;
    
    for(i = 2; i <= 100; i += 2) {
        sum = sum + i;
    }
    
    printf("Sum of even numbers from 1 to 100: %d\n", sum);
    return 0;
}

/* Output: Sum of even numbers from 1 to 100: 2550 */

📝 Summary

  • The for loop is used for known number of iterations
  • It has three parts: initialization, condition, and update
  • The initialization runs only once at the beginning
  • The condition is checked before each iteration
  • The update runs after each iteration
  • for(;;) creates an infinite loop that must be stopped with break
  • For loops are commonly used for array traversal, number series, and counting

Frequently Asked Questions About For Loop in C

1. What is a for loop in C?

A for loop is a control flow statement that allows you to execute a block of code repeatedly for a specific number of times. It consists of initialization, condition, and update expressions.

2. When should I use a for loop?

Use a for loop when you know the exact number of iterations in advance. For example, when traversing arrays, printing series, or performing calculations with a known count.

3. What happens if I omit the condition in for loop?

If you omit the condition, the loop becomes an infinite loop. For example, for(;;) will run forever unless you use a break statement inside the loop.

4. Can I declare variables inside the for loop initialization?

Yes, you can declare variables inside the initialization part. For example, for(int i = 0; i < 10; i++). These variables are local to the loop and cannot be accessed outside.

5. What is the difference between for loop and while loop?

A for loop is used when the number of iterations is known. A while loop is used when the number of iterations is unknown and depends on a condition. Both can achieve the same result, but for loop is more concise for counting.

💡 Tip: Always make sure the update expression eventually makes the condition false. Otherwise, you might create an infinite loop.

📖 Related Tutorials

  • while Loop in C - Complete Guide
  • do-while Loop in C - Complete Guide
  • for Loop Examples in C
  • Introduction to Loops in C

Previous Topic: -->> while loop assignments   ||   Next topic: -->> for loop examples


📚 Explore More Topics

🗄️ SQL Interview Questions & Answers

SQL SELECT Statement FAQ SQL Restricting & Sorting Data FAQ SQL Group Functions & Aggregated Data FAQ SQL Multiple Tables (JOINs) FAQ SQL Subqueries FAQ SQL DML Statements (Managing Tables) FAQ SQL Indexes, Synonyms & Sequences FAQ SQL DDL (Tables & Relationships) FAQ SQL Views FAQ SQL Indexing Best Practices FAQ SQL Window & Analytic Functions FAQ

🐍 Python Interview Questions & Answers

Python Interview Questions Python Syntax & Variables FAQ Python Data Types FAQ Python If-Else FAQ Python Loops FAQ Python Functions Interview Q Python String Manipulation FAQ Python Lists & Dictionaries FAQ Python Tuples & Sets FAQ Python Exception Handling FAQ Python OOP Interview Questions

☕ Java Interview Questions & Answers

Java Introduction Interview Q Java Development Environment FAQ Java Data Types FAQ Java Control Flow & Operators FAQ Java Basic Input/Output FAQ Java Arrays FAQ Java Strings FAQ Java Methods FAQ Java Basic OOP Concepts FAQ Java Advanced OOP Concepts FAQ Java OOP Best Practices FAQ Java Exception Handling FAQ Java Synchronization FAQ Java Threads & Concurrency FAQ Java Collection Framework FAQ Java File I/O & Serialization FAQ Java Serialization & Deserialization FAQ Java Features FAQ Java Inner & Anonymous Classes FAQ Java Memory Management FAQ Java Packages FAQ Java Wrapper Classes FAQ Java Streams & Lambda FAQ

C Language

  • Home
  • Why C Language
  • History of C Language
  • Applications of C Language
  • Introduction To C
    • What is Program?
    • Structure of C Program
    • Working Of C Program
    • CHARACTER SET
    • VARIABLES AND IDENTIFIERS
    • BUILT-IN DATA TYPES
    • OPERATORS AND EXPRESSIONS
    • CONSTANTS AND LITERALS
    • SIMPLE ASSIGNMENT STATEMENT
    • BASIC INPUT/OUTPUT STATEMENT
    • SIMPLE 'C' PROGRAMS
    • Assignments
  • Operators in C Programming
    • Arithmetic Operators
    • Assignment Operators
    • Increment and Decrement Operators
    • Relational Operators
    • Logical Operators
    • Bitwise Operators
    • Other Operators
    • Assignments
  • Conditional Statements
    • DECISION MAKING WITHIN A PROGRAM
    • CONDITIONS
    • IF STATEMENT
    • IF-ELSE STATEMENT
    • IF-ELSE LADDER
    • NESTED IF-ELSE
    • SWITCH CASE
    • Assignments
  • Loops Statements
    • Introduction to Loops
    • GO TO Statement
    • Do while Loop
    • While Loop
    • Nested While Loop
    • Difference Between While and Do while
    • Difference Between Goto and loop
    • while loop assignments
    • C FOR Loop
    • C For loop examples
    • Nested for loop
    • Nested for loop examples
    • Infinite while Loops
    • Infinite for Loops
    • Continue in Loops
    • break in Loops
    • difference while do..while & for
    • Assignments
  • Arrays
    • One Dimensional Array
    • Declaring 1D Arrays
    • Initilization of 1D arrays
    • Accessing element of one 1D Array
    • Read and Display 1D Arrays
    • Two Dimensional Arrays
    • Declare 2D Arrays
    • Read and Display 2D Arrays
    • Assignments/Examples
  • Functions
    • Introduction
    • Need For User-Defined Function
    • Multiple Function Program
    • Modular Programming
    • Elements Of User Defined Function
    • Function Definition
    • Function Declaration
    • Types of functions
    • Nesting of Function
    • Recursion
    • Passing Array To Functions
    • Scope,Visibility and Lifetime of Variables
    • Assignments
  • Structure
    • Introduction
    • Array vs Structure
    • Defining Structure
    • Declaring Structure Variables
    • Type Defined Structure
    • Accessing Structure Members
    • Structure Initilization
    • Copying & Comparing Structure Variables
    • Array of Structure
    • Arrays Within Structure
    • Structures Within Structures
    • Structures and Functions
    • Structure Examples/Assignments
  • Union
    • Define Union
    • Create and use Union
    • Difference Between Structure and Union
    • Union Examples
    • Union FAQ
  • Pointers
    • What Are Pointers In C?
    • How Do We Use Pointers In C?
    • Declaration Of A Pointer
    • The Initialization Of A Pointer
    • Syntax Of Pointer Initialization
    • Use Of Pointers In C
    • The Pointer To An Array
    • The Pointer To A Function
    • The Pointer To A Structure
    • Types Of Pointers
    • The Null Pointer
    • The Void Pointer
    • The Wild Pointer
    • The Near Pointer
    • The Huge Pointer
    • The far Pointer
    • dangling pointer
    • Accessing Pointers- Indirectly And Directly
    • Pros Of Using Pointers In C
    • Cons Of Pointers In C
    • Applications Of Pointers In C
    • The & Address Of Operator In C
    • How To Read The Complex Pointers In C?
    • Practice Problems On Pointers
  • File Processing
    • File Handling In C
    • Types Of Files In C
    • Operations Done In File Handling
    • File Examples
    • Binary Files
    • count words,lines in a file
    • Copy files
    • Update File
    • count vowels in a file
  • Preprocessor
    • Macro substitution division
    • File Inclusion
    • Conditional Compilation
    • Other directives
    • Examples
  • Dynamic Memory Allocation
    • malloc
    • calloc
    • free
    • realloc
    • Examples
  • Storage Classes
  • Graphics
  • Frequently Asked Interview Questions (FAQ)
    • Introduction To C FAQ
    • Operators FAQ
    • Conditional Statements FAQ
    • Loops FAQ
    • Arrays FAQ
    • Function FAQ
    • Structure FAQ
    • Pointers FAQ
    • Files FAQ
    • Storage classes FAQ
    • Dynamic Memory FAQ
  • Programs/Assignments
    • Introduction To C
    • Operators
    • Conditional Statements
    • Loops
    • Arrays
    • Function
    • Structure
    • Pointers
    • Files
    • Storage classes
    • Dynamic Memory
  • Case Studies
  • Multiple Choice Questions
    • Introduction To C MCQ
    • Operators MCQ
    • Conditional Statements MCQ
    • Loops MCQ
    • Arrays MCQ
    • Function MCQ
    • Structure MCQ
    • Pointers MCQ
    • Files MCQ
    • Storage classes MCQ
    • Dynamic Memory MCQ
    • More MCQ

Get in touch

  • tech2dsm@gmail.com

© Sankalan Data Tech. All rights reserved.