Int x=100; while(x!=0) { system.out.println (x); if(x%6==0) break; x--; }
Answers
The output is
100
99
98
97
96
Explanation:
In the below program, while loop should run for value of x from 100 to 1 but since break statement is written with in loop and when x divisible by 6 first , the loop will get terminated and the program control pass to the next immediate statement written after the body of while loop.
Java program to use break statement
public class Main
{
public static void main(String[] args) {
int x=100; // x initialized to 100
while(x!=0) // loop executes till x != 0
{
System.out.println(x); // print x
if(x%6==0) // loop breaks when x is divisible by 6 that value is 96
break;
x--; // decrement x by 1
}
}
}
Output
100
99
98
97
96