π 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
: treatsx
andz
in the case expression as wildcardscasez
: only treatsz
as wildcard, notx
π 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.