Skip to main content

SystemVerilog Loops & Control Flow Statements

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

🔁 SystemVerilog Loops & Control Flow Statements
#

SystemVerilog provides several looping constructs and control flow mechanisms that make it easy to write flexible and dynamic RTL logic and testbench behavior. These constructs resemble those in traditional programming languages like C, but with simulation and hardware modeling in mind.


🔄 Loop Types in SystemVerilog
#

1. for Loop
#

The most common loop, similar to C-style syntax.

for (int i = 0; i < 10; i++) begin
  $display("i = %0d", i);
end
  • Suitable for known iteration counts
  • Can declare loop variable inside or outside

2. while Loop
#

Executes as long as the condition is true.

int i = 0;
while (i < 5) begin
  $display("i = %0d", i);
  i++;
end
  • Use with caution in hardware: avoid unbounded loops in RTL

3. do...while Loop
#

Executes at least once, checks condition at the end.

int i = 0;
do begin
  $display("i = %0d", i);
  i++;
end while (i < 3);
  • Useful when you want to run the loop body at least once

4. foreach Loop
#

Special loop to iterate over arrays (packed, unpacked, dynamic, associative).

int arr[5] = '{0, 1, 2, 3, 4};

foreach (arr[i]) begin
  $display("arr[%0d] = %0d", i, arr[i]);
end
  • Simplifies array traversal
  • Works well in testbenches

🧭 Control Flow Statements
#

break
#

Exits the current loop immediately.

for (int i = 0; i < 10; i++) begin
  if (i == 5) break;
  $display("i = %0d", i);
end

💡 Use break when you want to stop iterating under certain conditions.


continue
#

Skips the rest of the current iteration and goes to the next.

for (int i = 0; i < 5; i++) begin
  if (i == 2) continue;
  $display("i = %0d", i);
end

💡 Useful to skip unwanted values or conditions.


return
#

Used inside a task or function to exit early and optionally return a value.

function int square(int x);
  if (x < 0)
    return 0;
  return x * x;
endfunction
  • Not used to exit loops, but to exit functions/tasks

⚠️ Hardware Design Tip
#

  • Avoid using unbounded while or do...while in synthesizable RTL
  • break and continue are not synthesizable in RTL — use FSM logic instead
  • These constructs are mainly useful in testbenches, UVM, and behavioral models

✅ Summary Table
#

Keyword Purpose Synthesizable? Common Use Case
for Loop with counter ✅ (if bounded) Iterating over indexes
while Loop with condition ⚠️ (avoid infinite) Testbench control
do...while Executes loop at least once ❌ (not recommended in RTL) Behavioral models
foreach Array iteration ❌ (testbench only) Array access in TB
break Exit loop immediately Skip remaining loop
continue Skip current iteration Ignore specific values
return Exit function/task early Conditional returns

SystemVerilog’s control flow features help create intuitive and flexible logic, especially in testbenches. In synthesizable design, always ensure loops are bounded and deterministic.


SystemVerilog - This article is part of a series.
Part 5: This Article

Related

SystemVerilog Arrays
· loading · loading
HDL SystemVerilog HDL
SystemVerilog Typedef & Alias
· loading · loading
HDL SystemVerilog HDL
SystemVerilog `fork...join`
· loading · loading
HDL SystemVerilog HDL
SystemVerilog `interface`
· loading · loading
HDL SystemVerilog HDL
Blocking vs Non-Blocking Assignments
· loading · loading
HDL SystemVerilog HDL
SystemVerilog Data Types
· loading · loading
HDL SystemVerilog HDL