Branch Statements

The conditional operator.

This is similar to the conditional operator in C, it can be imagined to be an abbreviated if-else. The conditional expression is evaluated in the same way, if it evaluates to true the true_expression is evaluated else the false_expression.

Syntax: conditional_expression ? true_expression : false_expression

To illustrate the conditional operator consider the implementation of a 2 to 1 multiplexor below:

        assign out = control ? in2 : in1;

The true and false expressions can themselves be conditional operators, so enabling more than one interdependant conditional_expression. Conditional operators can be useful in modelling conditional data assignment.

EXERCISE

Implement a 4 to 1 multiplexor using a nested conditonal operator, making sure it uses the same simulation block as before

Answers


previous contents