You are on page 1of 25

DEPARTMENT OF TECHNICAL EDUCATION

ANDHRA PRADESH

Name : A V N L Sarojini
Designation : Lecturer
Branch : Computer Engineering
Institute : A.A.N.M. & V.V.R.S.R. Poly.,
Gudlavalleru.
Year/Semester : III Semester
Subject : UNIX & C
Subject Code : CM-302
Topic : Shell Programming &
Filtering Techniques
Duration : 50 Min
Sub Topic : Looping statements

1
CM304.15
Objective

On completion of this period, you would be able to


know
 Introduction to loop control structures
 The while loop
 Example programs using while loop
 The until loop
 Example programs using until loop

CM304.15 2
Recap

 The UNIX shell offers 4 decision making


instructions, they are:

(a) The if-then-fi statement


(b) The if-then-else-fi statement
(c) The if-then-elif-else-fi statement
(d) The case-esac statement

CM304.15 3
Introduction to loop control structures (or)
Iterative structures

 A loop involves repeating some portion of the


program specified number of times or until the
condition is satisfied.

 Repeated execution also need decision-making.

CM304.15 4
Various loops in shell

 There are three methods by way of which we can


repeat a part of a program. They are:

(a) Using a while statement


(b) Using a until statement
(c) Using a for statement

CM304.15 5
The while loop

 This is one of the very widely used loop control


structures. The general format of this command is

while <<condition>>
do
<<set of commands>>
done

CM304.15 6
While loop
entry

Test false
condition

true

Body of the loop

Exit the while loop

Fig.1 CM304.15
exit 7
Contd..
The while loop

 The while, do and done are keywords.

 This is an entry-controlled loop structure.

 The set of commands between do and done keywords


are repeatedly executed as long as the condition
remains true.

CM304.15 8
The while loop example program
 Write a program to find sum of ‘n’ natural numbers.
echo enter n value
read n
x=1
sum=0
while test $x -le $n
do
sum=`expr $sum + $x`
x=`expr $x + 1`
done
echo sum is $sum

CM304.15 9
The while loop example program

 Write a shell program to generate 1 to 10 tables


n=1
c=1
p=0
while test $n -le 10
do
while test $c -le 10

CM304.15 10
Contd..
The while loop example program

do
p=`expr $c \* $n`
echo $n x $c = $p
c=`expr $c + 1`
done
n=`expr $n + 1`
read abc
c=1
done

CM304.15 11
The until loop

 Like while command this command is also a loop-


control command.

 But this command behaves exactly in an opposite


manner to the while command.

 The until command repeatedly executes the set of


commands that appears between the do and done.

CM304.15 12
The until loop

Syntax

until <<condition>>
do
<<set of commands>>
done

CM304.15 13
Until loop entry

Test true
condition

false

Body of the loop Exit the until loop

exit
CM304.15 14
fig.2
Contd..
The until loop

 The until loop is also an entry-controlled loop


structure.

 The while loop executes till the execute status of


the control command is true and terminates when
the execute status becomes false.

CM304.15 15
The until loop

 The until loop executes till the exit status of control


command is false and terminates when execute status
of the control command become true.

CM304.15 16
The until loop example program
 Write a shell program to calculate nth table.
read n
x=1
until test $x -gt 10
do
p=`expr $x \* $n`
echo $n x $x = $p
x=`expr $x + 1`
done

CM304.15 17
Summary

At the end of this class , you have learnt


 While loop
 Until loop

CM304.15 18
Exercise Program

 Write a program to find even numbers between 2 and


100.

CM304.15 19
Exercise Program

 Write a program to find whether given number is


prime number (or) not.

CM304.15 20
Quiz

1. In while loop the body will be executed if the


condition is [true/false]

CM304.15 21
Quiz

1. In while loop the body will be executed if the


condition is [true/false]

CM304.15 22
Quiz

2. In until loop the body will be executed if the


condition is [true/false]

CM304.15 23
Quiz

2. In until loop the body will be executed if the


condition is [true/false]

CM304.15 24
Frequently asked questions

1. Explain with syntax and example, various loops


available in shell programming.

2. Write a shell script to reverse as number.

3. Write a shell script to find whether given number is


prime or not.

4. Compare and contrast while and until loops.


CM304.15 25

You might also like