Friday, 20 November 2015

Java Tutorial : Java Break with Label


Click here to watch in Youtube : 
https://www.youtube.com/watch?v=dvfsD6wGtXs&list=UUhwKlOVR041tngjerWxVccw

BreakDemo.java
class BreakDemo
{
    public static void main(String[] args)
    {

        /*
         * An unlabeled break statement terminates the innermost switch, for,
         * while, or do-while statement.
         */

        for (int i = 0; i < 3; i++)
        {
            System.out.println("i : " + i + "\n");
            for (int j = 0; j < 4; j++)
            {
                System.out.println("j :" + j);
                if (j == 2)
                {
                    break;
                }
                System.out.println("j : after break :" + j);
            }
            System.out.println("for loop j completed");
            System.out.println("-------------------------");

        }
        System.out.println("for loop i Completed");

    }
}
Output
i : 0

j :0
j : after break :0
j :1
j : after break :1
j :2
for loop j completed
-------------------------
i : 1

j :0
j : after break :0
j :1
j : after break :1
j :2
for loop j completed
-------------------------
i : 2

j :0
j : after break :0
j :1
j : after break :1
j :2
for loop j completed
-------------------------
for loop i Completed
LabelBreakDemo.java
class LabelBreakDemo
{
    public static void main(String[] args)
    {

        /*
         * labeled break terminates an outer statement.
         */

        stop:
        for (int i = 0; i < 3; i++)
        {
            System.out.println("i : " + i + "\n");
            for (int j = 0; j < 4; j++)
            {
                System.out.println("j : " + j);
                if (j == 2)
                {
                    break stop;
                }
                System.out.println("j : after break :" + j);
            }
            System.out.println("for loop j completed-------------------------");

        }

        System.out.println("for loop i Completed");

    }
}
Output
i : 0

j : 0
j : after break :0
j : 1
j : after break :1
j : 2
for loop i Completed
Click the below link to download the code:
https://sites.google.com/site/javaee4321/java/ControlFlowDemoBreakLabelApp.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/ControlFlowDemoBreakLabelApp

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/9f2088c41ff235b2716289e24cabde9d7d07fb91/BasicJava/ControlFlowDemoBreakLabelApp/?at=master

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • No comments:

    Post a Comment

    Google+ Followers

    Google+ Badge