Break and Continue Statements in java with Examples using NetBeans
Break and Continue Statement in Java
Break and Continue Statements in java with Examples using NetBeans- In Java, the keywords Break and Continue are control structures that are generally used in loops such as while, do-while, and for loops or other statements such as switch-case, these two keywords are used in a statement to stop and continue statements in statements at certain conditions In this tutorial we will learn about how to use Break and Continue in Java, here we will create a simple example program using these two keywords.
Read related articles:
- If and else statements in Java
- Looping (while, do-while, for loops) tutorial in Java
- Use of switch and case statements in Java
Break keyword in java:
The break is generally used in java to end execution in a statement, by using the Break keyword, the statement in the statement will immediately stop, so we must create a condition. when the program will stop. for loops are used when we want to do something special like if the value reaches a certain number or more than a certain number then we will stop the program.
Example: how to use break statement in java using Netbeans :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package com.mycompany.javabasics; /** * * @author Fawadkhan */ public class Break_ContinueExample { public static void main(String[] args){ for(int i=0; i<7; i++){ if(i == 5){ break; }else{ System.out.println(i); } } } } |
output:
Where when the value of the variable i reaches 5, the program will stop completely and the statements in the for loops will not be re-executed. In addition to looping statements, in general, when we use switch-case statements, the statements in the case need to be added or ended with a break statement.
example: how to use break statement in switch case in java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
package com.mycompany.javabasics; /** * * @author Fawadkhan */ public class Break_ContinueExample { public static void main(String[] args){ int language = 2; switch(language){ case 1: System.out.println("C++"); break; case 2: System.out.println("Java"); break; case 3: System.out.println("Python"); break; default: System.out.println("Language not Found"); } } } |
output:
If we remove the break keyword from the case, it becomes like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
package com.mycompany.javabasics; /** * * @author Fawadkhan */ public class Break_ContinueExample { public static void main(String[] args){ int language = 2; switch(language){ case 1: System.out.println("C++"); case 2: System.out.println("Java"); case 3: System.out.println("Python"); default: System.out.println("Language not Found"); } } } |
output:
Continue Statement in java using NetBeans:
Continue keyword Unlike Break, the Continue keyword will not stop the program when executed, for example, using for loops but will only skip or skip it.
Examples are as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package com.mycompany.javabasics; /** * * @author Fawadkhan */ public class Break_ContinueExample { public static void main(String[] args){ for(int i=0; i<10; i++){ if(i == 4){ continue; }else{ System.out.println(i); } } } } |
output:
Try to pay attention, when the value in the variable i is at number 4, the program will skip or skip the execution, and the for loops will immediately continue on the next iteration without printing the value 4 first. That’s a basic tutorial on using Break and Continue in Java, hopefully, it’s useful.