Wednesday 25 January 2017

Control Statements in Java ( Jumping Statements )


                                          
Control Statements in Java ( Jumping Statements )


 Java has 3 jump statements break , continue and return. These statement transfer the control to the other part of programs.

Break

In java , break has 3 uses : first , it terminates a statement sequence in a switch statement . second , it can be used to exit a loop. Third , it can be used as a civilized form of goto.

Using break to exit loop

By using break , you can force immediate termination of  a loop , by passing the conditional expression and any remaining code in the body of loop.

When a break is encountered inside a loop , the loop is terminated and program control resumes at the next statement following the loop.

Program:

// To use break to exit the loop
  class BreakExitDemo
 { 
           public static void main ( String [] args )
        {
             for ( int i=0 ; i<100 ; i++ )                         Output:
           {                                                                      i=o
                    if ( i== 10 )                                            i=1 
                    break ;                                                     . 
                    System.out.println ( " i=" + i);                .  
           }                                                                        .
         System.out.println ( " Loop Complete");          i=9  
        }                                                                       Loop Complete
 }  

Using break as Goto

Since java does not support goto statement , because it provides a way to branch in an arbitrary and unstructured manner. So , we use break statement as it can also be employed by itself to provide a civilized form of goto statement.

It's general form is :

             break Label ;
here , label is a name of a label that identifies a block of code .

Program:

 // using break as a  civilized form of goto.
         class BreakDemo
      {
           public static void main ( String [] args )
         { 
                boolean t = true ;
      
   first:  {
        second:  {
               third:    { 
                             System.out.println ( " Before break. ");
                              if (t) break second ;
                              System.out.println (" This won't execute ");
                           }
                          System.out.println (" This won't execute " );
                      }
                       System.out.println ( " This is after second block ");
                }
         }
  }
                                           Output:
                                      Before Break
                                     This is after second block.

Continue

Sometimes it is useful to force an early iteration of a loop , i.e , you might want to continue running the loop , but stop processing the remainder of the code in its body for this particular iteration. The continue statement performs such an action.

 Program:

// To demonstrate continue statement
      class ContinueDemo
  {
       public static void main ( string [] args )
        {
               for ( int i=0 ; i<10 ; i++)                          Output:
           {                                                                      0  1
                  System.out.println (i + "  " );                   2  3
                 if ( i % 2 == 0 )                                         4  5
                    continue ;                                               6  7
                 System.out.println ( " " );                         8  9
            }
        }
    }

return

The return statement is used to explicitly return from a method. That is , it causes program control to transfer back to the caller of the method.

At any time in the method the return statement can be used to cause execution to branch back to the caller of the method. Thus , the return statement immediately terminates the method in which it is executed.

Program:

// To demonstrate return
     class ReturnDemo
      {
            public static void main ( String [] args )
           {
              boolean t = true;
             System.out.println (" Befor the return , ");
         
                   if ( t)
            return ;
               System.out.println ( " This won't execute ");
            }
       }
           Output:
         Before the return
                  
                                                                            By: Knowledge Bits

No comments:

Post a Comment

Video of the Day

Contact us

Name

Email *

Message *