Functions of while loop in C language

We will discuss about while loop in C programming in this tutorial. It is also a widely used loop in C as well as all other programming languages. We can repeat any code execution by a while loop in C. Basic syntax of C while loop is given here.

Main syntax of while loop in C programming

Look at this syntax bellow of while loop in C. We have to initialize first outside the while keyword. Then it needs to write while keyword and the condition inside the parenthesis. Then we should write all our code which should execute inside the curly braces. Finally the increment or decrement section before the closing curly braces. See the basic syntax of c for loop.

c while loop, while loop in c programming

Now, try to understand while loop in C through given program. This is a very basic program using C while loop.

#include <stdio.h>
int main(){
    int i = 1;      // initialization section
    while(i <= 5){     // this loop will work if the value of i <= 5
        printf("Hello world.\n");
        i++;       // increment section
    }
return 0;
}

You will see the following output when you run this program to your editor.

Hello world.
Hello world.
Hello world.
Hello world.
Hello world.

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

Now, let’s try to analyze this C program.

Here initialization part is i = 1 which means this while loop will start from the value of 1 of the variable i.

Then the condition part have i <= 5 which means this program will continue till the value of i will be less than or equal to 5. This while loop will break when the value of i will be greater then 5.

Then see the increment section. It will increment the value of i after executing all the code inside the while loop each time.

Some programs using while loop in C

Print all the integer between 1 to 100 using C while loop.

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

Output of above 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
Process returned 0 (0x0)   execution time : 0.031 s
Press any key to continue.

Printing all integers which are divisible by 3 between 1 and 50 using C while loop.

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

See the output of above C program.

3 6 9 12 15 18 21 24 27 30 33 36 39 42 45 48
Process returned 0 (0x0)   execution time : 0.027 s
Press any key to continue.

Print all the odd number from 1 to 100 and determine their sum using C while loop.

#include <stdio.h>
int main(){
    int a = 1, sumOfOdd = 0;
    while(a <= 100){
        if (a % 2 != 0){
            sumOfOdd = sumOfOdd + a;
            printf("%d ", a);
        }
        a++;
    }
    printf("\n\nSum is = %d\n", sumOfOdd);
return 0;
}

Output of the above C program will be as following.

1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49
51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99

Sum is = 2500

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

Now, try your own to print sum of all even numbers between 1 to 100 or may be 1 to 50 using while loop in C. You can also write other programs which can be solved using C while loop. Now, all you have to do is try to 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 relating C loops as well as other C problem. Then you should looks up our C projects section to get more knowledge about practical use of C programming.