for Loop in C

For Complete YouTube Video: Click Here

In this class, we will try to understand for Loop in C.

We have already discussed the while loop and do-while loop.

for Loop

The for loops are best choices to count up (Incrementing) and count down (Decrementing) the variable.

The for loop in C are frequently used.

For better understanding of for loop in c let’s start with the syntax.

for Loop Syntax

The image below is the syntax for the for loop in C.

for loop in C Syntax
for loop in C Syntax

In the above image for is the keyword and open and closed parenthesis are used for the expressions or statements.

The expressions of the for loop in C are of three types.

  1. Initialization Expression
  2. Control Expression
  3. Increment/Decrement Expression

Every statement is separated by a semi colon. Which indicates the end of the expression or statement.

Initialization Expression

In the initialization statement the variable will get initialized.

The initialization is done only at the first iteration of the for loop.

Control Expression

This expression is the going to control the iteration of the loop.

At every iteration, even in the first iteration after initialization the control expression will get executed.

If the control expression happens to be true then the body of the loop will get executed.

If the control expression happens to be false we will come out of the loops body.

Increment/Decrement Expression

After executing the loops body the Increment/Decrement of the variable is done.

Below the for is the loops body enclosed within the open and closed flower parentheses.

The loops body contain the statements to e executed repeatedly.

Once the control expression evaluates to false the execution of the loops body will stop.

for Loop Example

The image below is an example on for loop.

for loop in C Example
for loop in C Example

The above program is an example to print the integers from 1 to n.

The number of integers to be printed is provided by the user of the program and is stored in variable n.

Assume that user has given the value of n as 5.

In the above program i is the variable used to print the values.

The variable i initialized to 1 in the first iteration.

After the initialization the control statement i<n is checked.

This control statement is making the loop to iterate for 5 times as the value of n is 5 given by the end user.

If the control expression evaluates to true the body of the loop will get executed and print the value of i.

After executing the body of the loop the third expression increment of i is done.

This iteration repeats until the control expression evaluates to false.

General Constructs of the for Loop

The image below are the general constructs of the for loop to count up and count down.

General Construct for loop in C
General Construct of for loop in C

The first construct is to count up the variable for n times starting from zero to n-1.

As we are starting from the zero and to iterate for n times the value of variable should be n-1.

Therefore the i is initialized to zero and control statement is i<n.

Similarly, the second construct is to count up the variable for n times from 1 to n.

In the same way the count down of the variable can also be done by using 3rd and 4th constructs.

We suggest you to practice more on the above constructs for better understanding as they are frequently used.