Gice

Technology and General Blog

Do you at any time feel trapped at some issue in lifetime exactly where you just want to get rid of a minute or an occasion? When you just want to skip some times and then go with the flow? That could possibly not be possible in real lifetime but it’s possible in programming languages like JavaScript by utilizing crack and continue statements.

These statements are identified as Loop Manage Statements We are likely to make clear the split as well as keep on statements in this report. We will make guaranteed that every little thing about equally of the ideas is sent thoroughly and in a specific way.

What is the Split Assertion?

Normally, we use break statements when we offer with the loop as nicely as swap statements. Let us commence as a result of the operating of the break statement in each statements.

Working of break assertion in a loop

How do you get out of a loop when you need to do it each time a specific situation happens? That is easy. The split assertion is used at an occasion whereby fulfilling the situation currently being specified, the whole loop gets skipped and it usually takes you out of the loop. In other terms, the loop is stopped.

How to use crack statement with the Though loop in JavaScript

In this example, we are heading to detect and evaluate how the crack statement performs along with the though loop. The features of the split statement is going to be apparent then:

var k =
while (k < 6) {
  console.log(“The number will be “ + k)
  k++
  if (k === 4)
    break
 

The condition is set to be less than 6 in the loop. The break statement will terminate the loop whenever the value of “k” gets equal to 4. That’s where we have applied the break statement. All the values up to 4 will be printed except 4 and above. Why? Because the loop will get terminated whenever the value of k will be equal to 4.

How to use break statement with the For loop in JavaScript

In this aspect, we are gonna observe the functionality of the break statement in the for loop:

for (i=1 i<=8 i++)
  if (i === 5) break
  console.log(i)

In the above scenario, as you can clearly see that the break statement is applied on the condition where the value is equal to 5. That means what? It clearly means that whenever the value is equal to 5, the loop will be stopped and all the values up to 5 except 5 will be printed.

Working of break statement in switch

While dealing with the switch statements, we use break statements to exit the switch blocks. Whenever a break statement is encountered in a switch block, you jump out of the switch statement. In that case, the code after the break statement will definitely not work and it will also get skipped inside the switch block. The switch block, as a whole, will be ignored whatever code is thereafter the break statement.

Now we will take a look at a couple of examples to better understand how break statement works:

How to use break statement with JavaScript switch statement

The example we are going to encounter will indicate how the break statement works along with the switch statement:

switch (new Date().getDay())
  case 0:
    console.log(“Sunday”)
    break
  case 1:
    console.log(“Monday”)
    break
  case 2:
    console.log(“Tuesday”)
    break
  case 3:
    console.log(“Wednesday”)
    break
  case 4:
    console.log(“Thursday”)
    break
  case 5:
    console.log(“Friday”)
    break
  case 6:
    console.log(“Saturday”)

In this case, whenever a single case is satisfied, it will break out of the switch block and will make sure that no further condition is checked or the code is run.

What is the Continue Statement?

Let’s consider a situation where we are in a loop and we desire to break one iteration whenever a specified condition occurs and then we continue with the next iteration in the loop.

The Continue statement is going to fulfill that desire for us. Unlike break, the continue statement “jumps over” to the next iteration/execution of the loop.

Whenever a continue statement takes place, the loop condition is checked to see if the condition is satisfied or true and if so, it goes towards the next iteration.

Working of continue statement in loops

For different types of loops, the behavior of the continue statement is different:

Usage of continue statement in JavaScript While loop

We are going to deal with the while loop along with the continue statement. You will observe how the statement works.

var i = 0
while (i < 6)
  if (i === 2)
    continue
    i++
 
console.log(“The digit is “ + i)
i++

In the above example, we have used a while loop together with the continue statement.

The continue statement is applied just after the “if” statement in order to make sure that the iteration gets skipped whenever “i” gets equal to 2.

Loop through the block of code where the value of “2” gets skipped.

The output will be:

How to use continue statement with the For loop in JavaScript

In this example, we will discuss the use of the continue statement in the JavaScript for loop. Let’s proceed.

for (i = 1 i < 8 i++) i === 4)
  continue
  console.log(i)

In this example, we have used a for loop. In the “if” condition, we have specified that whenever the value is equal to 3 and 4, the iteration will be moved towards the next phase. That means that we are going to skip the iterations with the help of the continue statement whenever the value is equal to 3 and 4.

The output will be:

Conclusion

In this article, we have discussed everything related to the break and continue statements in JavaScript. We have learned to use the break statement to get out of a loop. We also learned how we can skip a single iteration in a loop whenever we desire to do so via the continue statement. From the description of the examples, we have achieved a precise level of understanding.

Leave a Reply

Your email address will not be published. Required fields are marked *