Verilog Conditional Statements

In Verilog, conditional statements are used to control the flow of execution based on certain conditions. There are several types of conditional statements in Verilog listed below.

Conditional Operator

The conditional operator allows you to assign a value to a variable based on a condition. If the condition is true, expression_1 is assigned to the variable. Otherwise, expression_2 is assigned.

Nested conditional operators

Conditional operators can be nested to any level but it can affect readability of code.

 // "y" is assigned to "out" when both "a < b" and "x % 2" are true // "z" is assigned to "out" when "a < b" is true and "x % 2" is false // 0 is assigned to "out" when "a < b" is false assign out = (a < b) ? (x % 2) ? y : z : 0; 

Here are some of the advantages of using conditional operators:

And some disadvantages:

if-else statement

The if-else statement allows you to perform different actions based on a condition.

 if () begin // statement 1 end else begin // statement 2 end 

If the condition evaluates to true, statement 1 is executed. Otherwise, statement 2 is executed.

Read more on Verilog if-else-if statements

case statement

The case statement is used when you have multiple conditions and want to perform different actions based on the value of a variable.

 case () value1: statement1; value2: statement2; . default: statementN; endcase 

The expression is evaluated, and based on its value, the corresponding statement is executed. If none of the values match the expression, the statement under default is executed.

Read more on Verilog case statement