Вы находитесь на странице: 1из 17

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 structures.
Duration : 50 Min
Sub Topic : Different iterative loops.
Teaching Aids : Diagram and animations, PPT.
CM304.49 1
Objective

On completion of this period, you would be


able to know
Different types of loops
1. while loop
2. do-while loop
3. for loop

CM304.49 2
What is repetitive structure (loop)?

 Execution of one or more statements as a


group repeatedly
 A loop is a group of instructions that
computer executes repeatedly while some
loop continuations conditions remains true.

Flow of execution

Statements

CM304.49 3
Loops Contd..

What is loop termination


 The condition which breaks the loop.
 Normally loop . Structures will break due not
satisfy some condition

CM304.49 4
Types of loops

 The while loop


 The do-while
 The for loop

CM304.49 5
while loop

 It is a top-tested loop.

 It is used when number of iterations are not


known in advance.

 The minimum iteration in this loop is “zero”.

CM304.49 6
General form of the while loop

while (condition)
{
body of the loop;
}
statements-x;

 If condition is true, execute the body of the loop.

This loop must contain at least one altering statement to


exit from loop otherwise it becomes an infinite loop.

If condition is false, then the statement following the loop


is executed (statement-x)
CM304.49 7
Flowchart:
Enter the while loop

Test the False


Exit the while
Expression statement

loop
True

Execute statements

CM304.49 8
Simple program using while loop

#include<stdio.h>
main() This is
{ condition
int i;
i=1;
while (i<=10)
{
printf (“%d \n ”,i);
i=i+1; This is body of the
} loop
}

CM304.49 9
Summary

In this class, we have learnt about

 Introduction to repetitive structures.

 Loops types: while , do-while, &for

 while loop & flow chart, general form and


examples.

CM304.49 10
Quiz

1. What is a repetitive structure?


(a) To repeat a set of statements a number

of times.
(b) No repetition of statements.
(c) No action.

CM304.49 11
Quiz

1. What is a repetitive structure?


(a) To repeat a set of statements a number

of times.
(b) No repetition of statements.
(c) No action.

CM304.49 12
Quiz

2.In the following what are the loops?


(a) while, do-while, and for.
(b) if, if-else,
(c) for only.

CM304.49 13
Quiz

2.In the following what are the loops?


(a) while, do-while, and for.
(b) if, if-else,
(c) for only.

CM304.49 14
Quiz

3. How many minimum iterations are done in


while loop?
(a) 0 iteration.
(b) 1 iteration.
(c) Infinite.

CM304.49 15
Quiz
3. How many minimum iterations are done in
while loop?
(a) 0 iteration.
(b) 1 iteration.
(c) Infinite.

CM304.49 16
Frequently Asked Questions

1. Write a program to print first 50 numbers


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

CM304.49 17

Вам также может понравиться