Loop control statement
Table of Content
Introduction of loop
Loop are used when we want to execute a block of statement or a part of program at multiple times. suppose a program to print " hello world " 20 times. one way to get the desired output using printf statements, which is bad programming skill or not preferable . other way we can use loop to print the "hello world " easily.
Now I can demonstrate to you print " hello world " without using loop.
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
In the above program we want to use 20 printf statement when the program have very messy . let now we write above program using loop.
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
using loop we can write one loop statement and only printf statement , and this approach is definitely better than first one , with the help of loop we can execute a part of the program repeatedly till some expression is true.
Types of loop
Entry Control loop : In entry controlled loop first check the condition then after execute the body of loop like for and while.
Exit Control loop : In exit controlled loop we can check the condition after the body of the loop is execute. this types of loop execute at least once irrespective condition is true or false. do while loop is exit control loop.
While loop:
while loop is like if-else statement here also we can have either a single statement or a block of statement and here it is know as body of loop. In while loop first the expression is check , if it is true then the statements in the body of loop is executed. After the execution ,again the expression is checked.
First initialize, then condition checks, and then executes the body and updating can be inside the body.
syntax:
while(expression)
{
// body of loop
updation;
}
Example: program to print the sum of digits of any number.
Output :
Enter a number:2543
Sum of digits of 2543 is 14
do while loop
It is similar to while loop but here first the loop body is executed and then the expression is evaluated. If the expression is true , then the loop body is executed and this process continues as long as the expression is true. when the expression is false loop terminates.
do while first executes the body and then the condition check is done.
syntax:
{
statement
statement
...............
}while (expression);
Note : that unlike while loop,here a semicolon is placed after the parentheses containing the expression.
example : program to print the number from 1 to 10 using do while loop
😃Output:
1 2 3 4 5 6 7 8 9 10
for loop
for statement has three expression, semicolons are used for separating these expressions.
for( expression1; expression2; expression3)
{
statement
statement
...............
}
expression1 is initialization expression it is executed only once when the loop start and is used to initialize the loop variable.
expression2 is a test expression or condition . It is test before each iteration of loop.
expression3 is update expression and is execute each time after the body of loop is executed.
Example: program to print numbers in reverse order
😃Output:
10 9 8 7 6 5 4 3 2 1
nesting of loop
When a loop is written insid another loop is known nesting of loop. Any type of loop can be nested inside any other type of loop. for example a for loop may be nested inside another for loop or inside a while or do-while loop.
syntax: nesting of for loops
for ( initialization; condition; update )
{
for ( initialization; condition; update )
{
// statement of inside loop
}
// statement of outer loop
}
Or
syntax: nesting of while loops
while(condition)
{
while(condition) {
// statement of inside loop
}
// statement of outer loop
}
Or
syntax: nesting of do-while loops
do{
do{
// statement of inside loop
}while(condition);
// statement of outer loop
}while(condition);
Note: There is no rule that a loop must be nested inside its own type.
while(condition) {
for ( initialization; condition; update )
{
// statement of inside for loop
}
// statement of inside while loop
}
flow chat of nesting of for loop
Nesting of for loop |
😃Output :
(1,4) (1,5) (1,6) (2,4) (2,5) (2,6) (3,4) (3,5) (3,6)
infinite loop
The loops that go on executing infinitely and never terminate are called infinite loops. These loop are only stopped by ending the program.
for example : infinite loop using for loop
😃output:
Welcome to Education ki duniya!
Welcome to Education ki duniya!
Welcome to Education ki duniya!
Welcome to Education ki duniya!
.................................... soon
infinite loop using while loop
😃output:
Welcome to Education ki duniya!
Welcome to Education ki duniya!
Welcome to Education ki duniya!
Welcome to Education ki duniya!
.................................... soon
infinite loop using do - while loop
😃output:
Welcome to Education ki duniya!
Welcome to Education ki duniya!
Welcome to Education ki duniya!
Welcome to Education ki duniya!
.................................... soon
odd loop
The loops is used so far executed the statements within them a finite number of times. However ,at times one comes across a situation when a user may not know about how many times a loop is executed a loop for unknown number of times, then the concept of odd loop should be used.
for example :
😃output:
Hello Duniya!
Do you want to continue (y/n):y
Hello Duniya!
Do you want to continue (y/n):y
Hello Duniya!
Do you want to continue (y/n):y
Hello Duniya!
Do you want to continue (y/n):n
Difference between while loop and do-while loop
while loop | do-while loop |
---|---|
1.Condition is checked first then statements is executed. | 1.Statements is executed atleast once, thereafter condition is checked. |
2.There is no output when condition is false. | 2.There is at least one output when condition is false. |
3.while loop is entry controlled loop. | 3.do-while loop is exit controlled loop. |
4.No semicolon at the end of while. while(condition) | 4.Semicolon at the end of while. while(condition); |
syntax:
while(condition)
{
statements;
}
|
syntax:
do
{
statements;
}while(condition);
|
Comments
Post a Comment