TechBeamersTechBeamers
  • Learn ProgrammingLearn Programming
    • Python Programming
      • Python Basic
      • Python OOP
      • Python Pandas
      • Python PIP
      • Python Advanced
      • Python Selenium
    • Python Examples
    • Selenium Tutorials
      • Selenium with Java
      • Selenium with Python
    • Software Testing Tutorials
    • Java Programming
      • Java Basic
      • Java Flow Control
      • Java OOP
    • C Programming
    • Linux Commands
    • MySQL Commands
    • Agile in Software
    • AngularJS Guides
    • Android Tutorials
  • Interview PrepInterview Prep
    • SQL Interview Questions
    • Testing Interview Q&A
    • Python Interview Q&A
    • Selenium Interview Q&A
    • C Sharp Interview Q&A
    • PHP Interview Questions
    • Java Interview Questions
    • Web Development Q&A
  • Self AssessmentSelf Assessment
    • Python Test
    • Java Online Test
    • Selenium Quiz
    • Testing Quiz
    • HTML CSS Quiz
    • Shell Script Test
    • C/C++ Coding Test
Search
  • Python Multiline String
  • Python Multiline Comment
  • Python Iterate String
  • Python Dictionary
  • Python Lists
  • Python List Contains
  • Page Object Model
  • TestNG Annotations
  • Python Function Quiz
  • Python String Quiz
  • Python OOP Test
  • Java Spring Test
  • Java Collection Quiz
  • JavaScript Skill Test
  • Selenium Skill Test
  • Selenium Python Quiz
  • Shell Scripting Test
  • Latest Python Q&A
  • CSharp Coding Q&A
  • SQL Query Question
  • Top Selenium Q&A
  • Top QA Questions
  • Latest Testing Q&A
  • REST API Questions
  • Linux Interview Q&A
  • Shell Script Questions
© 2024 TechBeamers. All Rights Reserved.
Reading: C For Loop Purpose, Flowchart, and Example
Font ResizerAa
TechBeamersTechBeamers
Font ResizerAa
  • Python
  • SQL
  • C
  • Java
  • Testing
  • Selenium
  • Agile Concepts Simplified
  • Linux
  • MySQL
  • Python Quizzes
  • Java Quiz
  • Testing Quiz
  • Shell Script Quiz
  • WebDev Interview
  • Python Basic
  • Python Examples
  • Python Advanced
  • Python OOP
  • Python Selenium
  • General Tech
Search
  • Programming Tutorials
    • Python Tutorial
    • Python Examples
    • Java Tutorial
    • C Tutorial
    • MySQL Tutorial
    • Selenium Tutorial
    • Testing Tutorial
  • Top Interview Q&A
    • SQL Interview
    • Web Dev Interview
  • Best Coding Quiz
    • Python Quizzes
    • Java Quiz
    • Testing Quiz
    • ShellScript Quiz
Follow US
© 2024 TechBeamers. All Rights Reserved.
C Tutorials

C For Loop Purpose, Flowchart, and Example

Last updated: Dec 25, 2023 1:37 pm
By Meenakshi Agarwal
Share
5 Min Read
C For Loop Purpose, Flowchart, and Example
C For Loop Purpose, Flowchart, and Example
SHARE

In this C programming class, we’ll cover the C for loop statement, its purpose, syntax, flowchart, and examples. Please note that the loops are the main constructs to implement iterative programming in C.

Contents
C For Loop FlowchartC For Loop SyntaxProgram-1: Program to find the factorial of a numberFlowchart:Algorithm:Code:Program-2: Program to find all numbers between 1 to 100 divisible by 4Flowchart:Algorithm:Code:

C For Loop for Beginners

In our previous tutorial, we learned the functioning of while and do-while loops. In this chapter, we will see the for loop in detail.

We’ve taken up an entire chapter on the “for loop” because it is the most used iterative programming construct. And the programmers use it in almost every program. Hence, let’s start with its explanation.

Why is the “For Loop” Used in C Programming?

The For Loop is a loop where the program tells the compiler to run a specific code FOR a specified number of times.

This loop allows using three statements, first is the counter initialization, next is the condition to check it and then there is an increment/decrement operation to change the counter variable. You will understand it once we see some programs.

For starters, this flowchart will help you.

C For Loop Flowchart

C For Loop Flowchart

C For Loop Syntax

for( triad statement )
{

//statement block

}

The for loop’s triad statement has the following structure:

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

First comes the initialization of the counter variable.

For example – If the variable is “i” then it needs to be initialized like the following.

i = 0;

Next, there comes a conditional statement separated by a semicolon.

Example: It can be a specific value, or it can be another variable.

"; i < 0; " or "; i >= a; "

The last is the increment/decrement operation again separated by a semicolon.

Example: It goes like the following.

"; i++" or "; i--"

Finally, the C for loop gets the following structure.

for (i = 0; i <= 10; i++)
{

//Statement block

}

Now we will see some examples of this through some programs.

C For Loop Examples

Program-1: Program to find the factorial of a number

Flowchart:

C For Loop Program-1 Flowchart

Algorithm:

Step 1: Start.

Step 2: Initialize variables.

Step 3: Check FOR condition.

Step 4: If the condition is true, then go to step 5 otherwise go to step 7.

Step 5: f = f * i.

Step 6: Go to step 3.

Step 7: Print value of factorial.

Step 8: Stop.

Code:

#include <stdio.h>
#include <conio.h>

void main()
{
    int i, a ,f = 1;

    printf("Enter a number:\n");
    scanf("%d", &a);

    for (i = 1; i <= a; i++)
        f = f * i ;

    printf("Factorial of %d is %d\n", a, f);
    getch();
}

The output of the above example is something like this-

C For Loop Program-1 Output

You have two options here. Either you can put {} after for or you cannot. However, if there are multiple statements, then you should put {} so that the code remains organized.

Program-2: Program to find all numbers between 1 to 100 divisible by 4

Flowchart:

C For Loop Program-2 Flowchart

Algorithm:

Step 1: Start

Step 2: Initialize variables

Step 3: For condition

Step 4: If condition. If it is true, then go to step 5 otherwise to step 7

Step 5: Print value

Step 6: Increment the value of "i" by 1

Step 7: Go to step 3

Step 8: Stop

Code:

#include<stdio.h>
#include<conio.h>

void main()
{
    int i;

    printf("Numbers from 0 to 100 divisible by 4\n");

    for(i = 0; i <= 100; i++)
    {
        if(i%4 == 0)
        {
            printf("%d\t", i);
        }
        i++;
     }

     getch();
}

Output:

C For Loop Program-2 Output

The for loop is very important in C language and will be used very often so you should study this comprehensively. This chapter may have fewer exercises but you’ll find plenty of for loop examples in further tutorials to come.

Happy Coding!

You Might Also Like

C Programming Language “Struct”

20 C Programs for Beginners to Practice

Learn C Programming – How To Guide for Beginners

Python vs C++ – Is Python or C++ Better?

The Best Top-Down Approach Guide for You in C

Meenakshi Agarwal Avatar
By Meenakshi Agarwal
Follow:
Hi, I'm Meenakshi Agarwal. I have a Bachelor's degree in Computer Science and a Master's degree in Computer Applications. After spending over a decade in large MNCs, I gained extensive experience in programming, coding, software development, testing, and automation. Now, I share my knowledge through tutorials, quizzes, and interview questions on Python, Java, Selenium, SQL, and C# on my blog, TechBeamers.com.
Previous Article C While and Do-While Loops C While and Do-While Loops
Next Article List Append Method in Python Explained with Examples List Append Method

Popular Tutorials

SQL Interview Questions List
50 SQL Practice Questions for Good Results in Interview
SQL Interview Nov 01, 2016
Demo Websites You Need to Practice Selenium
7 Sites to Practice Selenium for Free in 2024
Selenium Tutorial Feb 08, 2016
SQL Exercises with Sample Table and Demo Data
SQL Exercises – Complex Queries
SQL Interview May 10, 2020
Java Coding Questions for Software Testers
15 Java Coding Questions for Testers
Selenium Tutorial Jun 17, 2016
30 Quick Python Programming Questions On List, Tuple & Dictionary
30 Python Programming Questions On List, Tuple, and Dictionary
Python Basic Python Tutorials Oct 07, 2016
//
Our tutorials are written by real people who’ve put in the time to research and test thoroughly. Whether you’re a beginner or a pro, our tutorials will guide you through everything you need to learn a programming language.

Top Coding Tips

  • PYTHON TIPS
  • PANDAS TIPSNew
  • DATA ANALYSIS TIPS
  • SELENIUM TIPS
  • C CODING TIPS
  • GDB DEBUG TIPS
  • SQL TIPS & TRICKS

Top Tutorials

  • PYTHON TUTORIAL FOR BEGINNERS
  • SELENIUM WEBDRIVER TUTORIAL
  • SELENIUM PYTHON TUTORIAL
  • SELENIUM DEMO WEBSITESHot
  • TESTNG TUTORIALS FOR BEGINNERS
  • PYTHON MULTITHREADING TUTORIAL
  • JAVA MULTITHREADING TUTORIAL

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Loading
TechBeamersTechBeamers
Follow US
© 2024 TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use
TechBeamers Newsletter - Subscribe for Latest Updates
Join Us!

Subscribe to our newsletter and never miss the latest tech tutorials, quizzes, and tips.

Loading
Zero spam, Unsubscribe at any time.
x