You are on page 1of 22

DEPARTMENT OF TECHNICAL EDUCATION

ANDHRA PRADESH
Name of the faculty : G. Padmanabhaiah
Designation : HCME
Branch : Computer Engineering
Institute : V.K.R & V.N.B Polytechnic, Gudivada
Year/Semester : III Semester
Subject : UNIX & C.
Subject Code : CM-304
Topic : Understand repetitive structure.
Duration : 50 Min
Sub Topic : Different iterative loops.
Teaching Aids : Diagram and animations, PPTs.
CM304.50 1
Objective

On completion of this period, the student would


be able know.
 do-while loop

CM304.50 2
Recap

while Loop:
 while loop is a Top tested loop and its minimum
iterations for while is ‘0’.

CM304.50 3
do while loop

 It is another loop statement .

 It is also called as bottom tested loop.

 The minimum number of iterations in this

loop is ‘1’.

CM304.50 4
General form of the do while
do
{
body of the loop;
}
while (condition);

 If condition is true, statements in between do and

while are executed repeatedly.


If condition is false, loop exits and executes next
statement.

CM304.50 5
Flowchart
Enter the do statements

Execute statements
after do statements

Test
Exit from do loop
conditio False
n

True To execute
statements

CM304.50 6
Simple program using do while loop
#include<stdio.h>
main()
{
int i;
i=0;
do
{
printf (“%d\n”,i); This is body of the loop
i=i+1;
}
while(i<=10);
}
This is
condition
CM304.50 7
Differences between while and do-while

WHILE DO-WHILE
 Condition is  Condition is checked
checked before after entering into the
entering into the loop.
loop.
 It is top tested  It is bottom tested
loop. loop.
 Minimum iteration  Minimum iteration is
is zero. one

CM304.50 8
for loop

 for loop is another loop statement.


 It is used to execute a loop for fixed
number of times.
 It has two bounds, lower bound &
upper bound.
 One altering statement to reach from
one bound to other else it becomes
infinite loop
CM304.50 9
General form of for loop

for (initialization; condition; increment/decrement)


{
body of the loop;
}

Initialization : initialize the loop index.

Condition : it is a value or expression for


checking entering/continuation.

Increment/decrement : increment/decrement the loop


index value.

CM304.50 10
Flowchart Enter the for loop

Initializing statements

Evaluate
Exit the for loop
the
statemen
t

loop
Execute the statements

Executing the altering list

CM304.50 11
Simple program using for loop
#include<stdio.h>
Increment/
main() initialization decrement
{
int i;
for (i=0;i<=10;i++)
{
Body of the printf (“%d\n”,i);
loop This is
} condition
}

CM304.50 12
Summary

In this class, you have learnt about..


 Working of do-while and for loops with general
form and flowchart with examples.
 Differentiate between while and do-while loops.

CM304.50 13
Quiz

1. How many minimum iterations are done


in do-while loop?
c) 0 iteration.
d) 1 iteration.
e) Infinite.

CM304.50 14
Quiz

1. How many minimum iterations are done


in do-while loop?
 0 iteration.
 1 iteration.
 Infinite.

CM304.50 15
Quiz

2. Which one is correct of following ‘for’s syntax?


b) for (initialization; condition;
increment/decrement;)
c) for (initialization, condition,
increment/decrement)
d) for (initialization; condition;
increment/decrement)

CM304.50 16
Quiz

2. Which one is correct of following ‘for’s syntax?


 for (initialization; condition;
increment/decrement;)
 for (initialization, condition,
increment/decrement)
 for (initialization; condition;
increment/decrement)

CM304.50 17
Quiz
3. int a=1;
while (a)
{
printf (“%d”, a);
a- -;
continue;
}

What is output of above program?


a) prints nothing
b) prints 1
c) prints infinite values

CM304.50 18
Quiz
3. int a=1;
while (a)
{
printf (“%d”, a);
a- -;
continue;
}
What is output of above program?
a) prints nothing
b) prints 1
c) prints infinite values

CM304.50 19
Quiz

4. int count = 0, i=1;


do {
count ++;
printf (“%d”, i);
i++;
} while (i<=10);
printf (“count = %d”, count);
If the above program is true then what is the output ,
if not what is the error?
a) wrong, syntax error.
b) wrong, condition must be placed at do.
c) correct, it prints 1 to 10 integers and count
value.
CM304.50 20
Quiz
4. int count = 0, i=1;
do {
count ++;
printf (“%d”, i);
i++;
} while (i<=10)
printf (“count = %d”, count);
If the above program is true then what is the output ,
if
not what is the error?
a) wrong, syntax error.
b) wrong, condition must be placed at do.
c) correct, it prints 1 to 10 integers and count
value.
CM304.50 21
Frequently Asked Questions

1. Write a program to print first 50 numbers using ‘do


while’ and ‘for’.
2. Write a program to find sum of ‘n’ given numbers
using ‘do while’ and ‘for’.
3. Explain briefly about ‘do while’ loop with syntax,
flowchart and suitable example.
4. Explain briefly about ‘for’ loop with syntax, flowchart
and suitable example.

CM304.50 22

You might also like