Thursday, December 8, 2016

for loop

Using Looping Structures III

for loop

You can also use a for loop, which operates in a different manner than until or while loops. The
until and while loops keep looping indefinitely until the specified condition is met. A for loop, on
the other hand, loops a specific number of times.


It is very common to use the seq command within a for loop to create the sequence of numbers
to determine how many times it will loop. There are three options for creating a number sequence
with seq:


• If you specify a single value, the sequence starts at one, increments by one, and ends at
  the specified value.

• If you specify two values, the sequence starts at the first value, increments by one, and
  ends at the second value.

• If you specify three values, the sequence starts at the first value, increments by the second
  value, and ends at the third value.


Consider the following example:

seq 5 15

This command creates a sequence of numbers that starts at 5, increments by 1, and ends at 15.

Here is an example of using seq in a for loop:

for i in 'seq 15’

      do
            echo "The current number in the sequence is $i."
      done
exit 0


LX0-104 Exam Objectives (F)

No comments:

Post a Comment