Simulation not coming out of break in a loop in fork-join_any

I am using below code-


module  fork_try;
reg a,b;
initial begin

fork:fork2
 begin
   #2 $display("2.done and t5 and time %d",$time);  
   b=1;
 end
 begin
   for(a=0;a<4;a++) //why for loop is getting executed again and again?
   begin
   #2 b=a;  $display("b is %d and time %d",b,$time);
   if(b==2)
      begin $display("a is %d",a);break;  
      end
   end 
   ///if(b==1)
   //   continue; //continue statements only allowed within loops //continue statement inside a fork...join cannot be used to control loop outside the fork...join
   $display("5.done and t15 and time %d",$time);
 end
join_any

end//initial

endmodule

During simulation , i am getting following result-
2.done and t5 and time 2

b is 0 and time 2

b is 1 and time 4

1.done and t20 and time 5

b is 0 and time 6

b is 1 and time 8

b is 0 and time 10

b is 1 and time 12

b is 0 and time 14

b is 1 and time 16

b is 0 and time 18

until simulation ends.
Why fork is not getting finished. If i give continue instead of break, i am observing the same behaviour.

The issue is that your two variables (a and b), are declared as ‘reg a,b’ which are single bits. Your loop wants to count to 4, which overflows your a variable, resulting in an infinite loop.

Declare a and b as int and your will see the behavior you expect.

In reply to cgales:

Thanks!! Now it is working.