Skip to content

Verilog · Chapter 14.5.1 · Behavioural Modeling

if/else Statements in Verilog — Priority Branching & Latch Avoidance

The if, else if, else statement is behavioural logic's priority branch. It checks conditions in order, and the first one that is true wins. That ordering is its defining property, because it synthesizes to a priority mux, a cascade where each later condition is reached only if every earlier one was false. This makes if and else the natural fit for prioritized decisions, such as an error overriding normal operation or an interrupt taking precedence. This lesson covers the priority semantics, nesting, and grouping multiple statements with begin and end. It also drills the latch hazard: a combinational if with no else leaves the output unassigned on the false path and infers an unintended latch, so combinational branches must be complete or start with a default assignment.

Foundation13 min readVerilogif elsePriorityLatchCombinational

Chapter 14 · Section 14.5.1 · Behavioural Modeling

1. The Engineering Problem

if/else is the most-used branch, and it carries two facts every RTL engineer must hold: it is a priority structure, and in combinational logic it must be complete.

if/else if/else checks conditions in order — the first true one wins (priority) — and a combinational if without an else infers a latch.

This page drills the priority semantics and the latch-avoidance discipline for if/else.

2. Mental Model — First True Condition Wins

3. Priority Branching

priority.v
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
   always @(*)
       if      (irq)   action = SERVICE_IRQ;   // highest priority
       else if (busy)  action = WAIT;
       else if (ready) action = PROCEED;
       else            action = IDLE;           // else → complete (no latch)

irq is checked first; busy only if irq is false; and so on. The first true condition wins, so this is a priority encoder of actions — exactly when if/else is the right choice (the conditions have a natural precedence). The final else ensures action is always assigned.

Visual A — an if/else chain is a cascade of muxes (priority is structural)

if/else → a chain of 2:1 muxes (priority in the wiring)

data flow
if/else → a chain of 2:1 muxes (priority in the wiring)if irqselect 1 of {SERVICE, rest}else if busyselect 1 of {WAIT, rest}else if readyselect 1 of {PROCEED, IDLE}actionearliest true wins → priority
if/else is not 'branching' — it is hardware. Each condition becomes a 2:1 mux, and the conditions chain so the earliest true one overrides the rest. The cascade of muxes IS the priority: the structure that makes 'irq' beat 'busy' beat 'ready' is the wiring order, not run-time control flow. Reordering the conditions rewires the priority.

4. Nesting and Grouping

if statements nest, and multiple statements per branch need begin...end (14.2):

nesting.v
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
   always @(*)
       if (mode) begin                  // begin...end for >1 statement
           y = a;
           valid = 1;
       end else begin
           y = b;
           valid = 0;
       end
 
   // nested if for compound conditions:
   always @(*)
       if (en)
           if (sel) y = a;              // inner if
           else     y = b;
       else y = 0;

Remember the dangling-else and missing-begin...end traps from 14.2 — wrap multi-statement branches in begin...end, and be explicit about which if an else pairs with.

5. Latch Avoidance for if

A combinational if must assign every output on both the true and false paths:

if-latch.v
Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
   // LATCH — y unassigned when en=0:
   always @(*) if (en) y = d;
 
   // FIX — else:
   always @(*) if (en) y = d; else y = 0;
 
   // FIX — default first:
   always @(*) begin
       y = 0;
       if (en) y = d;
   end

The default-first pattern scales to complex branching: assign defaults at the top, override in branches, so no path leaves an output unassigned. (Sequential @(posedge clk) blocks intentionally hold, so an if without else there is a register that holds — fine; this latch rule is for combinational blocks.)

Visual B — completeness, and the latch from a missing else

if/else: priority cascade; missing else → latch

data flow
if/else: priority cascade; missing else → latchif a / else if b/ else cfirst true wins (priority)→ priority muxcascade, orderedif en (no else)false path unassigned→ LATCHoutput holds → memory
A complete if/else builds a priority mux (first true condition wins, ordered cascade). An if without an else in a combinational block leaves the output unassigned on the false path, so it holds its value — an inferred latch. Complete the branches or assign a default first.

6. Common Mistakes

  1. Missing else in combinational if — infers a latch; complete it (§5, DebugLab 1).
  2. Using if/else where case is clearer — value selection reads better as case (14.5.2).
  3. Missing begin...end — a statement escapes the branch (14.2).
  4. Dangling else — an else pairs with the nearest unmatched if; be explicit (§4).

7. Debugging Lab

One if/else debug post-mortem

8. Interview Q&A

9. Exercises

Exercise 1 — Priority order

For if (a) y=1; else if (b) y=2; else y=3;, which value does y get when both a and b are true, and why?

Exercise 2 — Spot the latch

Why does always @(*) if (sel) y = d; infer a latch, and what are two fixes?

Exercise 3 — Write a priority encoder

Write a combinational if/else that outputs the index (2-bit) of the highest-priority set bit among r[3:0] (r[3] highest), with a complete else.

10. Summary

The if/else statement is priority branching:

  • Priority — first true condition wins; synthesizes to a priority-mux cascade; textual order is priority order.
  • Nesting and grouping — nest for compound conditions; begin...end for multi-statement branches.
  • Latch avoidance — a combinational if needs an else (or default-first) so every path assigns every output; otherwise a latch is inferred.

The next sub-topic is the parallel branch: Chapter 14.5.2 Case Statements drills case/casez/casex — value selection, default for latch avoidance, and the casex x-matching hazard.