×

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

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.

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.

Decimal to Hexadecimal in C

Decimal to Hexadecimal in C Let us first understand the definitions of decimal and hexadecimal. In algebra, a decimal number is defined as a number whose whole number part and a...

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

Passing Array to Function in C

Need to pass Arrays? The need to pass arrays to a function arises when we need to pass a list of values to a given function. During the course of our programming...

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

How to Avoid Structure Padding in C

Generally, when an object of some structure type is declared, some contiguous memory will be allocated in block form, which will be allocated to structure members. To understand structure padding...

3 minutes read.

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? In for loop, the initialization command is...

3 minutes read.

What are linker and loader in C

Linker and loader are utility programs that have a significant role in executing a program. Linker: A linker is a program that joins the object files produced by the assembler/ compiler...

3 minutes read.

What is the main in C?

In the C programming language, "main" is a predefined term or function. It is the primary function in every C program and is in charge of beginning and terminating the...

6 minutes read.

Pointer to pointer in C

A pointer to another pointer is another type of multiple indirections and a chain of many pointers. Generally, a pointer consists of the address of the variable. Once a pointer to...

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

Goto and Labels in C

Introduction Goto in C: The goto statement is known as the jump statement in C. The goto is used to transfer the control of a program to a predefined label. The goto...

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.

Runtime vs compile time in C

In the C programming language, the often used terms in every step consist of compile time and runtime. The compile time is referred to the source code that will be...

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

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.

Malloc function in C

When the user or programmer does not know how much memory space is needed in the application, it a needs dynamic memory allocation during runtime. Dynamic memory allocation helps us to...

4 minutes read.

Fahrenheit to Celsius in C

Before going into conversion, we have to know what Fahrenheit and Celsius mean, these are both units to measure the temperature.In our daily life, we use both Fahrenheit and Celsius,...

1 minute read.

fgets() function in C

fgets() function is present in standard input and output library i.e, stdio.h library. It is a built-in function or pre-define function. It is used to read the specified stream from...

4 minutes read.