Help: java break

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chathura86
    New Member
    • May 2007
    • 227

    #1

    Help: java break

    Hi.

    I have a little problem with java.

    Please check the following java script:
    Code:
    class Break{
       public static void main(String args[]){
       boolean t =  true;
    		
       a: {
             b: 
                c: {
                   System.out.println("Line 4");
                   if(t) break b;
                   System.out.println("Line 7");
                }
                System.out.println("Line 9");
    			
             System.out.println("Line 11");
          }
       }
    }
    and i found that the output is

    Line 4
    Line 9
    Line 11

    Please some one explain me how does this come

    Thanks & Regards,

    chathura
    Last edited by JosAH; May 11 '07, 04:59 PM. Reason: added [code] ... [/code] tags
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    #2
    Welcome to TSDN.

    if u r from C background then it ll be easy to understand this code.
    assumming this .. now look
    in java break label; or continue label; these serve as goto as in C.

    in java a label immediately follwed by a label or starts with a scope ... and the label ends where the scope ends.

    now look at this code ...

    A:
    B:
    {
    //Some lines ------------1 ==========it ll print.
    break A or B; //Here control ll jump to Some Lines 3
    //Some lines ---------------2 ============ it ll not.
    }
    //Some lines --------------3 ============ it ll print.

    now ..............

    A:
    //Some lines =========Here ll an error (undefined label A)
    B:
    {
    //Some lines ------------1 ==========it ll print.
    break A; //Here control ll jump to Some Lines 3
    //Some lines ---------------2 ============ it ll not.
    }
    //Some lines --------------3 ============ it ll print.

    now............ .

    A:
    //Some lines =========No error because B label defined well.
    B:
    {
    //Some lines ------------1 ==========it ll print.
    break B; //Here control ll jump to Some Lines 3
    //Some lines ---------------2 ============ it ll not.
    }
    //Some lines --------------3 ============ it ll print.

    so the label should be defined well with a scope or immediately follwed by another label, then two labels r well defined.

    now i think u understand my point of view.
    so u can figure out the output now.

    best of luck.
    kind regards.

    dmjpro

    Comment

    • chathura86
      New Member
      • May 2007
      • 227

      #3
      Thanks

      I think i understand it now. It was a good explanation
      got exactly what i need

      Thanks again

      regards

      Chathura

      Comment

      Working...