 HSLC Computer Science Solution: Chapter 7 (Looping in JavaScript) - UJUDEBUG

Contents:

• Objective Type Questions:

1. Fill in the blanks:

a. The while loop is repeated when the condition is ……………………..
ans: true

b. The loop that never ends is called ……………………..
ans: infinite

c. …………………….. loop checks the stopping condition at the beginning of the loop.
ans: While

d. The …………………….. statement is used to get out of the loop before the entire loop gets executed.
ans: Break

e. The …………………….. statement skips the remaining code of the current iteration and transfers the control to the next iteration of the loop.
ans: Continue

2. True and False

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

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

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

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

e. Three statements control a for loop.
ans: True

3. Choose the correct option.

a. How many times will the given loop be executed?
for (var I = 20 ; I >= 5; I -= 6)
document.write (“I =” + I)
i. 1
ii. 2
iii. 3
iv. 4

ans: ii. 2

b. What will be the output of the following code?
I = 10, S = 1
while (I > = 5) 1 {
S = S + I
I = I – 2 1
}
document.write (S)
i. 24
ii.25
iii. 20
iv. 22

ans: ii.25

c. Which one of the following is a looping statement?
i. if
ii. break
iii. continue
iv.for

ans: iv.for

d. Which one of the following is an infinite loop?
i. for ( ; ; )
ii. for (i = 3; i > 5; i++)
iii. for (i = 3; i < 5; i++)
iv. for (i = 15; i > 10; i–)

ans: i. for ( ; ; )

e. Which of the following loop evaluates the stopping condition at the end of loop?
i. for
ii. while
iii. do…while
iv. None of these

ans: iii. do…while

• Descriptive Type Questions

a. What is a loop?
ans: Loop is a sequence of instructions that are executed repeatedly.

b. What are the three types of loops available in JavaScript?
ans: Three types of loops available in JavaScript are: while,do….while and for.

c. What are the three statements comprising a for loop?
ans: Three statements comprising a for loop are – initialization, condition and increment/decrement.

d. Name the two entry-controlled loops.
ans: The for and while loops are the two entry-controlled loops.

e. What happens when the condition is false in:
i. while loop ii. do…while loop

ans:
i. When the condition evaluated is false in a while loop, the program comes out of the loop and displays the value of number on the screen.

ii. When the condition evaluated is false in a do…..while loop, the program comes out of the loop and displays the final value of number on the screen.

a. Differentiate between while, do…while, and for loops in terms of their syntax.
ans:

 while loop do….while loop for loop 1. The syntax is –while(condition){statement(s)} The syntax is –do {statement(s)} while(condition) The syntax is – for (initialization; condition; increment/decrement) {statement(s)} 2. It is an entry-controlled loop. It is an exit-controlled loop. It is an entry-controlled loop. 3. If the program is false, the program comes out of the loop and displays the value of number on screen If the program is false, the program comes out of the loop and displays the value of number on screen If the program is false, the program will never enter in a loop. 4. There is no semi-colon(;) after the condition. A semi-colon(;) is required after the condition A semi-colon(;) is required after the condition

b. How is the break statement different from continue statement? Explain with an example.
ans: Break Statement: Sometimes we need to get out of the loop before the stopping condition becomes true. In this case, break statement is used to terminate the execution of the loop. For example:

<!DOCTYPE html>
<html>
<body>
<script language=”javascript” type=”text/javascript”>
var x=0
document.write(“Entering the loop <br>”)
while(x<10){
if (x==5)
{break}
x=x+1;
document.write(“Counter ” + x + “<br>”)
}
document.write(“Exiting the loop”)
</script>
</body>
</html>

Output: The loop breaks when the value of i reaches 5; i.e the program exits the loop.

Continue Statement: The continue statement skips the remaining code of the current iteration and forces the loop to re-evaluate the looping-condition for the next iteration of the loop. For example:

<!DOCTYPE html>
<html>
<body>
<script language=”javascript” type=”text/javascript”>
var x=0
document.write(“Entering the loop <br>”)
while(x<10){
x=x+1;
if (x==5)
{continue}
document.write(“Counter ” + x + “<br>”)
}
document.write(“Exiting the loop”)
</script>
</body>
</html>

Output: When the value of x reaches 5, the rest of the code is skipped, i.e , the statement to display the value of x as 5 is skipped and the program skips to the next iteration.

c. Will a for loop execute without initialization? Explain with suitable examples.
ans: Yes, a for loop will execute without initialization, but we have to initialize the variable before the for loop. Initialization is must. It should be either in the script or in the for loop.

d. What is an infinite loop? Explain with an example.
ans: If we omit the line “number++” from the condition “number<=10” then the loop would never end since the number will never be equal to 11. This is called an infinite loop. We will not be able to get out of an infinite loop without exiting the program.

For example:

var number=1;
while(number<=10){
document.write (“number” + “=” + “number” + “<br>”)
}

The output of the above program will be an infinite loop as the number evaluated will never satisfy the condition.

e. Can you increase/decrease the loop variable number by a value other than 1? If yes, write the statements 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: Yes, we can increase/decrease the loop variable number other than 1.

(i) If we decrease the variable by 1, then the statement is number-=1.
(i) If we increase the variable by 2, then the statement is number-=2.
(i) If we increase the variable by 3, then the statement is number-=3.
(i) If we decrease the variable by 4, then the statement is number-=4.

• Application-based Questions

1. Write the following code using for loop:

var i = 10;
while ( i >= 1 )
{
document.write (i);
i = i – 1;
}

ans:

<!DOCTYPE html>
<html>
<body>
<script type=”text/javascript”>
for (var i =10; i>=1;i–)
{
document.write(i + “<br>”);
}
</script>
</body>
</html>

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

for (var i = 1; i <= 100; i = i + 2)
document.write (i + “<br>”);

ans:

<!DOCTYPE html>
<html>
<body>
<script type=”text/javascript”>
var i=1
do{
document.write(i+”<br>”);
i=i+2

} while (i<=100)
</script>
</body>
</html>

3. What will be the output of 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 of the above code will be: Sum=25. The loop will be executed 5 times.

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

<!DOCTYPE html>
<html>
<body>
<script type=”text/javascript”>
var i = 1; S=0;
do{
S=S+i;
i=i+2;
}while(i<=10)
document.write (“Sum =” +S);
</script>
</body>
</html>

If the initial value of i is 11, the output will be: Sum =11

Interested? Have any questions?

#### +91 8486 201473 #### 1 Comment

1. HSLC Computer Science Solutions - UJUDEBUG says:

[…] Chapter 7: Looping in JavaScript […]

## Call Us Now:

Sales : +91-8011624355
Support : +91-8486201473
Support : +91-7002890335

## Lowest Price Promise

[one_third] [/one_third] [two_third] We’re all looking for the best prices, even saving a couple of rupees can make a huge difference. That’s why we’ll match the price of your products if you find them cheaper somewhere else.

## Our price match promise

We'll price match any item we sell against any other seller. Here’s what you need to do to you get the best price:
• Give us a call on +918486201473
• Let our friendly team know the product
• Tell us where you spotted it
• Get it on cheapest price
That’s it! The whole thing should only take a few minutes and you’ll save some money too. Winner winner! [/two_third]

## Lowest Price Promise

We’re all looking for the best prices, even saving a couple of rupees can make a huge difference. That’s why we’ll match the price of your products if you find them cheaper somewhere else.

## Our price match promise

We'll price match any item we sell against any other seller. Here’s what you need to do to you get the best price:

• Give us a call on +918486201473
• Let our friendly team know the product
• Tell us where you spotted it
• Get it on cheapest price

That’s it! The whole thing should only take a few minutes and you’ll save some money too. Winner winner!