×

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.

Variables in C

A variable can be defined as a name allocated to a storage space that can be manipulated by our programs. Every variable in C arbitrates about the overall layout and...

5 minutes read.

How to delete a file in C

A file is a group of data kept on a secondary device, such as a hard disc. It is typically utilized as a real-world application with a lot of data. These...

3 minutes read.

Merge Sort in C

The merge sort follows the principle of the divide and conquers algorithm. Firstly it divides the input of an array into two halves and then calls itself for the two...

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

Random function in C

Random function in C In the C programming language, the rand() is a function used for Pseudo Random Number Generator (PRNG). The random number generated by the rand() function is not...

4 minutes read.

Remove an element from an array in C

A collection of objects or pieces of the same data type stored in a single memory block is known as an array. A data structure called an array is used...

3 minutes read.

One Dimensional Array in C

One Dimensional Array in C: In the C programming language, an array is defined as a collection of one or more values of the same type. Every value present in...

4 minutes read.

Use of fflush(stdin) in C

Use of fflush(stdin) in C Usually, fflush() is only used for the output stream. The purpose is to clean (or flush) the output buffer and transfer the buffered data into...

2 minutes read.

Prototype in C

A prototype is nothing but a model, a model of initial creation of an intended product. Similarly, in the C programming language, all functions have a prototype. In general, all...

4 minutes read.

Star Program in C Language

Star Program in C Star patterns are a sequence of * or any other character used to construct any pattern or any like-square geometric form, triangle, hollow square, pyramid, rhombus, etc.  Many programmers worldwide highly...

4 minutes read.

#define in C

#define in C In the C programming language, the preprocessor directive acts an important role within which the #define directive is present that is used to define the constant or the...

3 minutes read.

Advantages of Dynamic Memory Allocation in C

Dynamic Memory Allocation Dynamic memory allocation is a process of assigning or allocating memory to a variable during the execution of a program. Dynamic memory allocation provides storage according to the...

3 minutes read.

Ceil function in C

Introduction In the C Programming Language, the ceil function is a library function which is used to obtain the ceil value, i.e., it returns the smallest integer that is greater than...

4 minutes read.

Banker’s Algorithm in C

Introduction Before doing a "s-state" check to look for potential activities and deciding if allocation should be allowed to continue, the banker's algorithm, a resource allocation and deadlock avoidance algorithm, tests...

5 minutes read.

Palindrome Number in C

What is a Palindrome? A number that doesn't change when reversed is known as a palindrome. We reverse a number and compare it to the original number to determine whether it is...

4 minutes read.

Type Casting in C

Type casting is a way to convert a variable from one data type to another data type.Syntax: (type) expression;     Example: #include <stdio.h> int main() { int a,b; float sum; printf("Example of Type Casting\n"); printf("Enter any integer nos:"); scanf("%d",&a,&b); sum=a+b; printf("Sum: %f",sum); return 0; } Output Example...

1 minute 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.

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.

Comma Operator in C

What is a comma operator? The comma operator in the C programming language has the least priority. The comma operator is essentially a binary operator that operates on the first operand...

4 minutes read.