If Statement

Definition:

The if statement is a statement that depending on the value of one or more corresponding conditions, selects for execution one or none of the enclosed sequences of statements,.

Simplified Syntax

if condition then

  sequential_statements

end if;

if condition then

  sequential_statements

else

  sequential_statements

end if;

if condition then

  sequential_statements

  elsif condition then

    sequential_statements

  else

    sequential_statements

end if;

Description

The if statement controls conditional execution of other sequential statements. It contains at least one Boolean condition (specified after the if keyword). The remaining conditions are specified with the elsif clause. The else clause is treated as elsif true then. Conditions are evaluated one by one until any of them turns to be true or there are no more conditions to be checked for. When a condition is true then the sequence of statements specified after the then clause is executed. If no condition is met then the control is passed to the next statement after the if statement.

The if statement can be used in a simplified form, often called if-then statement, where neither elsif nor else clause is supported (example 1).

In case when meeting a condition should cause some statements to be executed, but when it is not met some other actions should be performed, the else clause is used (example 2).

The elsif clause is used when different nested conditions have to be checked (example 3). This can be used for prioritizing conditions and signals.

The if statements can be nested (example 4).

Examples

Example 1

I1: if Status_Signal = hold
      then A1: Outputs <= 'X';
    end if I1;

The assignment will be realized only when the condition Status_Signal = hold is true. Otherwise, the statement that follows the end if I1 will be executed.

Example 2

function AND_FUNC (x, y: in Bit) return Bit is
begin
  I2: if x = '1' and y = '1'
        then return '1';
      else return '0';
      end if I2;
end AND_FUNC;

When the variables x and y are both equal to '1', then the function returns the value '1'. Otherwise, '0' is returned.

Example 3

Signal Code_of_Operation : Bit_Vector(1 downto 0);
I3: if Code_of_Operation(1) = '1'
      then F := Operand_1 + Operand_2;
    elsif Code_of_Operation(0) = '1'
      then F := Operand_1 - Operand_2;
      else F := "00000000";
    end if I3;

In this example, the bit number 1 of the Code_of_Operation has a higher priority than bit number 0. When the bit number 1 has a '1'value, then the two operands are added. If not (i.e. it is '0'), then the bit number 0 is checked. If it is '1', then the two operands are subtracted. Otherwise, when both bits of the Code_of_Operation are '0', the F signal is assigned all zeros.

Example 4

if Status = RUN
  then
    if Code_of_Operation = CONC
      then
        F := Operand_1 & Operand_2 ;
      else
        F := "00000000";
    end if;
  Output_1 <= F;
end if;

Nesting of if statements is legal, but you should be careful not to confuse the statement levels.

Important Notes

· One of the most typical errors with the if statements is to skip the space between end and if, at the end of the statement, and writing it as endif.

· If is a sequential statement that cannot be used in the concurrent statements section of an architecture. If assigning a new value to a signal must be influenced by a specific condition, then a conditional signal assignment should be used (which in turn cannot be used inside processes and procedures).

No comments:

Post a Comment