Test your JavaScript Skills : break & continue Keywords - 31

Part 31

Table of contents

Introduction

Namaste, In this blog I will discuss 7 code scenarios on break & continue keywords.

  1. Identify the output of the below program

     let i=10;
     while (true) {
         i++;
         if (i>15)
          break;
     }
    
     console.log(i);
    

    a.error

    b.15

    c.16

    d.no output

    e.undefined

    Answer:c

    Reason:The loop breaks when i is 16.

  2. Identify the output of the below program

     outer: for (let i=0;i<10;i++) {      
         for (let j=2;j<10;j++) {
             if (j==5) 
                 break outer;
         }    
     }
     console.log(i);
    

    a.11

    b.10

    c.error

    d.undefined

    e.no output

    Answer:c

    Reason:The let variable has block scope

  3. Identify the output of the below program

     let i=2;
     while(i<10) { 
       i++;
       if (i> 6)
            i++;
       else
           break;
     }
     console.log(i);
    

    a.6

    b.3

    c.11

    d.no output

    e.undefined

    Answer:b

    Reason:By computation the value of i is 3

  4. Identify the output of the below program

     let i=2;
     while(i<10) {
         (i < 6) ? i++ : continue; 
         i++;
     }
    
     console.log(i);
    

    a.11

    b.10

    c.error

    d.no output

    e.undefined

    Answer:c

    Reason:The continue keyword cannot be used with ternary operator

  5. Identify the output of the below program

    
     flagpost: {  
         var i=10;
         break flagpost;
         console.log(i+1);  
      }
      console.log(i);
    

    a.10

    b.undefined

    c.11

    d.no output

    e.error

    Answer:a

    Reason: The var variable has global scope

  6. Identify the output of the below program

    
     flagpost: {  
         break flagpost;
         console.log("1");  
      }
      console.log("2");
    

    a.2

    b.1

    c.error

    d.no output

    e.undefined

    Answer: 2

    Reason: By computation the result is 2

  7. Identify the output of the below program

     outer: for (var i=0;i<10;i++) {      
         for (let j=2;j<10;j++) {
             if (j==5) 
                 break outer;
         }    
     }
     console.log(i);
    

    a.1

    b.0

    c.error

    d.11

    e.5

    Answer:b

    Reason:The var variable has global scope

Did you find this article valuable?

Support Programming with Mahavir by becoming a sponsor. Any amount is appreciated!