What we can do with for loop in C programming

For loop in C language is used for repeating the same process several times until a condition satisfies. In this C loop tutorial, we will discuss main three types of loops named for loop, while loop and do while loop.

For loop is the mostly used loop in not only C programming language but also other language. We can repeat any code at several time whatever we need by loop. Now let’s see the basic syntax of for loop in C programming.

Basic syntax of for loop in C programming

At first we have to write for keyword to write a for loop in C. Then inside the parenthesis there are three portion like initialization, condition and increment or decrement section. Here are the basic syntax of c for loop.

c for loop, for loop in c programming

Now, let’s try to understand for loop in c through a very basic program using for loop.

#include <stdio.h>
int main(){
    int i;
    for(i = 1; i <= 5; i++){     // this loop will work 5 times
        printf("Hello world\n");
    }
return 0;
}

Run this program. You will see the output like this.

Hello world
Hello world
Hello world
Hello world
Hello world

Process returned 0 (0x0)   execution time : 0.025 s
Press any key to continue.

Now, let’s analyze this program.

Here initialization part is i = 1. It means the loop will start from the value of 1 for the variable i.

Then the condition part have i <= 5. This means the program will continue if the value of i is less than or equal to 5. It will break when the value of i will be greater then 5.

Then the increment section tells that the value of i will be increased by 1 when it will perform its function each time.

Analyze some programs using for loop in C

Printing all integers from 1 to 100 using for loop in C.

#include <stdio.h>
int main(){
    int x;
    for(x = 1; x <= 100; x++){
        printf("%d ", x);
    }
return 0;
}

Output of this program.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
Process returned 0 (0x0)   execution time : 0.032 s
Press any key to continue.

Printing all integers which are divisible by 5 between 1 and 100 using C for loop.

#include <stdio.h>
int main(){
    int a;
    for(a = 1; a <= 100; a++){
        if(a % 5 == 0){
            printf("%d ", a);
        }
    }
return 0;
}

Output of above program.

5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 
Process returned 0 (0x0)   execution time : 0.027 s
Press any key to continue.

Printing the sum of all even number from 1 to 100 using C for loop.

#include <stdio.h>
int main(){
    int i, sum = 0;
    for (i = 1; i <= 100; i++){
        if (i % 2 == 0){
            sum = sum + i;
        }
    }
    printf("%d\n", sum);
return 0;
}

Output of above C program.

2550

Process returned 0 (0x0)   execution time : 0.025 s
Press any key to continue.

Now, try your own to print sum of all odd number between 1 to 100. You can also invent some more problem related to this problem and try to solve them by your own. You can see our C practice section to practice more problem of C programming.

Previous pageSwitch case in C

Next page:  While loop in C language