Exit is used to exit the loop. If this statement contains a condition, it means that exiting from the loop depends on that condition.
After exit statement processed, the first next expression is processed. There is no necessity to use the name of the loop. If a label is not used, the inner loop ends. If you want to exit the outer loop, a label must be used.
exit loop_name when condition;
Exit statement is usually confused with next statement. The important point to remember is, the loop ends when you use â€śexitâ€ť, but the next statement is processed when you use â€śnextâ€ť without exiting the loop.
Loop_1: for count_value in 1 to 10 loop
exit Loop_1 when reset = '1';
A_1: A(count_value) := '0';
end loop Loop_1;
A_2: B <= A;;
In this example, reset:â€™1â€™ condition is checked in every repititions in the loop. If reset is 1, he loop ends then the A_2 labeled line is processed.Â Oherwise next step in the loop is processed.
a_v := 0;
Exit_1: exit Loop_X when condition_1;
Output_1(a_v) := Input_1(a_v);
a_v := a_v + 1;
Exit_2: exit when condition_2;
end loop Loop_Y;
Assign_Y: B(i) <= Output_1(i) after 10 ns;
Exit_3: exit Loop_X when condition_3;
end loop Loop_X;
Assign_X: A <=B after 10 ns;
There are two nested loops (LOOP_X and LOOP_Y) in the example above. If the condition_1 is TRUE, Exit_1 is processed then LOOP_X ends. Then the next step (Assign_X) is processed.
If condition_1 becomes FALSE, the next steps are processed including condition_2. If this condition_2 Â is TRUE, Exit_2 is processed then LOOP_Y which is the inner loop ends and Assign_Y is processed.
If this condition_3 Â is TRUE, LOOP_X ends. Since this condition is located in the outermost loop, you donâ€™t have to write LOOP_X as label in the Exit_3 line.
In this example we will design an adder which will result the sum of the numbers from 1 to the last number variable. There will be a start input and program will be activated at the start signalâ€™s rising edge.
If the last_number is 5, the sum is:
entity sum is
Port ( start : in STD_LOGIC;
last_number : in STD_LOGIC_VECTOR(5 downto 0);
result : out STD_LOGIC_VECTOR(15 downto 0) );
architecture Behavioral of sum is
if start ='1' and start 'event then
exit SUM_LOOP when i= CONV_INTEGER(last_number);
After simulating the code with Modelsim, we will get the plot below: