MOCKSTACKS
EN
Questions And Answers

More Tutorials









Java break and continue


Break statement :


The break statement is used to exit the loop irrespective of whether the condition is true or false.
Whenever a ‘break’ is encountered inside the loop, the control is sent outside the loop.
Syntax :
break;

Example to demonstrate the use of break inside a for loop :


public class CWH_break {  
public static void main(String[] args) {  
    //using for loop  
    for(int i=10;i>0;i--){  
        if(i==7){  
            break;   //break the loop
        }  
        System.out.println(i);  
    }  
}  
}  

Output

10
9
8

Flow control of break statement :



Continue statement :


The continue statement is used to immediately move to the next iteration of the loop.
The control is taken to the next iteration thus skipping everything below ‘continue’ inside the loop for that iteration.
Syntax :
continue;

Example to demonstrate the use of continue statement inside a for loop :


public class CWH_continue {  
public static void main(String[] args) {  
 
    for(int i=7;i>0;i--){  
        if(i==3){  
            continue;//continue skips the rest statement
        }  
        System.out.println(i);  
    }  
}  
}  

Output

7
6
5
4
2
1

Flow control of continue statement :



In a Nut Shell …


1.break statement completely exits the loop
2.continue statement skips the particular iteration of the loop.


Conclusion

In this page (written and validated by ) you learned about Java break and continue . What's Next? If you are interested in completing Java tutorial, your next topic will be learning about: Java Arrays.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.