The break statement, which is used to exit a loop early.
A label can be used with a break to control the flow more precisely. A label is simply an identifier followed by a colon(:) that is applied to a statement or a block of code.
Note: there should not be any other statement in between a label name and associated loop.
Example-1: Break from nested loop
<!DOCTYPE html>
<html>
<head>
<title>
Break Nested For loop
</title>
</head>
<body>
<script type="text/javascript">
<!--
document.write(
"Entering the Geeks For Geeks!<br /> ");
for (var i = 0; i < 5; i++) {
document.write(
"For Upper Level in GfG : " + i + "<br />");
document.write("<br />")
for (var j = 0; j < 5; j++) {
if (j == 3) break;
document.write(
"For Deeper Level in GfG : " + j + " <br />");
}
if (i == 3) break;
}
document.write("Exiting the Geeks For Geeks!<br /> ");
</script>
</body>
</html
|
Output:
Entering the Geeks For Geeks!
For Upper Level in GfG : 0
For Deeper Level in GfG : 0
For Deeper Level in GfG : 1
For Deeper Level in GfG : 2
For Upper Level in GfG : 1
For Deeper Level in GfG : 0
For Deeper Level in GfG : 1
For Deeper Level in GfG : 2
For Upper Level in GfG : 2
For Deeper Level in GfG : 0
For Deeper Level in GfG : 1
For Deeper Level in GfG : 2
For Upper Level in GfG : 3
For Deeper Level in GfG : 0
For Deeper Level in GfG : 1
For Deeper Level in GfG : 2
Exiting the Geeks For Geeks!
Example-2: Break from nested loop using Labels.
<!DOCTYPE html>
<html>
<head>
<title>
Break Nested For loop Using Labels
</title>
</head>
<body>
<script type="text/javascript">
<!--
document.write("Entering the Geeks for Geeks!<br /> ");
upperloop:
for (var i = 0; i < 5; i++) {
document.write(
"For Upper Level in GfG : " + i + "<br />");
document.write("<br />");
deeperloop:
for (var j = 0; j < 5; j++) {
if (j > 3) break;
if (i == 2) break deeperloop;
if (i == 3) break upperloop;
document.write("For Deeper Level in GfG: "
+ j + " <br />");
}
}
document.write("Exiting the Geeks For Geeks!<br /> ");
</script>
</body>
</html>
|
Output:
Entering the Geeks for Geeks!
For Upper Level in GfG : 0
For Deeper Level in GfG: 0
For Deeper Level in GfG: 1
For Deeper Level in GfG: 2
For Deeper Level in GfG: 3
For Upper Level in GfG : 1
For Deeper Level in GfG: 0
For Deeper Level in GfG: 1
For Deeper Level in GfG: 2
For Deeper Level in GfG: 3
For Upper Level in GfG : 2
For Upper Level in GfG : 3
Exiting the Geeks For Geeks!
Learn to code easily with our course
Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
14 May, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...