×

Flow Chart of For loop in C

Flow Chart of For loop in C

This is a flowchart that represents the process of executing the for loop in the C programming language.

Generally, as we know there are three main components of for loop:
1. The initialization statement,
2. The termination condition, and
3. The update statement: This statement this helps in altering the value for further execution.

Here, you can see that the first step is to declare the variable and initialize its value with the help of assignment operator.

In the next step, we will have to use a test expression that will certainly help in dismissing the for loop or from preventing it to run toward infinity as per the requirement. If the termination condition or test expression is true then the control will go to the main body of the loop.
The last component of for loop is the increment or decrement statement that will be executed after the execution of the body.

This execution will terminate here. And, the loop will go to execute for next time and check whether the value returned by the termination condition is true or false. Loop will continue to execute until the condition becomes false. As the value is found to be false then the control will come out of the loop and the loop will completely dismissed.

Example of For Loop:

Here is program in c that will print numbers from 11 to 20 with the help of for loop:

#include <stdio.h>
int main() 
{
  int a; /* declaring a variable ‘a’ */
  for (a = 11; a <= 20; ++a)
/* initializing its value as 11; a must be less than 20; modifying statement */
  {
    printf ("%d ", a);
/* printing final value of variable ‘a’ 
  }
  return 0;
}	

Explanation:

Initially we declared a variable ‘a’. Then, we have for loop syntax that contains 3 main components which are as follows:

  1. a=11            (Initialization statement)
  2. a<=20          (termination condition)
  3. ++a              (increment or decrement statement)

In this, the value of ‘a’ will begin with 11, and the termination condition will check that the value of ‘a’ is smaller than 20 or not. After this, there is modifying statement that helps in altering the value of ‘a’ so that further evaluations can be done. This can be pre-increment, post-increment, pre-decrement, or post-decrement. When the value of ‘a’ goes higher than 20 then the termination condition will terminate the for loop.

Two More Examples of For Loop

Example 1:

#include <stdio.h>
int main() 
{
  int a; 
/* declaring a variable ‘a’ */
  for (a = 199; a >= 148; --a)
/* initializing its value as 199; a must be more than 148; modifying statement */
  {
    printf ("%d ", a);
/* printing final value of variable ‘a’ */
  }
  return 0;
}

Example 2:

#include <stdio.h>
int main()
{
    int a, b, c = 0;
/* declaring a variable ‘a’ ‘b’ ‘c’ */
    printf("Enter a positive integer: ");
    scanf("%d", &a);
    for(b = 1; b <= a; ++b)
/* initializing its value as 1; a must be less than a; modifying statement */
    {
        c += b;
    }
    printf("c = %d", c);
/* printing final value of variable ‘c’ */
    return 0;
}

Related Topics

Difference between rand() and srand() function in C

What is the rand()? The rand() means random function. This function is used in C. It generates random numbers in the range of 0 to the RAND_MAX. Suppose we generate a...

3 minutes read.

Infinite loop in C

Definition of infinite loop The infinite loop is a loop that does not get dismissed because of its looping construct. A continuous output or no output are the two possible outcomes...

3 minutes read.

How to use floor() function in C

Introduction: The floor() used as a function in the C programming language. This function uses to return the largest or most significant integer value. The integer value is smaller than...

3 minutes read.

Displaying Array in C

Reference a collection of variables of similar data type in an array using a single element. The parts are stored next to each other. When declaring an array, you must specify...

4 minutes read.

Segmentation Fault in C

Segmentation Fault in C In the C programming language, segmentation fault or segmentation violation or core dump is a condition that the hardware component raises to protect the memory by indicating...

4 minutes read.

Multi-dimensional Arrays in C

Multi-dimensional Arrays in C The C programming allows the concept of multi-dimensional arrays. The data within the multidimensional arrays are stored in the tabular form, that is, in a row significant...

3 minutes read.

Heap Sort in C

In this tutorial, we will learn about heap sorting in C language, but before going to Heap sort, we have to know the concept of Complete Binary Tree. What is Complete...

8 minutes read.

How to Calculate Time Complexity in C?

What is time complexity? An algorithm's time complexity measures how long it takes to complete a task in relation to the size of the input. It should be noted that the...

5 minutes read.

C printf and Scanf

Input-Output functions in C Programming In C Language, the printf() and scanf() are inbuilt library functions that used for input and output. It is defined in the header file“<stdio.h>”. printf() Function: In C...

2 minutes read.

Self-referential structure in C

Self-referential structure in C A self-referential structure is a structure that can have members which point to a structure variable of the same type. They can have one or more pointers...

3 minutes read.

Kruskal algorithm in C

Given a weighted graph, Kruskal's algorithm generates a spanning tree with the lowest possible weights. Start by creating an edge list for the given graph, including the weights. Sort the...

3 minutes read.

C Language Environment Setup

To compile C program, we must have GCC compiler installed on our machine. In this C tutorial, all the examples are compiled and tested using GCC compiler. Although we can...

3 minutes read.

Find Day from Day in C Without using function

Introduction: In the given article, I find daily in C without using functions. It takes 365 days for the earth to revolve around the sun. It will be close to...

3 minutes read.

Data Structures And Algorithms in C

C language is one of the most flexible and simple languages. So, it is always better to learn any concept in C language compared to other languages. Let us look...

13 minutes read.

Difference between rand() and srand() function in C

What is the rand()?The rand() means random function. This function is used in C. It generates random numbers in the range of 0 to the RAND_MAX. Suppose we generate a...

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

10 Best IDEs for C or C++ Developers in 2024

Nobody can deny the fact that C and C++ were the first programming languages used by significant developers worldwide. Even now, newcomers who want to start programming are most frequently...

6 minutes read.

Multilevel Feedback Queue Scheduling (MLFQ) CPU Scheduling

In this article, you will learn how to initialise an array to 0 in C In C, an array is declared as: char ZEROARRAY[2022]; The global scope changes at runtime to all zeros....

3 minutes read.

Nested loop in C

Definition of Nested Loop A nested loop is generally used when we want to run a loop statement inside another loop statement. This kind of loop is also known as a...

3 minutes read.

Call Back Function in Embedded C

Callback function are one the most remarkable systems in C. A callback function is any code that is passed as a contention to another code, so this is the last...

3 minutes read.