Wednesday, May 16, 2007

It's a bug!

[It is not a feature]

I was working on simulating break and continue statements in loops.

First I worked with for loops, and got them working. Then I started with while. I wrote a simple while loop, and a simple break statement in it:
j = 0;
while(j <= 7)
begin
if(j==4)
break;
out1[j] = 1;
j = j + 1;
end

This worked fine. So, to check out continue, I simply replaced break with continue [as I had done with for] and expected it to work straighforward.
j = 0;
while(j <= 7)
begin
if(j==4)
continue;
out1[j] = 1;
j = j + 1;
end

But I was in for a shock! The tool got stuck! I looked at the testcase, and at the code, twice. Concluding that there was a bug in the implementation in the tool, I consulted a colleague. He agreed that there is a problem in the tool. After playing around with the test for a few minutes I realized that the tool was smart enough. We were the dumb party!!

[OK, so what is the bug?!]

4 comments:

All Is Whole said...

C/C++...........
now i am away from all this
I am into BI
Happy Coding.........

Smiles :)

Sigma said...

@Prashant: Thanks for coming here :-)
Well, this particular problem originated in Verilog :-)
What does BI stand for?

Pooja Aggarwal said...

I guess it does not matter which language you use - this will not work.
The value of j is not changing in the while (expression) where the code lands after continue. So it pretty much runs into an infinite loop. In case of the for loop it was working because the value of j was incremented in for stmt.

Shalini,
You must read "Deep C Secrets" - I bet you will find it interesting.

Sigma said...

Right on Pooja! This will not work in any language :-))
When I hit it, I was kind of biased by the "for" loop and the "break". And this being the first implementation of Verilog "break" and "continue", the probability of it's not working was rather likely [In all these years, I have seldom seen something work at the first go :-D].
Thanks for the recommendation - I am going to check it out :-)