BREAK, Labeled break, CONTINUE, Labeled continue, RETURN, goTo - Branching statements with examples in java


Branching statements in java >
    • 1.1) break,
      • Labeled break statement>
    • 1.2) continue,
      • Labeled continue statement>
    • 1.3) return
    • 1.4) goTo (not used)



Branching statements are Control Flow Statements. Control Flow Statements are statements inside your source files which are generally executed from top to bottom, in the order that they appear.


Now, let’s discuss different branching statements in java in detail >
    • 1.1) break,
      • Labeled break statement>
    • 1.2) continue,
      • Labeled continue statement>
    • 1.3) return
    • 1.4) goTo (not used)

1.1) break
  • break statement
  • break is keyword in java.
  • break statement can be used inside >
  • loop (for, while, do-while) or
  • switch statement.
  • break statement can’t be used inside method.
  • break statement is used to stop the loop, flow of control reaches right to the next line where loop ends.
  • If break statement is used inside nested loops, than innermost loop will be stopped,  flow of control reaches right to the next line where innermost loop ends.

break;



Program 1.1.1 to demonstrate break statement>
/** JavaMadeSoEasy.com */
public class StatementTest {
   public static void main(String[] args) {
          for (int i = 0; i < 3; i++) {
                 if(i>1){
                       break;
                 }
                 System.out.println("i= " + i);
          }
         
   }
}
/* OUTPUT
i= 0
i= 1
*/

Program 1.1.2 to show - If break statement is used inside nested loops, than innermost loop will be stopped,  flow of control reaches right to the next line where innermost loop ends >
/** JavaMadeSoEasy.com */
public class StatementTest {
   public static void main(String[] args) {
          for (int i = 0; i < 3; i++) { //outer loop
                
                 System.out.println("outer i= " + i);
         
                 for (int j = 0; j < 3; j++) { //inner loop
                       if(j>0){
                              break;
                       }
                       System.out.println("inner i= " + i+", j= " + j);   
                 }//end inner loop
          }//end outer loop
         
   }
}
/* OUTPUT
outer i= 0
inner i= 0, j= 0
outer i= 1
inner i= 1, j= 0
outer i= 2
inner i= 2, j= 0
*/




Labeled break statement>
So far we have used unlabeled break statements, but now we will learn how to use labeled break statements via program.
Program 1.1.3 to demonstrate labeled break statement>
/** JavaMadeSoEasy.com */
public class StatementTest {
   public static void main(String[] args) {
          outerForLoop: // Labeled outer loop
                 for (int i = 0; i < 3; i++) {
                       System.out.println("outer i= " + i);
         
                       innerForLoop: // Labeled  inner loop
                              for (int j = 0; j < 3; j++) {
                                     if (j > 0) {
                                            break outerForLoop;
                                     }
                                     System.out.println("inner i= " + i + ", j= " + j);
                              }// end inner loop
                 }// end outer loop
  
   }
}
/* OUTPUT
outer i= 0
inner i= 0, j= 0
*/



1.2) continue statement

continue statement
  • continue is keyword in java.
  • continue statement can be used inside >
  • loop (for, while, do-while)
  • continue statement can’t be used inside
    • method or
    • switch.
  • continue statement is used to skip the current iteration of loop, flow of control reaches right to start of loop.
  • If continue statement is used inside nested loops, than current iteration of innermost loop will be stopped,  flow of control reaches right to the start of innermost loop.


continue;


Program 1.2.1 to demonstrate continue statement>
/** JavaMadeSoEasy.com */
public class StatementTest {
   public static void main(String[] args) {
          for (int i = 0; i < 3; i++) {
                 System.out.println("before continue > i= " + i);
                 if(i>1){
                       continue;
                 }
                 System.out.println("after continue > i= " + i);
          }
         
   }
}
/* OUTPUT
before continue > i= 0
after continue > i= 0
before continue > i= 1
after continue > i= 1
before continue > i= 2
*/

Program 1.2.2 - If continue statement is used inside nested loops, than current iteration of innermost loop will be stopped,  flow of control reaches right to the start of innermost loop >
/** JavaMadeSoEasy.com */
public class StatementTest {
   public static void main(String[] args) {
          for (int i = 0; i < 3; i++) { //outer loop
                
                 System.out.println("outer i= " + i);
         
                 for (int j = 0; j < 3; j++) { //inner loop
                       if(j>0){
                              continue;
                       }
                       if (i > 0) {
                              continue;
                       }
                       System.out.println("inner i= " + i+", j= " + j);   
                 }//end inner loop
          }//end outer loop
         
   }
}
/* OUTPUT
outer i= 0
inner i= 0, j= 0
outer i= 1
outer i= 2
*/


Labeled continue statement>
So far we have used unlabeled continue statements, but now we will learn how to use labeled continue statements.
Program 1.2.3 to demonstrate labeled continue statement>
/** JavaMadeSoEasy.com */
public class StatementTest {
   public static void main(String[] args) {
          outerForLoop: // Labeled outer loop
                 for (int i = 0; i < 3; i++) {
                       System.out.println("outer i= " + i);
                      
                       innerForLoop: // Labeled inner loop
                              for (int j = 0; j < 3; j++) {
                                     if (j > 0) {
                                            continue innerForLoop;
                                     }
                                     if (i > 0) {
                                            continue outerForLoop;
                                     }
                                     System.out.println("inner i= " + i + ", j= " + j);
                              }// end inner loop
                 }// end outer loop
   }
}
/* OUTPUT
outer i= 0
inner i= 0, j= 0
outer i= 1
outer i= 2
*/




1.3) return statement

The return statement exits from the current method and control flow returns to where the method was invoked.

There are 2 types of return statement >

  • return statement that doesn’t returns value >  in this case return type of method must be void.

  • return statement that returns value > in this case data type of the returned value must match with the data type of the method's declared return value.

Program 1.3.1 to demonstrate return statement that doesn’t returns value>
/** JavaMadeSoEasy.com */
public class StatementTest {
   public static void main(String[] args) {
          m();
   }
  
   static void m(){ //return type of method is void.
          System.out.println("in m()");
          return; //return nothing
   }
}
/* OUTPUT
in m()
*/


Program 1.3.2 to demonstrate return statement that returns value>
Method returns int
/** JavaMadeSoEasy.com */
public class StatementTest {
   public static void main(String[] args) {
          System.out.println("m() has returned > "+m());
   }
  
   static int m(){ //data type of the method's declared return value is int.
          System.out.println("in m()");
          return 1; //data type of the returned value is int
   }
}
/* OUTPUT
in m()
m() has returned > 1
*/




1.4) goTo statement (not used)

goTo is a keyword in java, but not used.

It was added java 1.0 to be used in a later version of Java, but wasn’t implemented in java.

As goTo is not implemented, any attempt to use goTo will cause compilation error,

We can use labeled break (programs 1.1.3 ) or labeled continue (As used in above programs 1.2.3 ).


RELATED LINKS>

Primitive, Custom/reference Data Types, Integer, Floating-Point, Character and String literal, Escape sequence in java, decimal to hexaDecimal and binary conversion program


Arithmetic, Unary, Equality, Relational, Conditional, Bitwise, Bit Shift, Assignment, instanceof operators in detail with programs.


No comments:

Post a Comment