Loops are used for executing a set of statements repeatedly until a particular condition is satisfied. In perl we have following loop constructs. Click on the links below to read them in detail with examples.
- for loop: A “For” Loop is used to repeat a statement or set of statements, a known number of times. For example, if we want to display the numbers from 1 to 10 then we can use for loop that would execute 10 times. When the number of times is not known before hand, we use a “While” loop.
- while loop: As mentioned above, when we do not know before hand that how many times we need to repeat the loop then we prefer to use while loop.
- do-while loop: It is similar to while loop, however there is a difference between them, while loop executes the set of statements inside it after evaluating the condition. whereas do-while executes the set of statements first and then evaluates the condition. That is why we prefer do-while loop when we need to execute a block of statements at least once.
- foreach loop: This loop is usually used when we need to iterate arrays
- until loop: This loop behaves just opposite to the while loop. It executes the block of statements until the given condition returns true.
Leave a Reply