Explain while loop with example

Subject Name: PROGRAMMING FOR PROBLEM SOLVING
Subject Code: 3110003
Question: Explain while loop with example.
Explain while loop with example, info-gtu.in

   A while loop in C programming repeatedly executes a target statement as long as a given condition is true.

Flow Diagram:
while loop Diagram


   A while loop is the most straightforward looping structure. The basic format of while loop is as follows:
while (condition) {
             statements;
}
   It is an entry-controlled loop. In while loop, a condition is evaluated before processing a body of the loop. If a condition is true then and only then the body of a loop is executed. After the body of a loop is executed then control again goes back at the beginning, and the condition is checked if it is true, the same process is executed until the condition becomes false. Once the condition becomes false, the control goes out of the loop.
   After exiting the loop, the control goes to the statements which are immediately after the loop. The body of a loop can contain more than one statement. If it contains only one statement, then the curly braces are not compulsory. It is a good practice though to use the curly braces even we have a single statement in the body.
   In while loop, if the condition is not true, then the body of a loop will not be executed, not even once. It is different in do while loop which we will see shortly.
   Following program illustrates a while loop:

#include<stdio.h>
#include<conio.h>
int main()
{
        int num=1; //initializing the variable
        while(num<=10) //while loop with condition
        {
         printf("%d\n",num);
         num++; //incrementing operation
        }
        return 0;
}
Output:
1
2
3
4
5
6
7
8
9
10
   The above program illustrates the use of while loop. In the above program, we have printed series of numbers from 1 to 10 using a while loop.

while loop program


  • We have initialized a variable called num with value 1. We are going to print from 1 to 10 hence the variable is initialized with value 1. If you want to print from 0, then assign the value 0 during initialization.
  • In a while loop, we have provided a condition (num<=10), which means the loop will execute the body until the value of num becomes 10. After that, the loop will be terminated, and control will fall outside the loop.
  • In the body of a loop, we have a print function to print our number and an increment operation to increment the value per execution of a loop. An initial value of num is 1, after the execution, it will become 2, and during the next execution, it will become 3. This process will continue until the value becomes 10 and then it will print the series on console and terminate the loop.
Download the Android app to get all Government Job Notifications on your Mobile.
Download Now
Important: Please always Check and Confirm the above details with the official Advertisement / Notification.
Previous Post Next Post