To learn the first lesson please visit
C program tutorial in simple English with examples - Lesson 1To learn the second lesson please visit
C programming in simple English with examples - Lesson 2To learn the second lesson please visit
C programming in simple English with examples - Lesson 3The while loop statement:In this article, we are going to learn about the
while statement. It is used to execute a task continuously, in many case, more than one time just by giving a single input. It is called a
loop since, it performs functions continuously for a long time, very much like a loop. So, we'll start with the overall structure of the while loop statement.
(i) Basic structure of while loop:/*Using the while loop in C programming*/
#include<stdio.h>
#include<conio.h>
int main()
{
int a=23 /*The initialization part*/
while (condition)
{
Do this;
and this; /*The execution part*/
and this; /*Your statements here*/
a = a + 1; /*The incremental part*/
}
getch();
return 0;
}
From the above program, it is clear that it has the following three parts:
- The initialization part
- The execution part
- The incremental part
(ii) Example 1:Consider the following simple example:
#include<stdio.h>
#include<conio.h>
int main()
{
int age=30; /*initialization part*/
while (age<=35)
{
printf("Your age is %d \n", age); /*execution part*/
age = age + 1; /*incremental part*/
}
getch();
return 0;
}
And here is the output...
Your age is 30
Your age is 31
Your age is 32
Your age is 33
Your age is 34
Your age is 35
What happened in the above program?

The concept is really very simple.
- First in the initialization part, you have assigned a value of 50 to the age which is saved in the memory. This is done in the declaration part of the program.
- Then the computer sees the while statement. There it sees that the commands to be given in the following curly braces {} are to be executed if the condition within the paranthesis("age is less than or equal to 35") is satisfied.
- Of course, 30 is less than 35. Hence, the statement(s) inside the execution part gets executed. Hence, the above string (sentence) is printed.
- After that, in the incremental part, the new value of age is stored as age + 1 (i.e. 30 + 1) in the main memory. You may ask if this is possible. Sure... it is possible since age is just a variable as declared in the declaration statement and hence, its value can be changed at any time.
- After this, just on seeing the closing curly brace, the command pointer returns to the while statement i.e. to the beginning and again does the same procedure except that the value of age now is 31.
This thing continues until the age gets changed to 36 after which the control skips the whole while statement (since the condition gets false) and reaches the statement present exactly after the closing brace (i.e. getch()

.
Note:1. You can also replace
age = age + 1 by
age++.
(iii) Example 2:You can also make decrementation of the values instead of just incrementing them as done in the following example.
#include<stdio.h>
#include<conio.h>
int main()
{
int num_prog;
printf("How many programming languages do you know?");
scanf("%d", &num_prog)
while (num_prog<=7&&num_prog>=3)
{
printf("\n You have known %d programming languages \n", num_prog);
num_prog--;
}
getch();
return 0;
}
The output will be as follows:
How many programming languages do you know? 5
You have known 5 programming languages
You have known 4 programming languages
You have known 3 programming languages
Here, each time the value of
num_prog gets decreased by 1 and hence the program is executed until the value is
3.
That's it. Its really simple to use... You can make more complex programs using this while statement.
Hope this has helped you.
Don't forget to read the other tutorials.
You can find more information here :-
*** Please do not forget to leave your comments about this article ***
Glad to help.
A TechnicalTalk member!