+ 3
Why we use 'break' in java?
Why we use 'break' in java?
4 Respuestas
+ 14
//to come out of a loop
//example :::
int x = 1;
while(x > 0) {
System.out.println(x);
if(x == 4) {
break;
}
x++;
}
/* Outputs
1
2
3
4
*/
//hope it helps☺
+ 11
Loop is immediately terminated and the program control resumes at the next statement following the loop.
If you don't use break then the loop will not be terminated and the program will not give you the output.
+ 4
I understood.
'break' is to stop any loops.
+ 4
When the break statement is encountered inside a loop, the loop is immediately terminated and the program control resumes at the next statement following the loop. And It can be used to terminate a case in the switch statement.
for example--
public class Test {
public static void main(String args[]) {
int [] numbers = {1, 2, 3, 4, 5};
for(int x : numbers ) {
if( x == 3 ) {
break;
}
System.out.print( x );
System.out.print("\n"); } } }
it's output will be---
1
2