Basic Linux Tutorial
Computer Tutorial

Looping



Looping

The looping constructs allow you to repeat a list of commands, and as in the branching constructs, the decision to continue or cease looping will be based on the return code of a key command. The test command is frequently used to control the continuance of a loop.
Unlike branches, which start with a keyword and end with the keyword in reverse (if/fi and case/esac), loops will start with a keyword and some condition, and the body of the loop will be surrounded by do/done.
There are three basic types of loops :
» The let command
» while-do-done loop
» until-do-done loop
» for-do-done loop

The let command
The let command is also used to make arithmetic, logic, and assignment operations inside the loops and to change the value of the control variable.
while-do-done loop
The while-do-done loop checks for a condition and goes on executing a block of commands until that condition becomes false.
until-do-done loop
The until-do-done loop repeats the execution of a block of commands until a condition becomes true. As soon as the condition becomes true, the loop terminates.
for-do-done loop
the for-do-done loop, which is used to execute a block of commands for a fixed number of times.