Loop Control Statement in ES6

Loop Control Statement in ECMAScript 6

The loop control statements can be defined as a set of conditions that control the execution of the loop. It executes the loop until the condition becomes false. The loop control statements modify the loop execution from its usual manner. We can control the flow of the loop execution completely. We can start an execution, skip the running execution and start the next execution, and terminate execution of the code block.

The loop control statements contain the following statements used in ECMAScript 6.

  •  Break statement
  •  Continue statement
  • Return statement

Now, we are going to discuss each statement with syntax and example.

The Break Statement

The break statement is used to stop the execution of the code block and take control outside of the loop. We can use the break statement with the switch statement or inside the loop.

Syntax

break;  

Example

var s = 1;
 while (s <= 5)
 {
       console.log (“ ” +s);
 if (s = = 4) 
 { 
     break;
 }
 s ++;
 }  

In the above code if the value of s = 4, then the loop will stop and take the control outside of the loop.

Output

After the execution of the code block, we will get the following result /output.

Loop Control Statement in ECMAScript 6

The Continue Statement

The continue statement is used to stop the running execution of the loop and take the control back to the starting of the loop. 

Syntax

continue; 

Example

var r = 0;
 while (r <= 8)
 {
      r ++;
     if (r = = 5)
 {
      continue;
 }
      console.log (“r = ”+r); 
 }  

Output

In the above example, we will print the values of r. We have used the continue statement here. So, if the value of variable r will be 5, then the execution will be stopped, and the next iteration of the loop is started.

After the successful execution of the code, we will get the following output.

Loop Control Statement in ECMAScript 6

The return statement

The return statement is used if we want to complete the execution of the function and return the output to the user. The return statement is used to return a value to the function caller.

Syntax

return expression; 

Example

function getRectArea (height, width)
 {
       if (height > 0 && width > 0)
 {
       return height * width; 
 }
        return 0;
 } 
 console.log (getRectArea (4, 5)); 

Output

The function getRectArea () used in the above example contains height and width. After the execution of the code, we will get the following output.

Loop Control Statement in ECMAScript 6

Labels for the flow control

In the computer programing language, the label can be defined as an identifier pursued by the colon (:). We can apply the identifier label on the code block or the statements. The label helps us to control the flow of the execution. We can use it with the continue and the break statement.

We can not apply the line break statement (\n) among the control statement (break and continue) and its label name. We cannot define any statement between the loop and label name.

Syntax of label

labelname;
 code statement 

The syntax contains:

labelname- In ECMAScript, it will be any identifier, but reserved word is not allowed.

code statement- It is a code statement to be executed in ECMAScript.

If we are working in strict mode. then we will not use ‘let’ asa label name, it will be the cause of an error because of its reserving category. Here, we use labels with the following two statements in ECMAScript.  

                        Label                     Description
Label with continue statement It helps us to jump from one to another iteration without or with a label reference.
Label with the break statement It helps us to exit the switch statement/ loop without label reference. If we use a label reference, then we can jump out of the code statement.

The Label with continue statement

The label with continue statement provides us a facility to jump from one to another iteration, and it may be done with the help of label reference or without the label reference.

Syntax

continue labelname;

Example

var p, q;
 label 1:                                      // The first statement named as label 1
 for (p = 0; p < 3; p ++)
 {
 label 2:                                     // The second statement named as label 2
 for (q = 0; q < 3; q ++)
 { 
      if (p = = = 1 && q = = = 1)
      {
       continue label 1;
 }
        console.log (‘p =’ + p +’, q =’ + q); 
       }
 } 

Output

After the execution of the example, we will get following output

Loop Control Statement in ECMAScript 6

In the above output, we can see that the label with continue statement skips p = 1, q = 1 and p = 1, q = 2.

The label with break statement

The label with break statement gives us the facility to exit from the switch statement or loop without using a label reference. When we use label reference then we also skip the block code statement.

Syntax

break labelname; 

Example

var r, s;
 label 1:                                      // The first statement named as label 1
 for (r = 0; r < 3; r ++)
 {
 label 2:                                     // The second statement named as label 2
 for (s = 0; s < 3; s ++)
 {
      if (r = = = 1 && s = = = 1) 
      {
       continue label 1;
 }
        console.log (‘r =’ + r +’, s =’ + s);
       }
 } 

Output

Loop Control Statement in ECMAScript 6

Declaration of Labeled functions

ECMAScript 6 allows us to associate the label statement with a function declaration. Before the ECMAScript 6, it was not possible to perform. In ECMAScript 5, the “strict mode” does not allowed the function declaration in the statement block. In ECMAScript 6, the developer can use function declaration as labeledItem through the LabeledStatement, but sometimes an error occur in it.    

There are some rules that are defined for web browser flexibility.

Function Declaration of LabeledItem

In the strict mode, if the condition of source code is matched with the condition of rule, then it will be a cause of an error.

In ECMAScript 6, we can declare a labeled function in a non-strict mode like this;

L: function welcome () {}    

If we use the above statement in strict mode, then it will be a cause of an error.

“use strict”;
 L: function welcome () {}          

We cannot label a Generator function in strict mode or non- strict mode.

L: function* welcome ()                        // error: cannot label generator function  
 {
 }