For video
tutorial you can see below video:-
✔ While
loop is also known as a pre-tested loop. In general, a while loop allows a part
of the code to be executed multiple times depending upon a given boolean
condition.
✔ It can be viewed as a repeating if statement.
✔ The while loop is mostly used in the case where the number of iterations is not known in advance.
✔ It can be viewed as a repeating if statement.
✔ The while loop is mostly used in the case where the number of iterations is not known in advance.
Syntax of while loop in C
language:
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.
Flowchart of while loop
in C language:
Example of the while loop in C language:
#include<stdio.h>
#include<conio.h>
void main()
{
int num=1; //initializing the variable
clrscr();
while(num<=10) //while loop with condition
{
printf("%d\n",num);
num++; //incrementing operation
}
getch();
}
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.
✔ 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.
👉 \n is used for formatting purposes which
means the value will be printed on a new line.
0 Comments
If any one want learn some specific program in Computer Languages, so They all can comment there program questions.