By using break,you can force immediate termination of a loop, bypassing the conditional expression and any remaininh code in the body of the loop.When a break statement is encountered inside a loop, the loop is immediately terminated and the program control resumes at the next statement following the loop.
The continue statement performs such an action. In while and do-while loops, a continue statement causes control to be transferred directly to the conditional expression that controls the loop.
This Java code defines a class break_continue that contains a main method. The main method defines a for loop that iterates from 1 to 10. Inside the loop, the code checks if the value of i is equal to 5 using an if statement. If i is equal to 5, the continue statement is executed which skips the current iteration and moves on to the next iteration of the loop.
If i is not equal to 5, the code prints the value of i using the System.out.println method. After printing the value of i, the code checks if the value of i is equal to 8. If i is equal to 8, the break statement is executed which terminates the for loop.
public class break_continue { public static void main(String args[]) { for (int i = 1; i <= 10; i++) { if(i==5) continue; System.out.println(i); if(i==8) break; } } }
1 2 3 4 6 7 8To download raw file Click Here
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions