Prev Page Next Page

Loop Statement

Formal Definition

Statement that includes a sequence of statements that is to be executed repeatedly, zero or more times.

Simplified Syntax

loop_label: while condition loop


  end loop loop_label;

loop_label: for loop_parameter in range loop


  end loop loop_label;


The loop statement contains a sequence of statements, which are supposed to be repeated many times. The statement also lists the conditions for repeating the sequence or specifies the number of iterations.

A loop statement can have several different forms depending on the iteration scheme preceding the reserved word loop. In its simplest form, no iteration scheme is specified and the loop is repeated indefinitely (Example 1).

In order to exit from an infinite loop, an exit statement has to be used. See exit statement for details. An exit statement can be specified with a condition that must be met to exit the loop (Example 2).

Instead of specifying an infinite loop with a conditional exit statement, a while loop can be used. In such a case the reserved word while with a condition precede the keyword loop. The sequence of statements inside the loop will be executed if the condition of the iteration scheme is true. The condition is evaluated before each execution of the sequence of statements. When the condition is false, the loop is not entered and the control is passed to the next statement after the end loop clause (Example 3).

Another iteration scheme is useful when a discrete range can define the number of iterations. In this case the keyword for with a loop parameter precede the keyword loop. The header of the loop also specifies the discrete range for the loop parameter. In each iteration the parameter takes one value from the specified range, starting from the leftmost value within the range (Example 4).

The discrete range can also be declared in the form of a discrete type (example 5), including an enumerated type.


Example 1

signal Clock : BIT := '0';
Clk_1: process (Clock)
  L1: loop
      Clock <= not Clock after 5 ns;
      end loop L1;
end process Clk_1;

The process (which is a clocking signal generator) contains a loop without an iteration scheme, which will iterate indefinitely.

Example 2

L2: loop
    A:= A+1;
    exit L2 when A > 10;
    end loop L2;

The infinite loop becomes in practice a finite, as the iterations will terminate as soon as the variable A becomes greater than 10.

Example 3

Shift_3: process (Input_X)
variable i : POSITIVE := 1;
  L3: while i <= 8 loop
        Output_X(i) <= Input_X(i+8) after 5 ns;
        i := i + 1;
      end loop L3;
end process Shift_3;

The loop L3 will be repeated as long as the value of the variable i is not greater than 8. When i reaches the value of 9 the loop is no longer repeated.

Example 4

Shift_4: process (Input_X)
  L4: for count_value in 1 to 8 loop
       Output_X(count_value) <= Input_X(count_value + 8) after 5 ns;
      end loop L4;
end process Shift_4;

In the above example the loop statement parameter count_value will cause the loop to execute 8 times, with the value of count_value changing from 1 to 8.

Example 5

Shift_5: process (Input_X)
subtype Range_Type is POSITIVE range 1 to 8;
  L5: for count_value in Range_Type loop
       Output_X(count_value) <= Input_X(count_value +8) after 5 ns;
      end loop L5;
end process Shift_5;

The subtype Range_Type defines the range of integer values from 1 to 8. The parameter count_value changes according to this specification, i.e. from 1 to 8.

Important Notes

Prev Page Next Page