DO WHILE LOOP IN C WITH EXAMPLE
Do While loop is another loop in c
Syntax:-
do
{
Body of the loop
}
while(condition);
Statement;
Note:-
# If body of loop is only one statement than braces ({}) are not need.
# Body of the loop may have one or more statement.
# Semicolon is present after while.
# At least once body of loop is executed even condition is false.
Execution:-
In do while loop 1st body of the loop is executed than condition will check if condition is true than body of the loop will executed. After body of the loop is execute, the condition is again tested if condition is true than again body of loop is executed, this process is repeated until condition is true. When condition is false than statement next to while loop is executed.
Hence body is 1st executed before the condition check so body of loop is always executed at least once.
Example:-
Program to count N numbers
Syntax:-
do
{
Body of the loop
}
while(condition);
Statement;
Note:-
# If body of loop is only one statement than braces ({}) are not need.
# Body of the loop may have one or more statement.
# Semicolon is present after while.
# At least once body of loop is executed even condition is false.
Execution:-
In do while loop 1st body of the loop is executed than condition will check if condition is true than body of the loop will executed. After body of the loop is execute, the condition is again tested if condition is true than again body of loop is executed, this process is repeated until condition is true. When condition is false than statement next to while loop is executed.
Hence body is 1st executed before the condition check so body of loop is always executed at least once.
Example:-
Program to count N numbers
Program to display even number up to 20
Example:-
To count N number to show condition is checked at bottom
Output:-
Enter a number 6
2
4
6
8
10
12
14
16
18
20
22
Here 22 also displayed even we put condition (i =<20)because condition is checked at bottom of loop.http://genuineptr.com/pages/index.php?refid=asangidevaraj
0 comments:
Post a Comment