for loop in C-Definition, syntax, flow chart, examples

for loop in C-Definition, syntax, flow chart, examples

Before getting knowledge of while loop, we have to knowledge of what is a loop

Loop is the process of executing the same program statement or block of program statements repeatedly for specified a number of times or until the given condition satisfied. A loop executes when the given condition is true when a condition becomes false the program control will be out of the loop.

For example

We want to display C is the best 10 times, one of to get the desired output is we type printf(“\n C is best”); 10 times. However, it increases the length of the program and consumes a lot of time. Another way to perform this is by using a loop structure. With a loop structure, we do not need to type the same program statement repeatedly. The loop structure is also known as an iteration control structure. 

What is for loop in C programming

For loop is the most common type of loop which is used to execute the program statement or block of program statements repeatedly for a certain time. The body of the loop does not execute for a single time if the condition is not satisfied.

syntax of for loop in C:

        

for(initialization;condition;increasing/decreasing)

                              {

                                           body of loop;

                              }


            
flow chart of for loop in c

Part of Syntax

Initialization: -

             Initialization defines the loop starting point.

Condition:-

            Condition defines the loop stopping point. If the condition becomes true then the body of the loop will be executed otherwise program, the statement will be out of the loop.

 Increasing/decreasing: -

           Increasing and decreasing helps to increase or decrease the counter.

How does for loop work?

Ø  In for loop first, the loop starting point is evaluated. This process is called the initialization of the loop. For example the loop start with 1 then the initialization point is 1. Similarly, the loop starts with 0 the initialization point is 0.

Ø  Second step condition is evaluated. Condition is in the loop is the ending point of the loop. For example, i<=5. It means the loop is repeated until the value of i is reached 5. When the value of I becomes 6 then program control of the loop.

Ø  After the condition is evaluated, increasing and decreasing of the loop is defined.

Ø  At, first, the while loop checks the condition with an initial point if it is true then it executes its body. When it becomes false, program control will be out of the loop. After one time its body executes, at the second time it checks the condition with the value of the loop is increasing or decreasing given in the increasing/decreasing part. This process goes continuous until the given condition is satisfied.

Example 

C program to print "C is best"  10 time using for loop

#include<stdio.h>

#include<conio.h>

void main()

{

          int i;

          for(i=1;i<=10;i++)

          {

                   printf("\n C is best");

          }

          getch();

}

Output

C is best using C




In the above example, we evaluate initial point i=1 and give condition i<=10 because we have to print C is best 10 times.

At first, the loop is stat with initial point 1 and check condition as 1<=10 which is obviously true then the body of loop printf("\n C is best"), and second-time i++ increase the value of and check condition as 2<=10 which is also a true and execute body of loop printf("\n C is best");. Similarly, at the third time, it checks condition as 3<=10, and the body of loop printf("\n C is best"); is executed. Continuously, it executes until condition 10<=10 when the condition becomes 11<=10 then program control will be out of the loop and it prints “C is best” 10 times.


for(i=1;i<=10;i++)
It is a loop structure, where i=1 is an initialization part and it shows as the loop starts from 1.
i<=10 this part is called condition and it shows the loop is repeated until 10 times.
i++ is an inc/dec part. It shows the loop increase value of i by 1 until 10 times. In the C program to increase the value of a variable by 1 write variable++ and to decrease write variable--.

Let see another example.
Write a program to print 1 to n using for loop

 #include<stdio.h>

#include<conio.h>

void main()

{

          int i, n;

          printf("\n Enter the value of n:");

          scanf("%d",&n);

          for(i=1;i<=n;i++)

          {

                   printf("%d\t",i);

          }

          getch();

 

Output
1 to n term using C

In this example value of n must be provided by users. The loop for(i=1;i<=n;i++) increase the value of n and the body of loop printf("%d\t",i); prints the number from 1 to n.
For more example of for loop
Also, check

while loop in C  
For do while loop





Post a Comment

0 Comments