JavaScript Break and Continue statement

JavaScript Break and Continue statement



Javascript break statement.

In Javascript, the brake statement is used to exit any type of loop. Loop found in JavaScript, such as Javascript for loop, or javascript while Loop and Javascript do loop. When executing these JavaScript loops, if the compiler in these JavaScript loop finds a break statement inside them, then the Javascript loop will stop executing the statement and will exit immediately from the loop.


Important: In the Break statement switch case in Javascript, remember that. If you do not use brakes in javascript, the compiler will not be able to exit the javascript switch case.



Break Statement Examples-


<!DOCTYPE html>
<html>
<body>
<h1>javascript break statement. </h1>
<p>Click on the button to loop in javascript with a break. And the loop has to output from 0 to 4, but the break statement pulls you out of the loop. When this variable is equal to "3" .</p>
<button onclick="myFunction()">Try it</button>
<p id="web"></p>
<script>
function myFunction() {
    var text = ""
    var i;

    for (i = 0; i < 5; i++) {
        if (i === 3) {
            break;
        }
        text += "The number is " + i + "<br>";
    }
    document.getElementById("web").innerHTML = text;
}
</script>
</body>
</html>


Result-
javascript break statement.

Click on the button to loop in javascript with a break. And the loop has to output from 0 to 4, but the break statement pulls you out of the loop. When this variable is equal to "3" .




Syntax
break;

optional label reference:
break labelname;


Break statement in for loop
In JavaScript we need to use the break statement inside the Javascript for loop in JavaScript to exit the loop repetition. You understand this with the help of this example.
Example-



<!DOCTYPE html>
<html>
<head>
  
</head>

<body>
    <h1> JavaScript Break Statement </h1>
    <button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
function myFunction() {
    var text = "";
    var i;
    for (i = 10; i > 0; i--)
    {
        if(i == 6)
        {
            text ="<br \>Coming Out of <b> For Loop</b> Where i = " + i;
            break;
        }
      
        text ="<br \ ><b>Numbers </b>= " + i;
      
    }
     document.getElementById("demo").innerHTML = text; }
</script>
</body>
</html>




Break statement in the while loop
In javascript, we use the break statement in the loop while loop to exit the repetition.
Example-


<!DOCTYPE html>
<html>
<head>
    <title> JavaScript Break Statement in For Loop </title>
</head>

<body>
    <h1> JavaScript Break Statement </h1>
    <button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
function myFunction() {
var text = "";
    var i = 0;
    while (i <= 10)
    {
        text ="<br \ ><b>The Value of the Variable </b>= " + i;
        i++;
      
        if(i == 4)
        {
            break;
        }
    }
    text ="<br \>This statement is from Outside <b> While Loop</b> = " +i;
     document.getElementById("demo").innerHTML = text;
    }
  
</script>
</body>
</html>





More Examples-

Example
In this example, we use together while loop, with a break statement in javascript.


<!DOCTYPE html>
<html>
<body>
<h1>JavaScript </h1>
<p>Click on the button to loop in javascript with a break. And the loop has to output from 0 to 4, but the break statement pulls you out of the loop. When this variable is equal to "3" .</p>
<button onclick="myFunction()">Try it</button>
<p id="eo"></p>
<script>
function myFunction() {
    var text = "";
    var i = 0;
    while (i < 5) {
        text += "<br>The number is " + i;
        i++;
        if (i === 3) {
            break;
        }
    }
    document.getElementById("eo").innerHTML = text;
}
</script>
</body>
</html>


JavaScript Break and Continue statement






Continue JavaScript Statement


Javascript Continue statement is another one to control the flow of the loop. In this statement is used inside the for loop, while loop and do while loops. While executing these loops in javascript, if the compiler finds the Continue statement inside of them, then the loop in JavaScript stops the current recurrence. And starts from the beginning to the new recurrence.

Syntax
continue;

Using the optional label reference:
continue labelname;



Continue Statement Examples.
Examples-
In this example, we use the loop in javascript together with the release statement.
In this, we have JavaScript loop through the blocks of code, but leaving the value of "3"


<!DOCTYPE html>
<html>
<body>
<h1> JavaScript continue Statement
</h1>
<button onclick="myFunction()">Try it</button>
<p id="web"></p>
<script>
function myFunction() {
    var text = "";
    var i;
    for (i = 0; i < 5; i++) {
        if (i === 3) {
            continue;
        }
        text += "The number is " + i + "<br>";
    }
    document.getElementById("web").innerHTML = text;
}
</script>
</body>
</html>







Example
In this example, we use the loop in javascript together with the release statement.
In this, we loop the javascript through the block of code, but leave the value of "3".


<!DOCTYPE html>
<html>
<body>
<h1>continue statement</h1>
<p>Click the button</p>
<button onclick="myFunction()">Try it</button>
<p id="web"></p>
<script>
function myFunction() {
    var text = "";
    var i = 0;
    while (i < 5) {
        i++;
        if (i === 3) {
            continue;
        }
    text += "<br>The number is " + i;
    }
    document.getElementById("web").innerHTML = text;
}

</script>
</body>
</html>





Example
In this example, we use the loop in javascript together with the release statement.
In this, we have JavaScript loop through the block of code, but leave 2 and 3 numbers (using the OR operator) in it.


<!DOCTYPE html>
<html>
<body>
<h1>Continue statement</h1>
<p>Click the button</p>
<button onclick="myFunction()">Try it</button>
<p id="web"></p>
<script>
function myFunction() {
    var text = "";
    var i;
    for (i = 1; i < 8; i++) {
        if (i === 2 || i === 3) continue;
        document.getElementById("web").innerHTML += i + "<br>";
    }
}
</script>
</body>
</html>






Examples
In this example, we use the loop in javascript together with the release statement, and in this loop through an array.


<!DOCTYPE html>
<html>
<body>
<h1>continue statement</h1>
<p>Click the button</p>
<button onclick="myFunction()">Try it</button>
<p id="web"></p>
<script>
function myFunction() {
    var cars = ["BMW", "Volvo", "Saab", "Ford"];
    var text = ""
    var i;
    for (i = 0; i < cars.length; i++) {
        if (cars[i] === "Saab") {
            continue;
        }
        text += cars[i] + "<br>";
    }
    document.getElementById("web").innerHTML = text;
}
</script>
</body>
</html>




JavaScript Break and Continue statement:- Javascript break statement- In Javascript, the brake statement is used to exit any type of loop. Loop found in JavaScript, such as Javascript for loop, or javascript while Loop and Javascript do loop. Continue JavaScript Statement- Javascript Continue statement is another one to control the flow of the loop. In this statement is used inside the for loop, while loop and do while loops.

Post a Comment

[blogger]

MKRdezign

Contact Form

Name

Email *

Message *

Powered by Blogger.
Javascript DisablePlease Enable Javascript To See All Widget