The break Statement
The break keyword is used to break the statement
To break out of the statement labeled statementName use the following form of the break statement.
Syntax
Break statementName;
Labeled breaks are an alternative to the goto statement, which is not supported by the Java language.
Example for break statement
public class breakDemo
{
public static void main(String args[])
{
int n=6;
System.out.println(“Initial Value of n is: “+n);
while(true)
{
System.out.println(“Now the N value is : “+n);
--n;
If(n<2)
break;
}
System.out.println(“The out of the loop! “);
}
}
The Output will be:
Initial Value of n is: 6
Now the N value is : 6
Now the N value is : 5
Now the N value is : 4
Now the N value is : 3
Now the N value is : 2
The out of the loop!
No comments:
Post a Comment