×

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

Array to function in C

Array to function in C We can create functions that receive an array as an argument. To pass an array into a function, we need to write the name of the...

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

Do-While Loop in C

Both 'While' and 'Do-While' loops in C mostly have a similar concept, and the code of both loops runs for mainly similar purposes in the same manner. But, there is...

3 minutes read.

Factorial Program in C using For Loop

Before move on the factorial program. We have to know about the for loop in C programming language. The syntax of the For Loop is: for (initialization statement; test expression; an increment...

3 minutes read.

Formatted Input and output function in C

Formatted I/O functions display multiple outputs to the user by taking various inputs. All data types like int, float, char and double are supported by the formatted I/O function. In...

4 minutes read.

getch() function in C

getch() is a pre-define or built-in function present in the conio.h library. It returns the given character immediately without waiting for the enter key to be entered. By using getch()...

3 minutes read.

Stack implementation in C

Stack implementation in C Stack stores the data in a particular order. It is a linear data structure that follows the principle of the Last In First Out (LIFO) technique where...

3 minutes read.

tolower() Function in C

The tolower() capability is characterized in the ctype.h header document. In the event that the person passed is a capitalized letter set, the tolower() capability switches capitalized letters in order...

3 minutes read.

Dos.h Header File in C Language

Dos.h is a header file in C. Interrupt handling, sound generation, date and time functions, and other tasks can be performed using the functions in this library. This is exclusive to...

4 minutes read.

C Switch Statements

Switch-case statement comes under the Selection control statement; there are four types of Control statements Decision-making statements (if, if-else)Selection statements (Switch-case)Iteration statements (for, while, do-while)Jump statements (break, continue, goto) If we want...

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

Calloc in C

The calloc() is a library function in C which is used for memory allocation. The calloc() function dynamically allocates multiple blocks of memory to complex data structures. This includes data structures...

3 minutes read.

Pi() Function in C

Pi: Mathematic constants are not defined in the c or c++ programming languages. To use the Mathematical constants firstly, we have to define the _USE_MATH_DEFINES and then declare the cmath and...

3 minutes read.

Character Class Tests in C

Introduction The <ctype.h> is a header file in C which is used to declare functions for testing characters. This header file of the C standard library is used to declare multiple...

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

fopen() function in C

fopen() function, is one of the file handling functions. It is used to open the existing file and perform operations on the file. If the file does not exist, it...

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

Fibonacci series program in C using Recursion

In this tutorial, we’ll explore how to utilise recursion to build the Fibonacci sequence in C language. What does the term "Fibonacci Series" mean? The following number in a Fibonacci number series is...

2 minutes read.

Fee Management System in C

The concept is to split every operation into its very own characteristic. Software is made from all of the capabilities mixed with transfer cases. An example of the capabilities may...

5 minutes read.

C Program Swap Numbers in cyclic order Using Call by Reference

The three integers that the user entered in cyclical sequence are swapped in this tutorial using call by reference. C Program: #include <stdio.h> void cyclic_Swap ( int *X, int *Y, int *Z ); int...

2 minutes read.