×

For Loop in C

The Syntax of For Loop

for (initialization statement; test expression; update statement)
{
    /* main body of the FOR loop */
}

How does For loop work?

  1. In for loop, the initialization command is implemented only once.
  2. After that, it checks the test expression of the loop and finds whether the test expression is false or true. And, for loop is dismissed if the test expression is false.   
  3. If the test expression of the 'F' loop becomes true, then the program line provided in the F loop’s body is executed, and the update expression is changed accordingly.
  4. Again, the entire process of evaluation is to be done. The loop executes its body until and unless the value of the test expression in for loop becomes false. The loop automatically terminates as soon as the test expression result becomes false.

Flow chart of For Loop

Here we will try to understand the entire for-loop process with the help of a flowchart.

For Loop in C

Example 1: Program of Simple For loop:

For Loop in C

Output:

For Loop in C
  1. Here, we initialized the value of a to 1, and the loop starts its evaluation from 1.
  2. After that, the termination condition is implemented as a<10.
  3. The value of 'a' is smaller than 10, so the condition is true, and the statement inside the 'FOR' loop is implemented. As a result, one will be printed as the final value on the output screen.
  4. After that, the altering statement (++a) is implemented for increment. Due to increment, the value of variable ‘a’ will become 2.
  5. Again, the termination condition is checked for true or false, and if it is found to be true, then the body of FOR-LOOP is implemented once again. This time the value is displayed as 2 on the output.
  6. This process will continue to check and evaluate until the value of ‘a’ becomes 10.
  7. When the value of 'a' becomes 10, then the a<10 condition automatically becomes false, and the for loop terminates.

Example 2: Program of For loop:

For Loop in C

Output:

For Loop in C
  1. In this program, we initialized the value of a to 10, and the loop starts its evaluation from 10.
  2. After that, the termination condition is implemented as a>1.
  3. The value of a is greater than 1. So, the specification is true at 10, and the statement inside the 'For' loop is implemented. As a result, 10 will be printed as the final value on the output screen.
  4. After that, the altering statement (--a) is implemented for decrement. Now, as a result, the value of variable ‘a’ will become 9.
  5. Again, the termination condition is checked for true or false, and if it is found to be true, then the body of FOR-LOOP is implemented once again. This time the value is displayed as 3 on the output.
  6. This process will continue to check and evaluate until the value of a becomes 1.
  7. When the value of 'a' becomes 1, then the 'a>1' condition automatically becomes false, and the for loop terminates.

Example 3: Program of For loop:

For Loop in C

Output:

For Loop in C
  1. The variable ‘number’ stores the value entered by the user. As mentioned above, the user enters the value 20.
  2. As the initial value of the count is set to 1, the termination condition is checked. If the test expression count<=number is true, then the statements inside the for loop are implemented, and the total will be equal to 1.
  3. After that, the increment command (++count) is applied, and the count will be equal to 2.
  4. Once again, the termination program line is to be implemented.
  5. As 10 is also greater than 2. So, the termination statement evaluates as true, and the for loop statement will be shown on the screen.
  6. This time, the variable total stores 3.
  7. This process repeatedly executes until the count becomes 20.
  8. When the count becomes 20, the termination command becomes 0 (false), and the for loop is closed.
  9. After that, the total of all numbers to 20 is displayed on the output screen.

Related Topics

How to convert a string to hexadecimal in C

Converting a character array or any string to its respective hexadecimal form is simple. The only thing we have to do is to follow the below steps. Take each character from...

3 minutes read.

Floor() Function in C

Floor() function is a built-in function in C which is defined in the math.h header file. This function is used to return the nearest integer value which is less than...

4 minutes read.

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

3 minutes read.

Simple hash() function in C

Introduction In this context, we briefly discuss HASH FUNCTION, HASHING or HASH TABLE in C. It is a function used to map data and mapped arbitrary sizes to the fixed-size values. The...

7 minutes read.

#undef in C

#undef in C In the C programming language, the #undef directive is used to undefine the macro or any constant, which will be defined by the preprocessor directive #define. The #undef...

3 minutes read.

Fibonacci Series in C Using For Loop

In this C article, we will let you know about the procedure of displaying the Fibonacci series of the first “n” positive integers. The syntax of for loop used in the...

4 minutes read.

Built-in functions in C

The function is a set of instructions and statements enclosed in the "{}" delimiter. In c, there are two types of functions. Pre-define functions/ Built-in functionsUser define function. Built-in functions in C:- These...

8 minutes read.

Error handling in C

Error handling in C: The C programming standard does not provide direct convenience for handling the errors. However, being a system programming language, it definitely will give access to handling...

4 minutes read.

Int in C

The keyword int in C programming stands for integer and it is a data type which is used for variable declarations or declaration of functions of different types. Similar to...

4 minutes read.

Limitations of Synchronisation and Uses of Static Synchronisation in Multithreading

The multithreading component of java is the element around which the idea rotates as it permits simultaneous execution of at least two program pieces for the most significant usage of...

9 minutes read.

Use of Structure in C

We can usually use arrays in C programming to store elements of the same data type. We can use strings to store multiple elements of a character data type. Still,...

4 minutes read.

Sum of N numbers in C using For loop

Before we move on the program of sum of N numbers, first we have to know about the For Loop statement. The syntax of ‘for’ loop in C programming language is...

3 minutes read.

Why does sizeof(x++) not Increment x in C

Sizeof() is a much-involved operator in C or C++. It is an incorporated time unary administrator which can be utilized to figure the size of its operand. The consequence of...

5 minutes read.

fork() in C

Introduction To create a new function in the system, there is a need for a system call, i.e. fork system call. It is also known as the child process. These child...

2 minutes read.

C program to compare the two strings

C program to compare the two strings Strings can be compared either by using the string function or without using string function. First, we will look at how we can compare the strings with...

4 minutes read.

Null character in C

Introduction The Null character in the C programming language is used to terminate the character strings. In other words, the Null character is used to represent the end of the string...

3 minutes read.

GPA Calculator in C

GPA stands for Grade Point Average. This GPA is used to measure the student's academic performance in educational institutes. By using this, segregation takes place and lets us know how...

4 minutes read.

DSA Program in C

How is a Data Structure Implemented? The foundational terms of a data structure are the following terms. Data may be arranged using data structures to make it easier to use. Each data...

20 minutes read.

Find reverse of an array in C using functions

Introduction: Here, we describe how to find the reverse array in C using functions. Suppose you have an array with n elements. I need to display the elements present in...

3 minutes read.

Reverse a Stack using Recursion in C

In this tutorial we will learn how recursion will be used in this case to reverse a stack. For loops, while loops, do-while loops, and similar constructions are not permitted....

5 minutes read.