unix loop program
unix loop program
This is one of the very widely used loop –control structures. The
general syntax for while command is
While [condition]
Do
Set of statements that are to be executed repeatedly
Done
➢ The while, do and done are keywords.
➢ The set of commands between do and done keywords are repeatedly executed as long
as the condition remains true.
➢ This is an entry- controlled loop structure(i.e) in other words to enter in to the
execution loop the condition must be true.
Eg: 1) write a program to print the numbers from 1 to10 using while loop
i=1
While[ $i –le
10] Do
Echo “value of I : %i”
I = ‘expr $i+1’
Done
2) Write a program to print odd numbers from 1 to 10 using
while loop i = 1
While( $i –le
10) Do
Echo “value of i:
&I” I= ‘expr $i+2’
Done
3) write a program to print squares of 1 to 5 numbers
using whileloop i=1
while[ $I –le
5] do
s= ‘expr $i \* $i’
echo = “square of I “4s
i= ‘expr $i+1’
done
Eg: 1) Write a program to print the numbers from 1 to 10 using until loop
I=1
until[$i –gt 10]
do
echo “value of I :
$i” i=’expr
$i+1”
Done
2) Write a program to print odd numbers from 1 to 10 using untilloop
I=1
until[$i –gt 10]
do
echo “value of I :
$i” i=’expr
$i+2”
done
3) Write a program to print squares of 1 to 5 using until loop
I=1
Until [$i –gt 10]
do
s = ‘expr $i\* $i’
echo “square of I
“:&s i=’expr
$i+1’
done
The for command: This command works with a set of value generally given in the form of a
list.
Syntax: for variable in list
Do
Set of commands that are executed repeatedly
Done
Eg: 1) Write a program to print numbers from 1 to 10 using for loop
For I in 1 2 3 4 5 6 7 8 9 10
Do
Echo “ the value of i: “$i
Done
2) Write a program to print odd numbers from 1 to 5 using forloop
For I in 1 2 3 4 5
Do
Echo “ the value of i: “$i
Done
3) Write a program to print squares from 1 to 5 using forloop
For I in 1 2 3 4 5 6 7 8 9 10
Do
Echo ‘expr $i
\*$i’ Done