Verilog: How to Use If, Else, & If-Else If Statements

Conditional statements are essential for any kind of programming, and Verilog is no exception. The language uses `if-else` statements to control the flow of execution based on whether a particular condition is true or false.

In hardware design, these conditional statements are vital for making decisions within your code, creating dynamic and adaptable circuits.

This article will cover the syntax, hardware implementation, and examples of `if`, `else`, and `if-else if` statements in Verilog. We’ll look at how these statements are used to create more complex and functional hardware designs.

Verilog Syntax for Conditional Statements

Conditional statements are the bread and butter of any programming language, and Verilog is no exception. They allow your code to make decisions based on certain conditions. Here’s a quick rundown of the syntax for `if`, `else`, and `if-else-if` statements in Verilog.

The `if` Statement

The basic syntax of an `if` statement in Verilog is:

if (expression) statement;

The `expression` inside the parentheses is a Boolean expression. If this expression evaluates to true (1), the `statement` that follows will be executed.

The `else` Statement

The `else` statement is used in conjunction with an `if` statement. Its syntax is:

if (expression) statement1; else statement2;

If the `expression` in the `if` statement evaluates to false (0), then `statement2` (the one following the `else`) will be executed.

The `if-else-if` Statement

For handling multiple conditions, you can use the `if-else-if` statement. The syntax is:

if (expression1) statement1; else if (expression2) statement2; else statement3;

With this structure, the conditions are checked sequentially. Verilog will evaluate `expression1`. If it’s true, `statement1` is executed. If it’s false, Verilog moves on to `expression2`, and so on. If none of the expressions are true, then `statement3` (following the final `else`) is executed.

How Conditional Statements Translate to Hardware

When you’re writing Verilog code, it’s easy to think of `if`, `else`, and `else if` statements as simple decision-makers. But it’s helpful to understand how those statements get implemented in actual hardware.

`if` without `else`

When you use an `if` statement without an `else`, you’re essentially telling the hardware to hold onto the previous value if the condition is false. This requires a latch or register to “remember” the previous state.

Here’s a simplified example of how this might look in Verilog, using an `always` block and the `reg` data type:


always @(posedge clk) begin
  if (enable) begin
    output <= input_data;
  end
end

`if` with `else`

An `if-else` statement directly translates to a multiplexer (MUX) in hardware. Think of a MUX as a switch that selects one of multiple inputs based on a select line. In this case, the condition in your `if` statement acts as that select line, choosing either the output from the `if` block or the output from the `else` block.

`if-else if`

For more complex scenarios with multiple conditions, like an `if-else if` structure, you're essentially creating cascaded multiplexers. Each `else if` condition adds another layer of selection. The hardware evaluates these conditions in order, and the output corresponding to the first true condition is selected. This is similar to a priority encoder, where the first valid input takes precedence.

Practical Examples of `if`, `else`, and `if-else-if` in Verilog

Let's look at a few examples of how you can use these conditional statements in Verilog.

Example 1: Simple Adder/Subtractor

Here's how you can use an `if-else` statement to create a circuit that either adds or subtracts two numbers, based on a control signal.

[Verilog code demonstrating the adder/subtractor would go here]

In this code, we have input signals `a` and `b`, an output signal `result`, and a control signal `op`. The `always` block defines the behavior of the circuit. If `op` is 1, the circuit adds `a` and `b`. Otherwise, it subtracts `b` from `a`.

Example 2: Implementing a 2-to-1 Multiplexer

A multiplexer selects one of several input signals and forwards it to the output. Here's how to build a 2-to-1 multiplexer using `if-else`.

[Verilog code for the 2-to-1 multiplexer would go here]

This code uses a control signal `sel` to select between two inputs, `in1` and `in2`. If `sel` is 1, the output `out` is assigned the value of `in1`. Otherwise, `out` gets the value of `in2`.

Example 3: Priority Encoder

A priority encoder outputs the index of the highest-priority active input. This example shows how to use `if-else-if` to build one.

[Verilog code for the priority encoder would go here]

In this example, the code checks a series of conditions sequentially. If `in[3]` is 1, the output `out` is set to 3. Otherwise, it checks if `in[2]` is 1, and so on. This way, the highest-priority input is always encoded.

To Conclude

Conditional statements—if, else, and if-else-if—are essential for creating digital hardware using Verilog. They give you the power to make decisions within your code.

It's important to understand how these statements translate into actual hardware, so you can design circuits that are both effective and efficient.

Now that you have a basic understanding of conditional statements, I encourage you to explore more complex examples and find new ways to use this fundamental tool in your Verilog projects.