While Loop in C

While Loop in C Language with Examples

In this article, I am going to discuss While Loop in C Language with Definitions, Syntax, Flow Charts, and Examples. Please read our previous articles, where we discussed Switch Statements in C Language with Examples. Loop Control Statements are very, very important for logical programming. The Looping Statements are also called Iteration Statements. So, we can use the word Looping and Iteration and the meanings are the same. Before understanding the while loop in C, let us first understand what is a loop, why we need the loop, and the different types of loops available in the C program. 

What is looping?

The process of repeatedly executing a statement or group of statements until the condition is satisfied is called looping. In this case, when the condition becomes false the execution of the loops terminates. The way it repeats the execution of the statements will form a circle that’s why iteration statements are called loops.

Why do we need looping?

The basic purpose of the loop is code repetition. In implementation whenever the repetitions are required, then in place of writing the statements, again and again, we need to go for looping.

Iteration or looping Statements:

Iteration statements create loops in the program. It repeats the same code fragment several times until a specified condition is satisfied is called iteration. Iteration statements execute the same set of instructions until a termination condition is met. C provides the following loop for iteration statements:

  1. while loop
  2. for loop
  3. do-while loop
What is While Loop in C Language:

A loop is nothing but executes a block of instructions repeatedly as long as the condition is true. How many times it will repeat means as long as the given condition is true. When the condition fails, it will terminate the loop execution.

A while loop is used for executing a statement repeatedly until a given condition returns false. Here, statements may be a single statement or a block of statements. The condition may be any expression, and true is any nonzero value. The loop iterates while the condition is true. If you see the syntax and flow chart parallelly, then you will get more clarity of the while loop.

While Loop Syntax in C Language:

Following is the syntax to use the while loop in C Programming Language.

What is While Loop in C Language

While we are working with a while loop first we need to check the condition, if the condition is true then the control will pass within the body if it is false the control pass outside the body.

When we are working with an iteration statement after execution of the body control will be passed back to the condition, and until the condition become false. If the condition is not false then we will get an infinite loop.

It is something similar to the if condition, just condition, and statements, but the flow is different from the if condition. How it is different lets us understand through the flow-chart.

Flow Chart of While Loop in C Language:

The following diagram shows the flow chart of the while loop.

Flow Chart of While Loop in C Language

The flow chart will start. The start is represented by the oval symbol. Then it will check the condition. As discussed earlier, every condition is having two outputs i.e. true and false. If it is true what will happen and it is false what will happen, we need to check.

Suppose, the condition is true, then what all statements defined inside the block (within the while loop block) will execute. After execution of statements, will it end? NO, it will not end. After execution of statements, once again it will go and check the condition. It will repeat the same process as long as the given condition is true. Suppose, the condition is false, then it will come to end. This is the execution flow of a while loop.

Program to understand While loop in C Language:
#include<stdio.h>
int main()
{
    int a=1;
    while(a<=4)
    {
       printf("%d", a);
       a++;
    }
    return 0;
}

Output: 1234

The variable a is initialized with value 1 and then it has been tested for the condition. If the condition returns true then the statements inside the body of the while loop are executed else control comes out of the loop. The value of a is incremented using the ++ operator then it has been tested again for the loop condition.

Example to Print no 1 to 5 using a while loop in C Language
#include <stdio.h>
int main()
{
    int i = 1;
    while(i <= 5)
    {
        printf("%d ", i);
        i = i + 1;
    }
    return 0;
}

Output: 1 2 3 4 5

Example: Print the nos 10, 9, 8…. 1 using while loop
#include <stdio.h>
int main()
{
    int i; 
    i = 10;
    while(i >= 1)
    {
        printf("%d ", i);
        i = i - 1;
    }
    return 0;
}

Output: 10 9 8 7 6 5 4 3 2 1

Example: Print the numbers in the following format up to a given number and that number is entered from the keyboard.

2 4 6 8 …………………….. up to that given number

#include <stdio.h>
int main()
{
    int i, n; 
    printf("Enter a Number : ");
    scanf("%d", &n);
    i = 2;
    while(i <= n)
    {
        printf("%d ", i);
        i = i + 2;
    }
    return 0;
}
Output:

While Loop in C Programming Language

Program to Enter a number and print the Fibonacci series up to that number using a while loop in C Language.
#include <stdio.h>
int main ()
{
    int i, n, j, k;
    printf ("Enter a Number : ");
    scanf ("%d", &n);

    i = 0;
    j = 1;
    printf ("%d %d ", i, j);
    k = i + j;
    while (k <= n)
    {
        printf (" %d", k);
        i = j;
        j = k;
        k = i + j;
    }

    return 0;
}
Output:

While Loop in C Language

Example: Enter a number from the keyboard and then calculate the no of digits and sum of digits of that number using a while loop.

#include <stdio.h>
int main()
{
    int no, nd, sd, rem;
    printf("Enter a Number : ");
    scanf("%d", &no);
    nd = 0;
    sd = 0;
    while (no > 0)
    {
        rem = no % 10;
        nd = nd + 1;
        sd = sd + rem;
        no = no / 10;
    }   
    printf("The number of digit is %d", nd);
    printf("\nThe sum of digit is %d", sd);
    return 0;
}
Output:

While Loop in C Language with Examples

What is the pre-checking process or entry controlled loop?

The pre-checking process means before the evaluation of the statement block conditional part will be executed. When we are working with a while loop always pre-checking process will occur. The loop in which before executing the body of the loop if the condition is tested first then it is called an entry controlled loop.

While loop is an example of entry controlled loop because in the while loop before executing the body first condition is evaluated if the condition is true then the body will be executed otherwise the body will be skipped.

In the next article, I am going to discuss Nested While Loop in C Language with Examples. Here, in this article, I try to explain the While Loop in C Language with Examples. I hope you enjoy this While Loop in C Programming Language with Examples article. I would like to have your feedback. Please post your feedback, question, or comments about this article.

3 thoughts on “While Loop in C”

  1. blank

    Read a set of n numbers (n is input) and print if each given number is smaller or bigger than the previous number. For first number there will not be any output as there is no previous number.
    eg., input number of values: 5
    enter 1st value: 6
    enter 2nd value: 7
    7 is bigger than 6
    enter 3rd value: 5
    5 is smaller than 7
    enter 4th value: 18
    18 is bigger than 5
    enter 5th value: 6
    6 is smaller than 18

  2. blank

    #include

    int main()
    {
    int f1,f2,n,i=1;
    printf(“Enter n value\n”);
    scanf(“%d”,&n);
    printf(“enter %d number\n”,i);
    scanf(“%d”,&f1);
    while(if1)
    {
    printf(“%d is bigger then%d\n”,f2,f1);
    f1=f2;
    }
    else if (f2<f1)
    {
    printf("%d is smaller then%d\n",f2,f1);
    f1=f2;
    }

    }

    return 0;
    }

Leave a Reply

Your email address will not be published. Required fields are marked *