++i, i++, --i, i–, i = ++i, i = i++在for循环的区别
结论:
- ++i,i++ , i = ++i, i = i++在for循环中下面的用法中是没有区别的。
- –i, i --在for循环中下面的用法中是没有区别的。
1.for循环的逻辑
for语句的一般形式为:
for(表达式1,表达式2;表达式3)语句:
它的执行过程如下:
- 先求解表达式1.
- 求解表达式2,若其值为真(非0),则执行for语句中指定的内嵌语句,然后>执行下面的第3步。若为假(0),则结束循环,转到第5步。
- 若表达式为真,在执行指定的语句后,求解表达式3.
- 转回上面的第(2)步骤继续执行。
- 执行for循环外面的语句。
2.示例
program for_loop();
initial begin
for(int a; a<= 3; a++) begin
$display("a == %0d", a);
end
$display("=================================");
for(int b; b<= 3; ++b) begin
$display("b == %0d", b);
end
$display("=================================");
for(int c = 3; c>= 0; --c) begin
$display("c == %0d", c);
end
$display("=================================");
for(int d = 3; d>= 0; d--) begin
$display("d == %0d", d);
end
$display("=================================");
for(int e; e<= 3; e = e++) begin
$display("e == %0d", e);
end
$display("=================================");
for(int f; f<= 3; f = ++f) begin
$display("f == %0d", f);
end
end
endprogram
3.仿真结果
下面是用VCS仿真结果。
a == 0
a == 1
a == 2
a == 3
=================================
b == 0
b == 1
b == 2
b == 3
=================================
c == 3
c == 2
c == 1
c == 0
=================================
d == 3
d == 2
d == 1
d == 0
=================================
e == 0
e == 1
e == 2
e == 3
=================================
f == 0
f == 1
f == 2
f == 3