This example will output the following to the web browser console log: 1 - Inside while loop on Ģ - Inside while loop on TechOnTheNet. Once the counter is 3, the while loop will terminate even though the while loop's condition is set to (counter <= 5). In this while loop example, the break statement is used to exit the while loop early when the counter is equal to 3. continue in JavaScript stops the execution of the current loop, stops the execution of the remaining statement(s) inside the current loop for current. Try it Syntax break break label label Optional Identifier associated with the label of the statement. Let's look at an example of how to terminate a while loop early using the break statement.Ĭonsole.log(counter + ' - Inside while loop on ') Ĭonsole.log(counter + ' - Done while loop on ') break - JavaScript MDN break The break statement terminates the current loop, switch, or label statement and transfers program control to the statement following the terminated statement. You can also use the break statement to terminate a loop early in JavaScript. The LCPS School Board voted on Tuesday to adopt a balanced calendar that would provide a six-week summer break with one and two-week breaks scheduled throughout the school year to address burnout. This example will output the following to the web browser console log: TechOnTheNet JavaScript How to use the Break Statement with the While Loop In this switch statement example, the break statement is used to terminate the switch statement so that once a match is found no further values are not evaluated. This is the most common use of the break statement.įor example: // Set the TechOnTheNet technology to JavaScriptĬonsole.log('Other TechOnTheNet technologies') You can use the break statement with the switch statement. How to use the Break Statement with the Switch Statement When used in a nested loop, the break statement terminates the. Let's look at an example that shows how to use a break statement in JavaScript. Use the break statement to terminate a loop including for, while, and do.while prematurely.
0 Comments
Leave a Reply. |