Basic Linux Tutorial
Computer Tutorial

The while-do-done Loop



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 :


while condition
do
command block
done

The execution is as follows :
1. Commands in condition are executed.
2. If the return code of the last command in condition is 0 (true), execute command block.
3. Return to step 1.
4. If the return code of the last command in l condition is not 0 (false), skip to the first command
5. following the done keyword.

Flow chart of while-do-done Loop

while do done Loop

$ X = 1
$ while   [ $X -le 10 ]
case do

echo  hello X is $X
let X = X + 1

done