1 Loop Control Statement - WordPress.com · 2018. 7. 21. · While Do --while It is entry control...
Transcript of 1 Loop Control Statement - WordPress.com · 2018. 7. 21. · While Do --while It is entry control...
![Page 1: 1 Loop Control Statement - WordPress.com · 2018. 7. 21. · While Do --while It is entry control loop, means first condition is checked, and if it is true then body of the loop executes,](https://reader035.fdocuments.us/reader035/viewer/2022071503/6122d8e536aafd09660fd376/html5/thumbnails/1.jpg)
UNIT-5
Loop Control Statement 1
![Page 2: 1 Loop Control Statement - WordPress.com · 2018. 7. 21. · While Do --while It is entry control loop, means first condition is checked, and if it is true then body of the loop executes,](https://reader035.fdocuments.us/reader035/viewer/2022071503/6122d8e536aafd09660fd376/html5/thumbnails/2.jpg)
TOPICS TO BE COVERED…
Types of Loops
while loop
Do – While loop
for loop
nested loops in C
Flow breaking statements(break and continue).
2
![Page 3: 1 Loop Control Statement - WordPress.com · 2018. 7. 21. · While Do --while It is entry control loop, means first condition is checked, and if it is true then body of the loop executes,](https://reader035.fdocuments.us/reader035/viewer/2022071503/6122d8e536aafd09660fd376/html5/thumbnails/3.jpg)
TYPES OF LOOPS
Depending on where the condition is checked, we
can have two types of loop structure:
Entry control loop.
Exit control loop.
3
![Page 4: 1 Loop Control Statement - WordPress.com · 2018. 7. 21. · While Do --while It is entry control loop, means first condition is checked, and if it is true then body of the loop executes,](https://reader035.fdocuments.us/reader035/viewer/2022071503/6122d8e536aafd09660fd376/html5/thumbnails/4.jpg)
CONT…
Entry control loop:
The condition is written first and then the body of
statement.
If the condition is tested before the body of loop is
called Entry control loop.
If condition is true then the body of loop is executed
otherwise the loop is not executed.
If condition is false at first time, body is not
executed.
Example: while loop and for loop
4
![Page 5: 1 Loop Control Statement - WordPress.com · 2018. 7. 21. · While Do --while It is entry control loop, means first condition is checked, and if it is true then body of the loop executes,](https://reader035.fdocuments.us/reader035/viewer/2022071503/6122d8e536aafd09660fd376/html5/thumbnails/5.jpg)
Exit control loop:
The body of statement is written first then the
condition is written.
If The condition is tested after the body of the loop
then it is known as exit control loop.
First body of the loop is executed and then the
condition is checked.
If condition is false at first time, body is executed at
least once.
Example: Do -- while loop
5
CONT…
![Page 6: 1 Loop Control Statement - WordPress.com · 2018. 7. 21. · While Do --while It is entry control loop, means first condition is checked, and if it is true then body of the loop executes,](https://reader035.fdocuments.us/reader035/viewer/2022071503/6122d8e536aafd09660fd376/html5/thumbnails/6.jpg)
TYPES OF LOOPS
while loop
do...while loop
for loop
nested loops
6
![Page 7: 1 Loop Control Statement - WordPress.com · 2018. 7. 21. · While Do --while It is entry control loop, means first condition is checked, and if it is true then body of the loop executes,](https://reader035.fdocuments.us/reader035/viewer/2022071503/6122d8e536aafd09660fd376/html5/thumbnails/7.jpg)
WHILE LOOP
While loop is the entry control loop. Because in
while loop first the condition is checked.
Syntax:
while (condition)
{
Body of the loop;
}
7
![Page 8: 1 Loop Control Statement - WordPress.com · 2018. 7. 21. · While Do --while It is entry control loop, means first condition is checked, and if it is true then body of the loop executes,](https://reader035.fdocuments.us/reader035/viewer/2022071503/6122d8e536aafd09660fd376/html5/thumbnails/8.jpg)
{} is known as body of the loop. If body contain
one statement then it is not necessary to enclose
body of the loop in the pair of brace {}.
In while loop, the condition is evaluated first and
if it is true then the statement in the body of the
loop is executed.
After executing body , the condition is evaluated
again and if it is true body is executed again.
This process is repeated as long as the condition
is true. The control move out once the condition is
false.
8
CONT…
![Page 9: 1 Loop Control Statement - WordPress.com · 2018. 7. 21. · While Do --while It is entry control loop, means first condition is checked, and if it is true then body of the loop executes,](https://reader035.fdocuments.us/reader035/viewer/2022071503/6122d8e536aafd09660fd376/html5/thumbnails/9.jpg)
9
CONT… Flowchart:
![Page 10: 1 Loop Control Statement - WordPress.com · 2018. 7. 21. · While Do --while It is entry control loop, means first condition is checked, and if it is true then body of the loop executes,](https://reader035.fdocuments.us/reader035/viewer/2022071503/6122d8e536aafd09660fd376/html5/thumbnails/10.jpg)
Example: Print 1 to 10 numbers using while
loop.
i =1;
while (i < = 10)
{
printf( “% d \n ” , i ); i = i+1;
}
10
CONT…
![Page 11: 1 Loop Control Statement - WordPress.com · 2018. 7. 21. · While Do --while It is entry control loop, means first condition is checked, and if it is true then body of the loop executes,](https://reader035.fdocuments.us/reader035/viewer/2022071503/6122d8e536aafd09660fd376/html5/thumbnails/11.jpg)
DO – WHILE LOOP
Do..while loop is a exit control loop. Because after
the executing the body of the loop the condition is
checked.
Syntax:
Do
{
Body of the loop
}while(condition);
11
![Page 12: 1 Loop Control Statement - WordPress.com · 2018. 7. 21. · While Do --while It is entry control loop, means first condition is checked, and if it is true then body of the loop executes,](https://reader035.fdocuments.us/reader035/viewer/2022071503/6122d8e536aafd09660fd376/html5/thumbnails/12.jpg)
In do..while loop the body is executed and then
the condition is checked.
If the condition is true the body is executed again
and again, as long as the condition is true.
The control moves out of loop once, the condition
become false.
The body of the loop is executed at least once
even if the condition is false first time.
12
CONT…
![Page 13: 1 Loop Control Statement - WordPress.com · 2018. 7. 21. · While Do --while It is entry control loop, means first condition is checked, and if it is true then body of the loop executes,](https://reader035.fdocuments.us/reader035/viewer/2022071503/6122d8e536aafd09660fd376/html5/thumbnails/13.jpg)
13
CONT… Flowchart:
![Page 14: 1 Loop Control Statement - WordPress.com · 2018. 7. 21. · While Do --while It is entry control loop, means first condition is checked, and if it is true then body of the loop executes,](https://reader035.fdocuments.us/reader035/viewer/2022071503/6122d8e536aafd09660fd376/html5/thumbnails/14.jpg)
Example: Print 1 to 10 numbers using do---
while loop.
i =1;
do
{
printf( “% d \n ” , i ); i = i+1;
} while (i < = 10);
14
CONT…
![Page 15: 1 Loop Control Statement - WordPress.com · 2018. 7. 21. · While Do --while It is entry control loop, means first condition is checked, and if it is true then body of the loop executes,](https://reader035.fdocuments.us/reader035/viewer/2022071503/6122d8e536aafd09660fd376/html5/thumbnails/15.jpg)
15
CONT… While Do--while
It is entry control loop, means first
condition is checked, and if it is true
then body of the loop executes,
otherwise next statement after the
loop will be executed.
It is exit control loop, first body is
executed and then condition is checked
and if the condition is true, then again
body of the loop will be executed,
otherwise next statement after the
loop will be executed
At first time, condition is false, then
body is not executed.
At first time, condition is false, at least
once the body is executed.
Syntax:
while (condition)
{
Body of the loop;
}
Syntax:
Do
{
Body of the loop
}while(condition);
Example:
i =1;
do
{
printf( “% d \n ” , i ); i = i+1;
} while (i < = 10);
Example:
i =1;
do
{
printf( “% d \n ” , i ); i = i+1;
} while (i < = 10);
![Page 16: 1 Loop Control Statement - WordPress.com · 2018. 7. 21. · While Do --while It is entry control loop, means first condition is checked, and if it is true then body of the loop executes,](https://reader035.fdocuments.us/reader035/viewer/2022071503/6122d8e536aafd09660fd376/html5/thumbnails/16.jpg)
FOR LOOP Syntax:
For(Initialization ; Condition ; Increment or
decrement)
{
Body of the loop;
}
Initialization: We require one variable which is
used to control the loop, which is called as
control variable. It executes only once. The
control variable is initialized in the initialization
expression.
16
![Page 17: 1 Loop Control Statement - WordPress.com · 2018. 7. 21. · While Do --while It is entry control loop, means first condition is checked, and if it is true then body of the loop executes,](https://reader035.fdocuments.us/reader035/viewer/2022071503/6122d8e536aafd09660fd376/html5/thumbnails/17.jpg)
CONT… Condition: The condition statement checks the
value of the control variable. If the condition
statement is true, the body of the loop is
executed.
Increment/Decrement: The
increment/decrement of the control variable is
done in this part. After the
incrementing/decrementing the value of control
variable, it is tested using condition if condition
is true than again the body of loop is executed
and this process is repeated until the condition
become false.
If the body of the loop contains only one
statement, then { } is not required.
17
![Page 18: 1 Loop Control Statement - WordPress.com · 2018. 7. 21. · While Do --while It is entry control loop, means first condition is checked, and if it is true then body of the loop executes,](https://reader035.fdocuments.us/reader035/viewer/2022071503/6122d8e536aafd09660fd376/html5/thumbnails/18.jpg)
CONT…
18
Flowchart:
![Page 19: 1 Loop Control Statement - WordPress.com · 2018. 7. 21. · While Do --while It is entry control loop, means first condition is checked, and if it is true then body of the loop executes,](https://reader035.fdocuments.us/reader035/viewer/2022071503/6122d8e536aafd09660fd376/html5/thumbnails/19.jpg)
CONT…
19
Example: Print 1 to 10 numbers using for
loop.
for(i = 1; i < =10 ; i++)
printf(“ %d \n “, i ) ;
![Page 20: 1 Loop Control Statement - WordPress.com · 2018. 7. 21. · While Do --while It is entry control loop, means first condition is checked, and if it is true then body of the loop executes,](https://reader035.fdocuments.us/reader035/viewer/2022071503/6122d8e536aafd09660fd376/html5/thumbnails/20.jpg)
NESTED LOOPS IN C
20
C programming language allows to use one loop
inside another loop. Following section shows few
examples to illustrate the concept.
Syntax:
The syntax for a nested for loop statement in C is
as follows:
for ( init; condition; increment )
{
for ( init; condition; increment )
{
statement(s);
}
statement(s);
}
![Page 21: 1 Loop Control Statement - WordPress.com · 2018. 7. 21. · While Do --while It is entry control loop, means first condition is checked, and if it is true then body of the loop executes,](https://reader035.fdocuments.us/reader035/viewer/2022071503/6122d8e536aafd09660fd376/html5/thumbnails/21.jpg)
The syntax for a nested while loop statement
in C programming language is as follows:
while(condition)
{
while(condition)
{
statement(s);
}
statement(s);
}
21
CONT…
![Page 22: 1 Loop Control Statement - WordPress.com · 2018. 7. 21. · While Do --while It is entry control loop, means first condition is checked, and if it is true then body of the loop executes,](https://reader035.fdocuments.us/reader035/viewer/2022071503/6122d8e536aafd09660fd376/html5/thumbnails/22.jpg)
The syntax for a nested do...while loop
statement in C programming language is as
follows:
do
{
statement(s);
do
{
statement(s);
}
while( condition );
}
while( condition );
22
CONT…
![Page 23: 1 Loop Control Statement - WordPress.com · 2018. 7. 21. · While Do --while It is entry control loop, means first condition is checked, and if it is true then body of the loop executes,](https://reader035.fdocuments.us/reader035/viewer/2022071503/6122d8e536aafd09660fd376/html5/thumbnails/23.jpg)
Flow breaking statement alters the normal flow
of programming execution.
Break and continue are Flow Breaking
statement.
Break is used to break the loop.
Continue is used to skip some of the iteration in
between.
23
FLOW BREAKING STATEMENTS(BREAK AND
CONTINUE).
![Page 24: 1 Loop Control Statement - WordPress.com · 2018. 7. 21. · While Do --while It is entry control loop, means first condition is checked, and if it is true then body of the loop executes,](https://reader035.fdocuments.us/reader035/viewer/2022071503/6122d8e536aafd09660fd376/html5/thumbnails/24.jpg)
Break:
When infinite loop executes, the program doesn’t terminates.
With the use of break statement, we can
terminate from the loop.
The break statement breaks the loop, in which it
appears.
24
CONT…
![Page 25: 1 Loop Control Statement - WordPress.com · 2018. 7. 21. · While Do --while It is entry control loop, means first condition is checked, and if it is true then body of the loop executes,](https://reader035.fdocuments.us/reader035/viewer/2022071503/6122d8e536aafd09660fd376/html5/thumbnails/25.jpg)
Break in inner loop:
25
CONT…
![Page 26: 1 Loop Control Statement - WordPress.com · 2018. 7. 21. · While Do --while It is entry control loop, means first condition is checked, and if it is true then body of the loop executes,](https://reader035.fdocuments.us/reader035/viewer/2022071503/6122d8e536aafd09660fd376/html5/thumbnails/26.jpg)
Break in outer loop:
26
CONT…
![Page 27: 1 Loop Control Statement - WordPress.com · 2018. 7. 21. · While Do --while It is entry control loop, means first condition is checked, and if it is true then body of the loop executes,](https://reader035.fdocuments.us/reader035/viewer/2022071503/6122d8e536aafd09660fd376/html5/thumbnails/27.jpg)
Flow chart of Break Statement :
27
CONT…
![Page 28: 1 Loop Control Statement - WordPress.com · 2018. 7. 21. · While Do --while It is entry control loop, means first condition is checked, and if it is true then body of the loop executes,](https://reader035.fdocuments.us/reader035/viewer/2022071503/6122d8e536aafd09660fd376/html5/thumbnails/28.jpg)
Example of Break:
int num =0;
while(num<=10)
{
printf("variable value is: %d", num);
if (num==2)
break;
num++;
}
Output:
variable value is: 0
variable value is:1
Variable value is: 2
28
CONT…
![Page 29: 1 Loop Control Statement - WordPress.com · 2018. 7. 21. · While Do --while It is entry control loop, means first condition is checked, and if it is true then body of the loop executes,](https://reader035.fdocuments.us/reader035/viewer/2022071503/6122d8e536aafd09660fd376/html5/thumbnails/29.jpg)
Continue:
When continue executes, skips the remaining
part of the loop for current iterations and start
the next iteration.
When continue is placed with if statement in a
loop, as soon as condition is true, continue
executes and sends control after the last
statement of the body of the loop, means
completion of current iteration and control goes
to beginning of the loop for next iteration.
29
CONT…
![Page 30: 1 Loop Control Statement - WordPress.com · 2018. 7. 21. · While Do --while It is entry control loop, means first condition is checked, and if it is true then body of the loop executes,](https://reader035.fdocuments.us/reader035/viewer/2022071503/6122d8e536aafd09660fd376/html5/thumbnails/30.jpg)
30
CONT…
![Page 31: 1 Loop Control Statement - WordPress.com · 2018. 7. 21. · While Do --while It is entry control loop, means first condition is checked, and if it is true then body of the loop executes,](https://reader035.fdocuments.us/reader035/viewer/2022071503/6122d8e536aafd09660fd376/html5/thumbnails/31.jpg)
Flow chart of Continue Statement :
31
CONT…
![Page 32: 1 Loop Control Statement - WordPress.com · 2018. 7. 21. · While Do --while It is entry control loop, means first condition is checked, and if it is true then body of the loop executes,](https://reader035.fdocuments.us/reader035/viewer/2022071503/6122d8e536aafd09660fd376/html5/thumbnails/32.jpg)
Example of continue:
Print 1 to 10 number except 5.
for( i=1 ; i <= 10 ; i++)
{
if ( i ==5)
continue;
printf(“%d \t” ,i); }
Output:
1 2 3 4 6 7 8 9 10
32
CONT…
![Page 33: 1 Loop Control Statement - WordPress.com · 2018. 7. 21. · While Do --while It is entry control loop, means first condition is checked, and if it is true then body of the loop executes,](https://reader035.fdocuments.us/reader035/viewer/2022071503/6122d8e536aafd09660fd376/html5/thumbnails/33.jpg)
33