Friday, 29 May 2015

Java Jump Statements

Jump statements are used for breaking the control flow. Java supports the following jump statements:

  • break
  • labeled break
  • continue
  • labeled continue
  • return
Beside this the constructs used for exception handling can also be put into this category but they are discussed in a separate chapter.

break Statement

The general form of simple break statement is:

break;

The unlabeled break statement can be used inside loops (for, while and do-while) and switch statements. It terminates the containing statement and transfers the control outside the closest enclosing loop or switch statement. The rest of containing statement body is skipped and the execution continues after the containing statement.

This is similar to break statement in C/C++. Refer to examples of simple-if statement and switch statement discussed above for the use of break statement.

labeled break Statement

The syntax of the labeled break statement is as follows:

break  <label>;

The labeled beak statement is used to terminate the block whose label is specified in the break statement. Unlike simple break statement, you can terminate any block. For example, it is possible to terminate the outermost loop from inside a deeply nested for loop.

The break statement can also be used to terminate a simple block (i.e. the block need not be a loop or switch statement)

Example: The following program searches for a given element inside a matrix (2-D array) and makes use of the labeled break statement to come out of the outermost for loop as soon as the element is found.

import java.util.*;

class SearchMatrix

{ public static void main(String args[]) throws IOException

{ int i, j, x, row, col , matrix[][];

String number;

boolean found = false;

Buffered Reader in

= new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter the number of rows in the matrix: ");

number = in.readLine();

row = Integer.parseInt(number);

System.out.print("Enter the number of columns in the matrix: ");

number = in.readLine();

col = Integer.parseInt(number);

matrix = new int[ row][col];

for(i = 0; i < row; i++) //enter one element in one line

{ System.out.println("enter the elements of row " + (i+1));

for(j = 0; j < col; j++)

{ number = in.readLine();

}

}

System.out.print("Enter the element to be searched : ");

number = in.readLine();

x = Integer.parseInt(number);

outerloop: for(i = 0; i < row; i++)

matrix[i][j]= Integer.parseInt(number);

{

for(j = 0; j < col; j++)

{

if(matrix[i][j] == x)

{ found = true;

}

 break outerloop;
}

}
}
if(found)

System.out.println("The number "+ x + " is present");

else

System.out.println("The number " + x + " is not present");
}
}

The use of labeled break statement in the above program is a substitute of the labeled goto statement of C/C++. This is a better alternative, as the program is more readable hence the labeled break is referred to as the civilized form of goto statement.

No comments:

Post a Comment