11.2 The while-do-done Loop

   

11.2 The while-do-done Loop

The while-do-done loop is used to test a condition before the execution of the block of commands contained inside the loop. The command block is executed if the test is successful and returns a true value. It may happen that the command block never executes if the test fails the very first time. The loop continues to execute as long as the condition remains true. The general syntax of the while-do-done loop is shown here.

 while condition do    command block done 

The condition is usually an expression containing a test or let command. Both of these commands are usually used in implicit mode. The while-do-done loop can be represented as a flow diagram as shown in Figure 11-1.

Figure 11-1. The while-do-done loop.

graphics/11fig01.gif

Let us see an example of the loop. We start with assigning the value 1 to a variable VAR1 . Every time the loop executes, we double the value of the variable. The loop continues as long as the value of the variable is less than 100. As soon as the variable value reaches this limit, the loop execution terminates, and the next command after the done keyword is executed. The shell program script-20 follows .

 #!/usr/bin/sh echo "The while loop example" echo VAR1=1 while ((VAR1 < 100)) do    echo "Value of the variable is : $VAR1"    ((VAR1 = VAR1 * 2)) done echo echo "The loop execution is finished" 

You can also use the test command instead of the let command in the comparison made in the while condition. In that case, this line will be:

 while [ VAR1 -lt 100 ] 

When you execute this program, you will see the output shown here.

 $  ./script-20  The while loop example Value of the variable is : 1 Value of the variable is : 2 Value of the variable is : 4 Value of the variable is : 8 Value of the variable is : 16 Value of the variable is : 32 Value of the variable is : 64 The loop execution is finished $ 

A while loop may become an infinite loop if you make a mistake while making a test decision. For example, consider the following program where you start with a value of VAR1 equal to 1. You add 2 to the value of VAR1 at each step. You compare the value of the variable with 10. This condition is never fulfilled because the value of the variable never becomes 10. It goes from 9 to 11, skipping the value to which the comparison is made. By changing " != " to " <= ", you can solve the problem. The program script-21 is shown here.

 #!/usr/bin/sh echo "The while loop example" echo VAR1=1 while ((VAR1 != 10)) do    echo "Value of the variable is : $VAR1"    ((VAR1 = VAR1 + 2)) done echo echo "The loop execution is finished" 

Another example of an infinite loop is when you forget to modify the control variable inside the loop, such as in the code segment that follows.

 VAR1=1 while ((VAR1 != 10)) do    echo "Value of the variable is : $VAR1" done 

Here the value of VAR1 is always 1, and the condition remains true, resulting in an infinite loop.


   
Top


HP Certified
HP Certified: HP-UX System Administration
ISBN: 0130183741
EAN: 2147483647
Year: 2000
Pages: 390
Authors: Rafeeq Rehman

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net