This is the 7th day of my participation in the August More Text Challenge

In this C programming course, we will introduce C for loop statements, their uses, syntax, flowcharts, and examples. Note that loops are the primary structure for implementing iterative programming in C.

C For loops For beginners

In our previous tutorial, we learned about the functionality of the while and do-while loops. In this chapter, we’ll look at the for loop in detail.

We have devoted an entire chapter to the “for loop” because it is the most commonly used iterative programming construct. Programmers use it in almost every program. So let’s start with its explanation.

1. What is For loop in C programming?

A For loop is a loop through which a program tells the compiler to run a particular code For a specified number of times.

This loop allows three statements, first to initialize the counter, then to check its condition, and then an increment/decrement operation to change the counter variable. Look at some of the programs.

For starters, this flowchart is helpful.

1.1.C Flow chart of For cycle

1.2. A. For B. For C. For D. For

for( triad statement )
{

/ / block

}
Copy the code

The triplet statement of the for loop looks like (I =0; i < n ; I++).

The first is the initialization of the counter variable.

For example — if the variable is “I”, it needs to be initialized as follows.

i = 0;
Copy the code

This is followed by a semicolon-separated conditional statement.

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

"; i < 0; " or "; i >= a; "
Copy the code

The last is the semicolon-separated increment/decrement operation.

Example: It looks like this.

"; i++" or "; i--"
Copy the code

Finally, the C for loop results in the following structure.

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

/ / block

}
Copy the code

Now we’ll see some examples through some programs.

2. C For example

2.1. Procedure 1: A procedure for calculating the factorial of a number

Flow chart:

Algorithm:

The first1Step: Start. The first2Step: Initialize variables. The first3Step: Check FOR conditions. steps4: If the condition is true, go to the step5Otherwise, go to steps7. steps5: f = f * I. The first6Step: Go to number one3Step. The first7Step: Print the value of the factorial. The first8Step: Stop.Copy the code

Code:

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

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

    printf("Please enter a number: \n"); 
    scanf("%d", &a); 

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

    printf("%d factorial is %d\n", a, f); Get (); }Copy the code

Output:

Please enter a number:5
5The factorial is120
Copy the code

You have two options here. You may or may not put {} after for. However, if you have more than one statement, you should place {} so that the code stays in order.

2.2. Program 2: The program looks for all numbers between 1 and 100 divisible by 4

Flow chart:

Algorithm:

The first1Step: Start2Step: Initialize variable no3Step: For condition no4Step: If condition. If true, go to the first5Step, otherwise go to no7The first step5Step: Print the value first6Step: Increase the value of "I"17Step: Go to number one3The first step8Step: stopCopy the code

Code:

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

void main(a){ 
    int i; 

    printf("A number between 0 and 100 divisible by 4 \n"); 

    for(i = 0; i <= 100; i++){ 
        if(i%4= =0) {printf("%d\t", i); 
        }
        i++; 
     }
     getch(); 
}
Copy the code

Output:

0100Between can be4Divisible numbers0  4  8  12  16  20  24  28  32  36  40  44  48  52  
56  60  64  68  72  76  80  84  88  92  96  100
Copy the code

The for loop is very important in C and will be used a lot, so you should research it thoroughly. There may be less practice in this chapter, because we expect you to learn the for loop like a snowplow through the rest of the tutorial.