0% found this document useful (0 votes)
2 views

unix loop program

The document explains loop control structures in programming, specifically the while, until, and for commands, along with their syntax and examples. It also covers the use of continue and break statements to control loop execution. Each loop type has distinct behavior regarding condition evaluation and execution of commands.

Uploaded by

dgpguru
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

unix loop program

The document explains loop control structures in programming, specifically the while, until, and for commands, along with their syntax and examples. It also covers the use of continue and break statements to control loop execution. Each loop type has distinct behavior regarding condition evaluation and execution of commands.

Uploaded by

dgpguru
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

1) The while command:

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

The until command:


➢ Like the while command this command is also 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 as long as the condition remains false.
Syntax:
Until
[condition] Do
Set of commands to be executed repeatedly
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

The continue and break statements:


➢ One can come out of the loop permanently by using a break statement (for, while,until).
➢ Similarly ,a continue statement is used to resume the next iteration of loop without considering
the
statements that appear after the continue statement within the loop.
➢ Eg: $ cat menu2.sh
ans=y
while[“$ans “=”y”]
do
echo “MENU \n
1) List of files\n
2) Today’s date \n
3) Process status\n
4) Users of the system\n
5) Present working directory\n
6) Quit to unix\n
Enter your option:\c” Read choice
Case “$choice” in
1) ls –l;;
2) date;;
3) ps;;
4) who;;
5) pwd;;
6) break;;
7) echo “Invalid choice”
Continue;;

You might also like