Java

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:

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 :

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:

Break and Continue

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:

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:

Break and Continue


If we remove the break keyword from the case, it becomes like this:

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:

Break and Continue


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:

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:

Break and Continue

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.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button