LOOPING IN jAVASCRIPT


    SEBA 
    CLASS- 10
    CHAPTER-7
     LOOPING IN JAVASCRIPT 



    OBJECTIVE TYPE QUESTIONS:

    1. Fill in the blanks:

    a. The while loop is repeated when the condition is true.

    b. The loop that never ends is called a/an infinite loop.

    c. While loop checks the stopping condition at the beginning of the loop.

    d. The break statement is used to get out of the loop before the entire loop gets executed.

    e. The continue statements skips the remaining code of the current iteration and transfer the control to the next iteration of the loop.


    Table of Content


    2. Write True or False

    a. There is no difference between while and do…while loops. False

    b. A break statement cannot be written inside a for loop. False

    c. The while loop is executed once for the false condition. False

    d. A while loop may not contain a continue statement. True

    e. Three statements control a for loop. True

     

    3. Multiple choice questions:

    a. How many times will the given loop will executed?

    For (var I =20 ; I >= 5; I -=6)

    document.write(“I=” +I)

    Answer: 1

    b. What will be the output of the following code?

    I =10, S =1

    While (I > = 5) {

    S = S + I

    I = I -2

    }

    document.write(S)

    Answer: 25

    c. Which one of the following is a looping statement.

    Answer: for

    d. Which one of the following is an infinite loop?

    Answer: for (;;)

    e. Which of the following loop evaluate the stopping condition at the end of loop?

    Answer: None of these


    Table of Content

    DESCRIPTIVE TYPE QUESTIONS:
    1. Short answer questions

    a. What is loop?

    Ans: Loop is a sequence of instruction that are executed repeatedly.

    B. What are the three types of loops available in JavaScript?

    Ans: The three types of loops available in JavaScript are:

    i. for loop

    ii. while loop

    iii. do… while loop

    c. What are the three statements comprising a for loop?

    Ans: The three statements comprising a for loop are:

    i. initialization

    ii. condition

    iii. increment/decrement

    d. Name the two entry control loops?

    Ans: The two entry control loop are:

    i. for loop

    ii. while loop

    e. What happens when the condition is false in:

    i. while loop   ii. do…while loop

    Ans: while loop – when the condition evaluates to false in while loop, the statements goes outside the body of the loop and execute the statement below.

    do…while loop – when condition evaluates to false in do…while loop, the statements come out of the loop and displays the final value of number on the screen.


    Table of Content

    2. Long answer questions:

    a. Differentiate between while, do…while, and for loops in their syntax.

    Ans: i) While loop:

    while (condition)

    {

    statements(s)

    }

    OR

    Initialization;

    while(condition expression)

    {

    Statements(s);

    Increment/decrement;

    }

     

    Ans: ii. do…while loop:

    do

    {

    Statement(s)

    }

    while (condition)

    OR

    Initialization;

    do

    {

    Statements(s);

    Increment/decrement;

    }

    While(condition expression)

     

    Ans: iii. for loop:

    for (initialization; condition; increment/decrement)

    {

    Statement(s)

    }

     

    b. How is break statements different from continue statements? Explain with example.

    Ans: The different between break statements and continue statements is that:

    Break statements is used to get out of the loop before the normal end of the loop. while the Continue statements skips the remaining code of the current iteration and transfers the control to the next iteration of the loop.

     

    Example: of break statements

    while( i < 10)

    {

    If (i==5)

    {

    break; // breaks out of the loop completely

    }

    i = i +1;

    document.write(“Counter=” + i + “<br/>”)

    }

    Here, the loop breaks when the value reaches 5, i.e., the program exits the loop. The break statements are used inside the while loop. The loop then jumps to the statements after the closing brace of the loop statement.

     

     

    Example: of Continue statements

    while( i < 10)

    {

    If (i==5)

    {

    continue; // skip rest of the loop body

    }

    document.write(“Counter=” + i + “<br/>”)

    }

    Here, the value of i reaches 5, the rest of the code is skipped and the programs skips to the next iteration.

     

     

    c. Will a for loop execute without initialization? Explain with suitable examples.

    Ans: As we know, to execute a single statement or sets of interactive statements repeatedly, we use looping statement which is comprises of three statements i.e. initialization, condition, and increment/decrement. So, for loop cannot execute without initialization.

    Example:

    for (i = 1; i <= 5; i++)

    Here, the first expression i = 1 initialize the number to a variable. It will always run before the first execution on the loop as soon as the loop is entered. In this case, we initialize i to 1. If this first expression i =1 is not present in the JavaScript code then the loop will not be executed.

     

    d. What is infinite loop? Explain with example.

    Ans: An infinite loop is a piece of code that keeps running forever as the terminating condition is never reached.

    Example:

    while(i <=10)

    {

    document.write(“ i = ” +i + “<br/>);

    i ++

    }

    Here, if we omit the line i ++, the loop would never end since number will never equal to 11.

     

    e. Can you increase/decrease the loop variable number by a value other than 1? If yes, write the statement for the following:

    i. decrease the variable number by 1  ii. increase the variable number by 2

    iii. increase the variable number by 3 iv. decrease the variable number by 4

    Ans:

    i. a-=1

    ii. a+=2

    iii. a+=3

    iv. a-=4


    Table of Content


    APLICATION BASED QUESTIONS:

    1. Write the following code using for loop:

    Var i = 10;

    While ( i >= 1)

    {

    document.write(i);

    i = i  - 1;

    }

    Ans:

    for (var i =10 ; i <= 1; i = i  - 1)

    {

    Document.write(i);

    }

     

    2. Write the following code using do…while loop:

    for(var i =1 ; i <= 100; i =i  + 2)

    document,write(i + “<br>”);

    Ans:

    var i  = 1;

    do

    {

    document.write(i + “<br>”);

    i + i  +2;

    }

    while (i <= 100)

     

    3. What will be the output for the following code?

    var i  = 1; S = 0;

    while ( i <= 10)

    {

    S = S + i;

    i = i + 2;

    }

    document.write(“Sum = “ + S);

    Also, write how many times the loop will be executed.

    Ans: The output will be 25. The loop will run 5 times.

    When,

    S will be 0 then i will be 1

    S will be 1 then i will be 3

    S will be 4 then i will be 7

    S will be 11 then i will be 9

    S will be 25 then i will be 11

     

    4. write the code mentioned in Q3 using do…while loop. What happens if the initial value of i is 11?

    Ans:

    var i = 11; S = 0;

    do

    {

    document.write(“Sum =” +S);

    }

    While (i <= 10)

    The loop will run one time and will show the output as 11. Because the loop will terminate once since the condition ( i <= 10) will be false for the initial value 11.

     

    5. Execute the following code to obtain the output.

    var i = 1;

    while ( i <= 5)

    {

    for ( j = 3; J <=i ; j++)

    document.write( i + “<br>” + j);

    }

    Ans: The output will not come since it’s an infinite loop.

    Explanation:

    Its initial value is 1 and it will satisfy first condition that is while ( i <= 5) then it will check the condition that is ( j <= 1) which is false since i is 1 and j is 3 ( 3 is not less than or equal to 1), the second part is j++ which will result to j+1 of (3 + 1 =4) and again it will check its value with i that is (j<=i) which will again result to false. Thus the loop will continue incrementing and checking the value (j <=i) which will result t false all time. So, it is an infinite loop.


    Table of Content
    PLIZ SHARE YOUR EXPRESSION