This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubts about break

 
Bin Zhao
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If there are two loops ,one nested in the other,
Does the break statement in the inner loop exit the inner loop or the whole two loops?
Can anybody clear this?
Thanks
 
yogesh sood
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
break statement without label will cause exit from inner most loop or loop in which it is called but labeld break will cause break out oflabeled loop
yogi: for (int i=0;i<7;i++)
{
for( int j=0;j<6;j++)
if (j==2)
break;//break out of second loop only
else if(i==5)
break yogi;//break out of first loop
}
//line7
second break will take control to statement after outer most
loop i.e at line 7
Hope this will help :-)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic