Most languages have the concept of loops. If we want to perform a task repetitively with a little bit of change, the loop is the solution to write the logic. Different types of loops are used in this scripting language. The following examples demonstrate the loops with some examples.
for loop
There are two types of syntaxes of for loop. In the first example, the loop is iterated based on the value of i. The first part is an initialization, the second part is condition checking and the last part is increment or decrement.
In the following example, the loop iterates over the elements starting from 0 to 5. In the first iteration, i will be assigned 0, in the next iteration i will be 1, and so on.
nested for loop
Similar to the if-else block, for loop can also be nested as shown below.
while loop
Any one of for or while loops can be used to perform repetitive tasks. The syntax of the while loop is shown in the following example.
nested while loop
Similar to for loop nesting, a while loop can also be nested as shown below.
until loop
There is another loop, until, which can be used instead of the while loop. But the difference is that the loop will run until the condition is true, whereas the while loop runs until the condition is false.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.