Norway


In this C programming class, we’ll cover the C for statement, its , syntax, , and examples. Please note that the loops are the main constructs to implement iterative programming in C.

C For Loop for Beginners

In our previous , we have learned the functioning of while and do-while loops. In this chapter, we will see the for loop in detail.

We’ve taken up an entire chapter on the “for loop” because it is the most used iterative programming construct. And the programmers use it in almost every program. Hence, let’s start with its explanation.

1. What is For Loop in C Programming?

The For Loop is a loop where the program tells the compiler to run a specific code FOR a specified number of times.

This loop allows using three statements, first is the counter initialization, next is the condition to check it and then there is an increment/decrement operation to change the counter variable. You will understand it once we see some programs.

For starters, this flowchart will help you.

1.1. C For Loop Flowchart

C For Loop Flowchart  - C For Loop Flowchart - C For Loop – Learn Its Purpose with Flowchart, and Example

 

1.2. C For Loop Syntax

for( triad statement )
{

//statement block

}

The for loop’s triad statement is like the ( i=0 ; i < n ; i++ ).

First comes the initialization of the counter variable.

For example – If the variable is “i” then it needs to be initialized like the following.

i = 0;

Next, there comes a conditional statement separated by a semicolon.

Example: It can be a specific value, or it can be another variable.

"; i < 0; " or "; i >= a; "

The last is the increment/decrement operation again separated by a semicolon.

Example: It goes like the following.

"; i++" or "; i--"

Finally, the C for loop gets the following structure.

for (i = 0; i <= ; i++)
{

//Statement block

}

Now we will see some examples of this through some programs.

2. C For Loop Examples

2.1. Program-1: Program to find the factorial of a number

Flowchart:

C For Loop Program-1 Flowchart  - C For Loop Program 1 Flowchart - C For Loop &#8211; Learn Its Purpose with Flowchart, and Example

 

Algorithm:

Step 1: Start.

Step 2: Initialize variables.

Step 3: Check FOR condition.

Step 4: If the condition is true, then go to step 5 otherwise go to step 7.

Step 5: f = f * i.

Step 6: Go to step 3.

Step 7: Print value of factorial.

Step 8: Stop.

Code:

#include <stdio.h>
#include <conio.h>

void main()
{
    int i, a ,f = 1;

    printf("Enter a number:n");
    scanf("%d", &a);

    for (i = 1; i <= a; i++)
        f = f * i ;

    printf("Factorial of %d is %dn", a, f);
    getch();
}

The output of the above example is something like this-

C For Loop Program-1 Output  - C For Loop Program 1 Output - C For Loop &#8211; Learn Its Purpose with Flowchart, and Example

 

You have two options here. Either you can put {} after for or you cannot. However, if there are multiple statements, then you should put {} so that the code remains organized.

2.2. Program-2: Program to find all numbers between 1 to 100 divisible by 4

Flowchart:

C For Loop Program-2 Flowchart  - C For Loop Program 2 Flowchart - C For Loop &#8211; Learn Its Purpose with Flowchart, and Example

 

Algorithm:

Step 1: Start

Step 2: Initialize variables

Step 3: For condition

Step 4: If condition. If it is true, then go to step 5 otherwise to step 7

Step 5: Print value

Step 6: Increment the value of "i" by 1

Step 7: Go to step 3

Step 8: Stop

Code:

#include<stdio.h>
#include<conio.h>

void main()
{
    int i;

    printf("Numbers from 0 to 100 divisible by 4n");

    for(i = 0; i <= 100; i++)
    {
        if(i%4 == 0)
        {
            printf("%dt", i);
        }
        i++;
     }

     getch();
}

Output:

C For Loop Program-2 Output  - C For Loop Program 2 Output - C For Loop &#8211; Learn Its Purpose with Flowchart, and Example

 

The for loop is very important in C language and will be used very often so you should study this comprehensively. This chapter may have fewer exercises because we expect you to the for loop like a snow plower in further tutorials to come.



Source link

LEAVE A REPLY

Please enter your comment!
Please enter your name here