Labelled Statement in JAVA
A labelled statement is simply a statement, there has been named by pretending an identifier and a colan (:) to it. The label are used by break and continues statement.Labelled Statement in JAVA :-
- break statement.
- continue statement.
Labelled Statement in java
1 )- break statement :- break statement is one of the several control statement. JAVA provides control to the flow of a program as the name says, break statement is generally used to break the loop of the switch statement.
Note :- JAVA doesn't provide go to statement like other programming languages.
break statement has two form.
1 )- Labelled break Statement :- Labelled break statement is used when we want to jump out of the nested or multiple loop statement
ex :-
class test
{
public static void main(String args[])
{
int a[] = {10, 20 , 30, 40};
outer :
for (int i = 0; i<a.length; i++)
{
System.out.println(a[i]);
if(a[i] == 30)
{
break outer;
}
}
}
}
2 )- Unlabeled Break Statement :-Unlabeled break statement is used to jump out of the loop where specific condition occurs.
ex :-
class test
{
public static void main(String args[])
{
int i = 0;
for (i = 0; i<10; i++)
{
System.out.println(i);
if(i == 5)
{
break;
}
}
}
}
2 )- Continue Statement :- Continue Statement is used when we want to skip the rest of the statement of the body of the loop and continue with the next iteration of the loop.
ex :-
class test
{
public static void main(String args[])
{
int i = 0;
for ( i = 0; i<10; i++)
{
System.out.println(i);
if(i == 5)
{
continue;
}
}
}
}
3 )- Return Statement :- The last control statement is return, The return is the statement that terminates the execution of the method in which it appears and return controls to the calling method. it also returns the value of the expression. if the method is of type void, the return can be omitted.
Syntax :-
return expression;
ex:-
class test
{
int add_num(int a, int b)
{
return (a+b);
}
int sub_num(int a, int b)
{
int c = a*b;
System.out.println(c);
}
public static void main(String args[])
{
test t1 = new test();
t1.sub_num(10, 20);
int c = e1.add_num(10, 20);
System.out.println(c);
}
}
|
0 Comments