What is For loop statement? Why use for loop ?

for loop statement
for loop statement

Looping statement are the statements execute one or more statement repeatedly several number of times.

Why use loop ?

When you need to execute a block of code several number of times then you need to use looping concept in C language.

Advantage with looping statement

Reduce length of Code

Take less memory space.

Burden on the developer is reducing.

Time consuming process to execute the program is reduced.

For loop in C is the most general looping construct. The loop header contains three parts: an initialization, a continuation condition, and step.

for (initialization; condition; step/inc/dec)
Statement 1;
Statement 2;
Statement n;

For statement contain three parts separated by two semicolons. The first part is known as initialization. The variable called loop control variable or index variable is initialized in this part.

The second part is known as the condition. The condition should be a value one. The condition check can be a compound expression made up of relational expression connected by logical AND, OR.

The third part is known as step. It should be an arithmetic expression. The initialization need not be contained to a single variable. If more than one variable is used for initialization they are separated by commas. The step can also applied to more than one variable separated by commas.

When you need to execute a block of code several number of times then you need to use looping concept in C language. In C Programming Language for loop is a statement which allows code to be repeatedly executed. It contains 3 parts.



Increment or Decrements

Initialization: This step is execute first and this is execute only once when we are entering into the loop first time. This step is allow to declare and initialize any loop control variables.

Condition: This is next step after initialization step, if it is true, the body of the loop is executed, if it is false then the body of the loop does not execute and flow of control goes outside of the for loop.

Increment or Decrements: After completion of Initialization and Condition steps loop body code is executed and then Increment or Decrements steps is execute. This statement allows to update any loop control variables.

Note: In for loop everything is optional but mandatory to place 2 semicolons (; 😉

For loop is the most commonly used and popular loop amongst all three loops present in C. The reason behind its popularity lies in the simplicity. For loop is the easiest loop to begin programming with loops.

Syntax of for loop:

for ( initialization ; condition ; update )
//Body of loop

for loop statement
Fig:for loop statement
  • First initialize the variable, It execute only once

when we are entering into the loop first time.

  • In second step check condition
  • In third step control goes inside loop body and execute.
  • At last increase the value of variable

Same process is repeat until condition not false

Few more things to note about for loop:
Initialization part runs only once.

The condition, body of loop and updation part runs repeatedly until the loop terminates.

for lopping statement

Note: For updation you may use any expressions that increments the value of i by 1. Such as you are free to use: i = i + 1; or i+=1; or i++;

Also if the body of loop contains single statement then there is no need to use curly braces { } for the for loop. As the above loop can also written as.
for ( i=1 ; i<=10 ; i++) printf(“Hello, World!\n”);

But it is always recommended that use pair of curly braces { } with the for loop whether it contains single or multiple statements.

An infinite for loop is a loop without any termination condition. Infinite for loop runs infinite times without terminating from the loop. They are generally used in programming when the number of repetitions are unknown by the programmer, for example: The CPU works in an infinite loop with its task as FETCH-DECODE-PROCESS. And keeps running infinitely until the user shuts down the PC.

example of empty loop
Write a C program to enter any number and print table of the given number using for loop. 

Format of the table should be like: If num = 5
5 * 1 = 5
5 * 2 = 10

5 * 10 = 50

c program of for loop


difference between conditional and looping statement

Nested loop

In Nested loop one loop is place within another loop body.

When we need to repeated loop body itself n number of times use nested loops. Nested loops can be design upto 255 blocks.


Be the first to comment

Leave a Reply

Your email address will not be published.