Originally posted by Nirmal:
Hi Gouru,
I run this program and got the answer as '5'. I put messages in and saw the output.
When the value of 'i=5',it will come out of loop and the control goes to 'label1'. In lebel ,the loop value becomes false and so it come out the loop without decrementing value of y. So y is still the same (ie 5). When you print intermediate results in the program ,you will better understand the flow. Hope this clears some basics of your question. Any changes or modifications?.
Thanks.
Nirmal
Hi gouru
I have run this program and found that this runs for all the three cases i.e.
continue label1;
continue label2;
continue;
Actually, It is not coming out of the loop when 'i' reached a value of 5 to label1 as stated by the Nirmal.
When we are saying continue label1 or continue label2 or continue
the control is transferred to the beginning of the loop. Also the other label1 is treated as entirely unknown. Only after i has reached a value of 10 it is coming out of the initial for loop. Then it is executing the other statement.
label1:
for(;i<5;i--)
{
y--;
}
The value of i is 10 hence it is coming out of that for loop without changing the j value.
The output of j is 5.
So label is not exactly like goto. Label has restrictions and can be used only for the same loop and cannot be used to transfer control out of the loop.Compiler gives an error when you try to pass control to outside the loop. Here two label1's are treated as totally different. One thing i could not understand is the fact that this loop is working for both continue label1 and continue label2. This is really surprising.
Anyone can help us
murali