Ready-to-run snippets

Code Examples

Ready-to-run Verilog, SystemVerilog, and UVM examples. Click any to open it live in the IDE.

← All examples

for loop: Using the for Loop in Verification

SystemVerilog Control Flow & Loops

Runnable example from the SystemVerilog for loop tutorial (Using the for Loop in Verification)

Open in IDE
testbench.sv
// Note: Verilator does not support array[i][j] = "0" on a string, so putc() on a temporary copy is used (same effect).
module tb;
	string array [5] = '{"apple", "orange", "pear", "blueberry", "lemon"};

	initial begin
      for (int i = 0, j = 2; i < $size(array); i++) begin
			begin string tmp; tmp = array[i]; tmp.putc(j, "0"); array[i] = tmp; end
        $display ("array[%0d] = %s, %0dth index replaced by 0", i, array[i], j);
		end
	end
endmodule
Admin mode — edit and delete controls are visible on each row. Use the IDE to add new snippets via File → Save as Snippet.