JAVA Break, Continue And Return
Java has three type of branching control keywords: break, continue and return. These keywords are used to change the normal flow of execution of program.
Break keyword :
The break keyword is used to stop the execution of the loops. In many situations we need to get out of the loop before the loop execution is complete. Whenever a break keyword is encountered inside a loop the execution gets out of loop and start executing the next line of code after the loop. The break statement is of two types: labeled and unlabeled.
Unlabeled break statement:
Unlabeled break statement is used to get out of a single loop. If unlabeled break is used inside the nested loops it will exit only from the loop in which break is used. Lets see the example below:
public class UnlabeledBreak { public static void main(String[] args) { |
Output of Program:
Labeled Break Statement:
Labeled breaks statement is used when we need to break the nested loops. Lets understand the difference between Labeled and Unlabeled break statement with an example.
In this example we need to traverse a 2D array and print the elements of array until a particular element is found. To do this we have to use nested loops. Now to break and get out of the nested loops we need to use labeled break.
Output of Program:-
Continue Keyword:- Continue statement statement is used to skip a single iteration of a loop. Whenever a continue keyword is encountered inside a loop, the current iteration of loop ends just like a normal end of any iteration without executing the statement written after the continue statement inside the loop. Just like break statement continue statement is also of two types: labeled and unlabeled.
Unlabeled Continue Statement:
Output of Program:
Labeled Continue Statement:
Output of Program:-
Return Keyword:- Return statement is a bit different form break and continue as break and continue changes the normal control flow of loops but return statement works on the control flow of the method. Return statement stops execution of the method and transfer the control back to the caller of the method. The return statement can be used in two ways, return statement that returns a value and another that does not return a value. Return statement must return the same type of value as defined in the declaration of method. If the method is defined as void than return statement will not return any value.
The use of return statement will be more clear in upcoming tutorials and we will be using it more and more now.
In this example we need to traverse a 2D array and print the elements of array until a particular element is found. To do this we have to use nested loops. Now to break and get out of the nested loops we need to use labeled break.
public class LabeledBreak { public static void main(String[] args) { |
Output of Program:-
Continue Keyword:- Continue statement statement is used to skip a single iteration of a loop. Whenever a continue keyword is encountered inside a loop, the current iteration of loop ends just like a normal end of any iteration without executing the statement written after the continue statement inside the loop. Just like break statement continue statement is also of two types: labeled and unlabeled.
Unlabeled Continue Statement:
public class UnlabeledContinue { public static void main(String[] args) { |
Output of Program:
Labeled Continue Statement:
public class ContinueLabel { public static void main(String args[]) { for (int i = 1; i < 10; i++) { |
Output of Program:-
Return Keyword:- Return statement is a bit different form break and continue as break and continue changes the normal control flow of loops but return statement works on the control flow of the method. Return statement stops execution of the method and transfer the control back to the caller of the method. The return statement can be used in two ways, return statement that returns a value and another that does not return a value. Return statement must return the same type of value as defined in the declaration of method. If the method is defined as void than return statement will not return any value.
public class ReturnStatement {
} } } |
The use of return statement will be more clear in upcoming tutorials and we will be using it more and more now.
Plz give email ,i want some ip java project with my sql as backend for my project have sumbit on 28 ,plz contact me ,my
ReplyDeleteEmail-shivamkumar75@gmail.com
Plz give warna mere prac. Ke no to gaye.
I am technology Enthusiast. Your blog is really awesome, attractive and impressive. I like the way you think. it is very useful for Java SE & Java EE Learners. Your article adds best knowledge to our Java Online Training from India. or learn thru Java Online Training from India Students. or learn thru JavaScript Online Training from India. Appreciating the persistence you put into your blog and detailed information you provide. Kindly keep blogging.
Deleteperfect explanation about java programming .its very useful.thanks for your valuable information.java training in chennai | java training in velachery
ReplyDeleteVery informative ..i suggest this blog to my friends..Thank you for sharingjava training in chennai | chennai's no.1 java training in chennai | best java institute in chennai
ReplyDeleteThe knowledge of technology you have been sharing thorough this post is very much helpful to develop new idea. here by i also want to share this.
ReplyDeleteDigital Marketing Training in Chennai
Digital Marketing Training in Bangalore
digital marketing training in tambaram
digital marketing training in annanagar
Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
ReplyDeletefull stack developer training in annanagar
full stack developer training in tambaram
full stack developer training in velachery
Thank you for allowing me to read it, welcome to the next in a recent article. And thanks for sharing the nice article, keep posting or updating news article.
ReplyDeletepython training institute in chennai
python training in Bangalore
python training institute in chennai
Excellant post!!!. The strategy you have posted on this technology helped me to get into the next level and had lot of information in it.
ReplyDeleteBlueprism training in Chennai
Blueprism training in Bangalore
Your very own commitment to getting the message throughout came to be rather powerful and have consistently enabled employees just like me to arrive at their desired goals.
ReplyDeleteData Science training in Chennai
Data science training in bangalore
Data science online training
Data science training in pune
I read this post two times, I like it so much, please try to keep posting & Let me introduce other material that may be good for our community.
ReplyDeletejava training in jayanagar | java training in electronic city
java training in chennai | java training in USA
Useful information.I am actual blessed to read this article.thanks for giving us this advantageous information.I acknowledge this post.and I would like bookmark this post.Thanks
ReplyDeleteangularjs Training in bangalore
angularjs Training in btm
angularjs Training in electronic-city
angularjs online Training
angularjs Training in marathahalli
Your article increases the curiosity to learn more about this topic. Keep sharing your information regularly for future reference.
ReplyDeleteSelenium Training in Chennai
selenium course
iOS Course in Chennai
iOS Training Chennai
JAVA Training in Chennai
JAVA Course in Chennai
Whoa! I’m enjoying the template/theme of this website. It’s simple, yet effective. A lot of times it’s very hard to get that “perfect balance” between superb usability and visual appeal. I must say you’ve done a very good job with this.
ReplyDeleteAWS Training in Bangalore | Amazon Web Services Training in bangalore , india
AWS Training in pune | Amazon Web Services Training in Pune, india
AWS Training in Chennai|Amazon Web Services Training in Chennai,India
aws online training and certification | amazon web services online training ,india
I accept there are numerous more pleasurable open doors ahead for people that took a gander at your site.
ReplyDeletesafety course in chennai
Thanks for Sharing!
ReplyDeleteJava Training in Chennai
Python Training in Chennai
IOT Training in Chennai
Selenium Training in Chennai
Data Science Training in Chennai
FSD Training in Chennai
MEAN Stack Training in Chennai
Excellent post, it will be definitely helpful for many people. Keep posting more like this.
ReplyDeleteAzure Training in Chennai
Microsoft Azure Training in Chennai
Data Science Course in Chennai
Data Science Training in Chennai
DevOps certification in Chennai
DevOps Training in Chennai
Azure Training in Velachery
Azure Training in Tambaram
Your blog gave me a piece of idea on the concept I am searching for. Thanks for sharing.
ReplyDeleteBest English Classes in Mumbai
Best English Speaking Course in Mumbai
Best Spoken English Classes in Mumbai
Best English Speaking Institute in Mumbai
English Coaching Classes in Mumbai
Spoken English Classes in Chennai
Spoken English Class in Chennai