for loop

For Loop in C || Simple Tutorial

Hi guys. Welcome to the Digital Chalkie Educational Blog. Today I’m going to discuss “While Loop in  C programming”. This article belongs to our  ” C Programming Tutorial Series”. You can read other articles by click here.

Definition:- A loop is a method that uses to execute a block of statements repeatedly until a given condition returns false.

Syntax of For Loop

Here is the way that we can write a for loop in C program.

for ( initialization ; condition ; increment)
{ 
  Body Statement(s)
}

Let see how the above syntax works.

  • The initialization is executed only once. This step allows you to initialize the loop control variable.
  • Next, the program evaluates the “Condition“. If the condition is true, the Body Statements are executed. If not it terminates the loop and jumps to the statement just after the for-loop.
  • After the executes the Body statement in the loop, the program (flow control) jumps to the increment and executes it. By ‘increment’ we update the value of the ‘ loop control variable’ which we initialize at “Initialization Step”.
  • After the ‘increment‘ program evaluates the condition again. Then executes the body statements if the condition true.
  • This process repeats until the condition become false. ( statements –> increment –> condition –> statement –> increment…….)  When the condition becomes false, the loop terminates.

You can clearly understand these processes by the example given below.

Flow Diagram of For loop

for loop C
Note : The yellow box does not belong to the standard flow chart. Here I use it to mark the elements of for loop.

  Example to Refer

include <stdio.h>
int main()
{
    int i=0;
    for (i=1; i<=5; i++)  // Condition
    {
        printf("%d \t",i); //statement to repeat 
    }
    printf("\n For Loop is Over. i = %d ",i);
    return 0;
}

In the above program, we try to print the integers one to five. We use “i” as the loop control variable (counter variable).  At the beginning “i” initialize to one. We know that five is greater than one. So the condition is true and it begins to execute body statements. According to the example program prints 1 and a tab space.  Then it execute the increment ” i++”. It means that to increase the value of “i” by one. So “i” become 2.

After the print 5, “i” becomes 6.  When i=6 our condition is false and program terminates the loop. After terminating it execute the code segment just after the for loop. [ printf(“\n For Loop is Over. i = %d “,i) ]

Output of above example

1       2       3       4       5
 For Loop is Over. Value of i = 6

 

Try this Yourself.

Write a program to print the first “n” square numbers. You can use this online compiler or Codebloks.

Bonomi Tharinda
Bonomi Tharinda is the Developer of the BMaxx Blog. He is a skilled web developer and graphic designer. In addition, he is an undergraduate student of a representative University in Sri Lanka. He is working as a tutor of an institute and as a part-time blogger, he will help you all to make your problems solved.Here are his social accounts and you can follow him through them.