Purpose of Return statement.

0

Purpose of Return statement:
/*Basically return statement is used to terminate method execution and for sending control back to calling method.
-> "return;" sends control back to calling method without value.
-> "return <value>;" send's control back to calling method with value. */

//What is the Output from the below program.
//PORStatement.java

class PORStatement
{
static void method1(int a)
{
System.out.println("Before if");
if(a==12)
{
System.out.println("In if");
return;
}
System.out.println("After if");
System.out.println("Hi");
}
static int method2(int a)
{
System.out.println("Before if");
if(a==12)
{
System.out.println("In if");
return a+10;
}
System.out.println("After if");
System.out.println("Hi");
return 30;
}
public static void main(String[] args)
{
method1(12);
method1(24);

method2(12);
method2(24);
}
}

compilation:
G:\netlojava>javac PORStatement.java

Execution:
G:\netlojava>java PORStatement

Out Put:

Before if
In if
Before if
After if
Hi
Before if
In if
Before if
After if
Hi

About the author

Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus. Aenean fermentum, eget tincidunt.

0 comments:

Recent Posts