Home > VHDL > Concurrent Statements > Loop statements

Loop Statements :

In loop statement, label is optional. In this statement iterative scheme is used. The iterative schemes used in loop statement are, 1) loop, 2) while...loop, and 3) for...loop. Next and exit statements are used in loop. Sequential statements are mainly implemented inside the loop. The next statement skips the remainder.
The syntax is :
[iteration_scheme] loop
{ statement }
{ next [
label ] [ when condition ] ; }
{ exit [
label ] [ when condition ] ; }
end loop;

1) Basic Loop : The basic loop statement do not have iteration scheme. It executes the statements unless and until exit or next statement are found. The syntax is :

loop
 { statements }
 end loop;
 Example :
 for i in DR loop
 -- Statements..
 end loop; 

2) While...loop : In while...loop if the condition evaluates true the loop is executed. The condition is repetitively calculated unless and until condition is true. When the condition is false, the loop is terminated. The syntax is : while condition

loop
 {
statements }
 end;
  

Example :

process
 begin
 while  ef= 1 and test /= '1 loop
 clk
<= not
clk;
 wait for clk_time/4;
 end
loop;
 end
process; 

3) For...loop :

The for loop has fixed number of iterations. The for loop has automatic declaration for index. You do not need to separately declare the index variable. The loop should not have numeric types and values. A for...loop statement have integer variable declared with the identifier. The identifier receives the first value of range, and the sequence of statements executes once. The syntax is :

 for
identifier in range loop
 {
statement }
 end
loop;
 Example :
library ieee;
use
ieee.std_logic_1164.all;
entity test is
 port (D: in std_logic_vector(0
to 9);
 DD:
out
std_logic);
 constant WIDTH: integer := 10;
 end test;
 architecture behave of test is
 begin
 process(D)
 variable otmp: Boolean;
 begin
 ot
:= false;
 for i in 0 to D'length - 1 loop
 if D(i) = '1' then
 ot
:= not ot;
 end
if;
 end
loop;
 if otmp then
 DD
<= '1';
 else
 DD
<= '0';
 end
if;
 end
process;
 end behave;