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

[ad_1]

In this C programming class, we’ll cowl the C for loop assertion, its function, syntax, flowchart, and examples. Please word that the loops are the principle constructs to implement iterative programming in C.

C For Loop for Beginners

In our earlier tutorial, now we have realized the functioning of while and do-while loops. In this chapter, we’ll see the for loop in element.

We’ve taken up a whole chapter on the “for loop” as a result of it is essentially the most used iterative programming assemble. And the programmers use it in virtually each program. Hence, let’s begin with its rationalization.

1. What is For Loop in C Programming?

The For Loop is a loop the place this system tells the compiler to run a selected code FOR a specified variety of occasions.

This loop permits utilizing three statements, first is the counter initialization, subsequent is the situation to test it and then there is an increment/decrement operation to vary the counter variable. You will perceive it as soon as we see some applications.

For starters, this flowchart will provide help to.

1.1. C For Loop Flowchart

C For Loop Flowchart

1.2. C For Loop Syntax

for( triad assertion )


//assertion block

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

First comes the initialization of the counter variable.

For instance – If the variable is “i” then it must be initialized like the next.

i = 0;

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

Example: It generally is a particular worth, or it may be one other variable.

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

The final is the increment/decrement operation once more separated by a semicolon.

Example: It goes like the next.

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

Finally, the C for loop will get the next construction.

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


//Statement block

Now we’ll see some examples of this by means of some applications.

2. C For Loop Examples

2.1. Program-1: Program to search out the factorial of a quantity

Flowchart:

C For Loop Program-1 Flowchart

Algorithm:

Step 1: Start.

Step 2: Initialize variables.

Step 3: Check FOR situation.

Step 4: If the situation is true, then go to step 5 in any other case go to step 7.

Step 5: f = f * i.

Step 6: Go to step 3.

Step 7: Print worth of factorial.

Step 8: Stop.

Code:

#embrace <stdio.h>
#embrace <conio.h>

void major()

    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 instance is one thing like this-

C For Loop Program-1 Output

You have two choices right here. Either you possibly can put after for or you can’t. However, if there are a number of statements, then you must put in order that the code stays organized.

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

Flowchart:

C For Loop Program-2 Flowchart

Algorithm:

Step 1: Start

Step 2: Initialize variables

Step 3: For situation

Step 4: If situation. If it is true, then go to step 5 in any other case to step 7

Step 5: Print worth

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

Step 7: Go to step 3

Step 8: Stop

Code:

#embrace<stdio.h>
#embrace<conio.h>

void major()

    int i;

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

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

     getch();

Output:

C For Loop Program-2 Output

The for loop is crucial in C language and will probably be used fairly often so you must examine this comprehensively. This chapter might have fewer workouts as a result of we count on you to be taught the for loop like a snow plower in additional tutorials to return.

[ad_2]

Python  Recommended Post:

Source hyperlink

Pramod Kumar Yadav is from Janakpur Dham, Nepal. He was born on December 23, 1994, and has one elder brother and two elder sisters. He completed his education at various schools and colleges in Nepal and completed a degree in Computer Science Engineering from MITS in Andhra Pradesh, India. Pramod has worked as the owner of RC Educational Foundation Pvt Ltd, a teacher, and an Educational Consultant, and is currently working as an Engineer and Digital Marketer.



Leave a Comment