Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding Continue Keyword

 
Gaurav Chhabras
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class jcp
{
public static void main(String args[])
{
int i = 0, j = 0, k = 0,h = 0;
label1:
for (h = 0; h < 6; h++)
{
label2:
do
{
i++;
k = h + i + j;
switch (k)
{
default: break label1;
case 1: continue label2;
case 2: break;
case 3: break label2;
case 4: continue label2;
case 5: continue label1;
}
}while (++j<5);
}
System.out.println(h + "," + i + "," + j);
}}




i have one question regarding above code - :

when it encounters CONTINUE LABEL2 then it will execute the (++j<5) part of the do while loop , but when it encounters CONTINUE LABEL1 then it does not executes (++j<5).
Can you please tell me that why it is so.

Thanks
Regards
Gaurav
 
Kris Krason
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's because when you do continue label2, then the do {} while loop will move to the next iteration, and while it does, it checks wheter (++j<5) is true.

When you do continue label1, then it goes to next FOR loop, it completely ignores do {} while, it will h++, and check the for loop test.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic