5

i am new to java and i see some complicated code while see solution for my program on the net

int a=5;
int b=10;
first:{
    second:{
        third:{
        if(a==b>>1){
            break second;
       }
           System.out.println(a);
    }
    System.out.println(b);
}

can anybody explain me what is the meaning of this type of code and how we deeply use it in program and i cant find this type of code in my java book please help me thanks in advance

Radiodef
  • 37,180
  • 14
  • 90
  • 125
Jagdish
  • 129
  • 1
  • 8
  • 5
    possible duplicate of [What's the point of using labeled statements in Java?](http://stackoverflow.com/questions/12070942/whats-the-point-of-using-labeled-statements-in-java) and [Please explain the usage of Labeled Statements](http://stackoverflow.com/questions/2710422/please-explain-the-usage-of-labeled-statements) – Baby Apr 29 '15 at 02:16
  • @Baby definitely duplicate – Ankur Anand Apr 29 '15 at 03:57
  • @AnkurAnand: I'm less inclined to agree. I don't see either of those questions as duplicates of this one. – Makoto Apr 29 '15 at 04:23

2 Answers2

0

There are some problems with that code. You have four '{' so you should have four '}' or it won't compile.

Break is typically used to get out of loops, but in this case it is taking you from inside one label to an outer label. If there were code outside of the if statement, but inside of the third label, it would be skipped because of the break. In this case it would not print a.

  • Apply consistent indenting and it's much easier to track where the scope starts/stops and (in this case) the missing closing brace. – Scott Sosna Apr 29 '15 at 03:06
0

In your codea==b>>1 gets passed
since b is 10,if we right shift it once it will be 5.so,a==5 is true and program control goes inside the third braces. Inside the third braces break second; statement makes the program control to get out of the second braces.So your output of the program prints nothing.
Labelled break statement used to exit the particular block.Please refer example Stackoverflow

Community
  • 1
  • 1
anavaras lamurep
  • 1,303
  • 2
  • 17
  • 33