Nested Loop and Labeled break and Labeled continue.

In this article,Nested Loop and Labeled break and Labeled continue is explained in detail with examples.Using a particular looping structure inside an another looping structure is known as nested loop.
Syntax
outer-loop:(no of rows)
{
Inner-loop:(no of columns)
{
----
----
}
}
Here inner loop is executed that means number of times based on outer loop iterations.
It is used in following situations  (From topic Nested Loop and Labeled break and Labeled continue)
  • To represent data in the form of rows and columns.
  • Matrices.
  • Tables.
  • Graphs.
  • Maps.
  • Two-dimensional arrays.
Ex
//program related to Nested Loop and Labeled break and Labeled continue--nested loop
class ForLoopNested
{
public static void main(String args[])
{
for(int i=1;i<=3;i++)
{
for(int j=1;j<=3;j++)
{
System.out.print("("+i+","+j+")");
}
System.out.println();
}
System.out.println("End of the Nested loop");
}
}
Output
Nested Loop-javaform
                         To get the code file use me.

Labeled break and labeled continue.
This feature is designed especially for nested loops only.
Label is an identifier in a program,which is used for a loop. It works like a 'bookmark'.
Syntax
label1:for(----) //outer loop
{
-----
label2:for(-----) //inner-loop
{
-----
----
}
-----
}
Here label works like a name for a particular loop.
Note
If a break statement is used with a label then it is known as labeled break.
Labeled break.
Syntax
label1:for(---) //outer loop
{
---
lable2:for(---) //inner loop
{
---
if(condition)
break label1;
----
}
---
}
In this case,break will skip remaining iterations of inner loop and as well as remaining iterations of outer loop,and control comes out of outer loop block. (From topic Nested Loop and Labeled break and Labeled continue.)
Ex
//program related to Nested Loop and Labeled break and Labeled continue--label break
class ForLoopBreakLabel
{
public static void main(String args[])
{
outer:for(int i=1;i<=3;i++)
{
inner:for(int j=1;j<=3;j++)
{
if((i==2)&&(j==2))
{
System.out.println();
break outer;
}
System.out.print("("+i+","+j+")");
}
System.out.println();
}
System.out.println("End of For Loops");
}
}
Output
Labelled Brake-javaform
                           To get the code file use me.

Labelled Continue.
If continue is used with a label then it is known as labeled continue.
Syntax
Label1:for(----) //outer-loop
{
Label2:for(----) //inner-loop
{
----
if(condition)
continue label1;
-----
}
Here, in this case,it skips remaining iterations of inner loop and continues with next iteration of outer loop. (From topic Nested Loop and Labeled break and Labeled continue.)
Ex
//program related to Nested Loop and Labeled break and Labeled continue--label continue
class ForLoopContinueLabel
{
public static void main(String args[])
{
outer:for(int i=1;i<=3;i++)
{
inner:for(int j=1;j<=3;j++)
{
if((i==2)&&(j==2))
{
System.out.println();
continue outer;
}
System.out.print("("+i+","+j+")"); //print is used to display cursor in same line.
}
System.out.println();
}
System.out.println("End of For Loops");
}
}
Output
Labelled Continue-javaform
                            To get the code file use me.

Continue to the next topic Getting input from the keyboard.

Begin your career in Digital Marketing,What is digital marketing? Digital Marketing online course. It's an current evolving technology which can give support to establish your own startup through Digital Marketing

Do check my new startup Surprise Planners in HyderabadLavish Surprises our services are surprise party planners in Hyderabad, surprise giftssurprise birthday party planners Hyderabad, Wedding anniversary surprises, surprise planners Hyderabad.

Hi Friends, Please comment down your views in the comment section below. Thank you...

No comments:

Connect with Us by Subscribing here for more Updates..!

* indicates required