什么是循环
循环------让某段代码反复执行多次。
常用的循环语句
forever | 永久运行给定的语句集 |
---|---|
repeat | 在给定的次数内重复给定的语句集 |
while | 只要给定条件为真,就重复给定的语句集 |
for | 类似于while循环,但更浓缩和流行的形式 |
do while | 至少重复给定的语句集一次,然后只要条件为真就循环 |
foreach | 主要用于遍历数组中的所有元素 |
demo
// Code your testbench here
module tb;
bit [7:0] cnt_for;
bit [4:0] cnt;
bit clk;
integer i = 0;
always #5 clk = !clk;
initial begin
forever begin
#10 $display("hi,forever");
end
end
initial begin
repeat(2) begin
$display("hello world");
end
end
initial begin
while(cnt<10)begin
@(posedge clk)
$display("cnt = %d",cnt);
cnt ++;
end
end
initial begin
for(cnt_for = 3;cnt_for <20;cnt_for = cnt_for + 2)begin
$display("cnt_for == %d",cnt_for);
end
end
initial begin
do begin
i = i + 1;
$display("i = %0d", i);
end while (i < 5);
end
initial begin
#100 $finish;
end
endmodule