Skip to main content

Control Flow

· loading · loading · · ·
HDL Verilog HDL
Axolot Logic
Author
Axolot Logic
Digital Design Engineer
Table of Contents
Verilog HDL Series - This article is part of a series.
Part 7: This Article

πŸ”„ Control Flow Statements in Verilog
#

Control flow statements are used inside always or initial blocks to create conditional and sequential behavior.

βœ… Common Constructs:
#

Keyword Description Example
if, else Conditional execution if (a) y = 1; else y = 0;
case Multi-way branch See below ⬇️
for Counted loop for (i=0; i<4; i++) ...
while Loop until condition is false while (a != b) ...
repeat Loop fixed number of times repeat (5) ...
forever Infinite loop (use with care!) forever #10 clk = ~clk;

🧰 The case Statement
#

case provides a clean way to choose between multiple values.

βœ… Basic Syntax:
#

case (sel)
  2'b00: y = a;
  2'b01: y = b;
  2'b10: y = c;
  default: y = 1'b0;
endcase

πŸ”Ή casex and casez
#

  • casex: treats x and z in the case expression as wildcards
  • casez: only treats z as wildcard, not x

πŸ” Loops in Verilog
#

Loops are typically used in testbenches, generate blocks, or initialization routines.

βœ… Example – for loop:
#

integer i;
initial begin
  for (i = 0; i < 8; i = i + 1)
    mem[i] = 0;
end

⚠️ Loops must converge during synthesis β€” avoid infinite or variable-length loops in RTL.


Verilog HDL Series - This article is part of a series.
Part 7: This Article

Related

Blocking vs Non-Blocking Assignments
· loading · loading
HDL Verilog HDL
Command-Line Input
· loading · loading
HDL Verilog HDL
Compiler Directives & Macros
· loading · loading
HDL Verilog HDL
Delay Controls
· loading · loading
HDL Verilog HDL
Hierarchical Reference
· loading · loading
HDL Verilog HDL
Module Definition, Usage, and Constructs
· loading · loading
HDL Verilog HDL