JavaScript Break and Continue (Examples) -

JavaScript Break and Continue (Examples)

JavaScript Break and Continue (Examples)

JavaScript, as a versatile and powerful programming language, offers developers a plethora of tools to efficiently manipulate data and control program flow. Among these tools, the break and continue statements stand out as indispensable tools for managing loops. In this article, we’ll delve into the functionality and usage of these statements, exploring how they can enhance the efficiency and readability of your code. In this blog we will learn JavaScript Break and Continue (Examples).

Understanding the Break Statement

The break statement in JavaScript serves a simple yet crucial purpose: it allows you to abruptly exit a loop structure, be it a for, while, or do-while loop. When encountered within the body of a loop, the break statement immediately terminates the loop’s execution and transfers control to the statement following the loop.

In others word It breaks the iteration of a loop at a specific condition

Syntax- break;

Example-1 WAP to terminate the loop when i value become 6.

<html>

<script>

for (i=1;i<=10;i++ )

{

            if (i==6)

            {

                  break;

            }

            document.write(“<br>”+i);

}

</script>

</html>

Output

1

2

3

4

5

6

Example-2 WAP to print area of circle 3 times loop terminate when user enter radius as 0

<html>
<script>
for (i=1;i<=3;i++ )
{
var r=parseInt(prompt(“enter the radius”));
if (r==0)
{
document.write(“<br>loop terminated by break statement..”);
break
}
var area=3.14*r*r;
document.write(“<br>Area of circle=”+area);
}
</script>
</html>
In the above example if we do not enter r value 0 than you will get 3 times area of circle but if you enter r value 0 then loop terminate and you got the message “Loop terminated by break statement..”. If you enter first time value of r=5 and second time value of r=0 then you will get the below output-
Output
Area of Circle=78.5
Loop terminated by break statement..
Watch Our Live class Video on YouTube by click on the below link

 

Utilizing the Continue Statement

While the break statement allows for premature termination of a loop, the continue statement provides a means to skip the current iteration and proceed to the next one. When encountered within the body of a loop, the continue statement effectively bypasses the remaining code within the loop’s body for the current iteration and moves on to the next iteration.

Example-1 
<html>
<script>
for (i=1;i<=10;i++ )
{
if (i==6)
{
continue;
}
document.write(“<br>”+i);
}
</script>
</html>
Output
1
2
3
4
5
7
8
9
10
Note-6 skip in the iteration

Example-2 WAP to print area of circle 5 times loop terminate when user enter radius as 0 and skip the iterate when user enter negative value.

<html>
<script>
for (i=1;i<=5;i++ )
{
var r=parseInt(prompt(“enter the radius”));
if (r==0)
{
document.write(“<br>loop terminated by break statement..”);
break;
}
if (r<0)
{
continue;
}
var area=3.14*r*r;
document.write(“<br>Area of circle=”+area);
}
</script>
</html>
Example-3 WAP to print the sum of only the positive number.
<html>
<script>
var sum=0
for (i=1;i<=10;i++ )
{
var n=parseInt(prompt(“enter the number”));
if (n<0)
{
continue;
}
sum=sum+n;
}
document.write(“Sum of the positive number=”+sum);
</script>
</html>

Practical Applications

Both the break and continue statements find numerous practical applications in JavaScript programming. Here are a few common scenarios where they can be particularly useful:

Searching within Arrays: When iterating over an array, the break statement can be used to exit the loop once a desired condition is met, while the continue statement can be employed to skip certain elements based on specific criteria.

Validating Input: In scenarios where user input needs to be validated, these statements can be used to efficiently handle edge cases or invalid inputs without executing unnecessary code.

Optimizing Loops: By strategically placing break and continue statements within loops, you can improve the efficiency of your code and minimize unnecessary iterations.

Conclusion

In conclusion, the break and continue statements are powerful tools in JavaScript for controlling the flow of loops. Whether you need to prematurely exit a loop or skip certain iterations, these statements provide essential mechanisms for achieving such behavior. By mastering their usage, you can write more efficient and readable code, enhancing the overall quality of your JavaScript applications.

Also Check Our Latest Uploads

JavaScript looping statements with examples

types of conditional statements in javascript

 

1 thought on “JavaScript Break and Continue (Examples)”

Leave a Comment