- Debug Daily
- Posts
- Intro: Buggy Loop
Intro: Buggy Loop
Identify the bug in the below loop (and fix it if you want!)
module counter_checker;
logic [3:0] values[0:3]; // Array of 4 elements
int sum = 0;
initial begin
// Initialize array
values[0] = 1;
values[1] = 2;
values[2] = 3;
values[3] = 4;
// Buggy loop: supposed to sum all 4 elements
for (int i = 0; i < 3; i++) begin
sum += values[i];
end
$display("Sum is: %0d", sum); // Expected: 10, Actual: 6
end
endmodule
Reply