Transfer Statements in Java – Flow Control in JAVA

Explanation of 2 categories of flow control are already studied in the previous part, moving on to 3rd category of flow control:

3.Transfer Statements:

Statements which can transfer the execution from one block of code to other block of code is known as transfer statements.

Break statement in Java :

Break statement is used in the program to stop the further execution of the code and come out of the block. It is generally used in switch statements, loops and in any other block to stop the execution. Syntax:

break;

For example,

for(int i=0;i<5;i++)                  //starting a loop
{
if(i==3)                          //break on certain condition
{
break;
}
System.out.println(i);            //if the condition is false then printing
}

Output:

0
1
2

Note- If break statement is used out of loop or switch statements then it will give the compile time error.


Continue statement in Java :

Instead of breaking any iteration if one needs to continue it, then in such condition we use continue statement. Syntax:

continue;

Just like break statement, continue also should be used in a iteration block, otherwise we will get compile time error. For example:

for(int i=0;i<5;i++)        //starting an iterative loop
{
if(i==1)                    //condition
{
continue;                   //if the condition is true then rest of the block will not execute
}
System.out.println(i);      //if the condition is false then this line of code will be executed
}

Output:

0
2
3
4

Labelled Break & Continue:

In nested iterative loops break and continue statements can be get easily mixed up, so in such situations we use labelled break and continue statements. For example:

label1:
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
if(i==j)
{
//break or continue statement
}
System.out.println(i+"-----"+j);
}
}

If in the above code we replace comment by normal and labelled break and continue statement then the output will be different.
Output:

//Output when normal break statement is used as "break;"
1-----0
2-----0
2-----1

//Output when labelled break statement is used as "break label1;"
no output

//Output when normal continue statement is used as "continue;"
0-----1
0-----2
1-----0
1-----2
2-----0
2-----1

//Output when normal continue statement is used as "continue label1;"
1-----0
2-----0
2-----1

Return:

Return statement is used mostly in functions. It is used to return a value of particular data type. Return statement can only return value of the type which is stated in function’s prototype. For example:

public return_datatype function_name(arguments)
{
//code
return return_datatype;
}

If the datatype in the prototype is void then return statement is not written in the function’s body. For example:

public void function1()
{
System.out.println("This is a function with void return type.");
}
public int function2()
{
System.out.println("This is a function with int return type. Any value in the range of integer can be passed.");
return 0;
}

Return type of a function can also be an object of a class. So in that case too, return statement is used. For example,

public ArrayList<String> show()
{
ArrayList<String> str = new ArrayList<String>();  //creating object of arraylist of strings
//code
return str;                                       //returning the object
}

Try statement in Java :

Usage of try statement is to put the code into it which can probably throw any exception. It protects the program from crash!! With the try statement at least one catch statement is necessary. Syntax:

try
{
//code
}
catch()
{
//code
}

Catch statement in Java :

Catch statements are executed only if any exceptions are thrown in the try block otherwise they are never executed. For a single try block there can be many catch blocks. For example:

try
{
//code
}
catch()
{
//code
}
.
.
.
catch()
{
//code
}

Catch block takes an object of any exception class as its argument. But one thing should be kept in mind if multiple catches are placed that is parent exception class should not be placed before child class otherwise the child class code will never execute. For example:

int i=0,j;
try{
j=1/i;
System.out.println(j);
}
catch(ArithmeticException ae)
{
System.out.println("Arithmetic Exception is caught.");
}
catch(Exception e)
{
System.out.println("Exception is caught.");
}

Output:

Arithmetic Exception is caught.

 


Finally statement in Java :

Once the exception is thrown in catch block, compiler will execute the code of catch block and will terminate. But if one needs to execute some lines of code necessarily even if any exception is thrown then finally block is used. For example:

try{  
int data=25/5;  
System.out.println(data);  
}  
catch(NullPointerException e)
{
System.out.println("NullPointerException is caught.");
}  
finally
{
System.out.println("Executing finally block.");
}  
System.out.println("Rest code");  
}

Output:

NullPointerException is caught.
Executing finally block.

 

Leave a Comment

Your email address will not be published. Required fields are marked *