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

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

Empty Blackboard

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

1. Short Answer 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.

2. Long answer questions

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?

support@ujudebug.com

For more info Call us

+918011624355

7 Comments

  1. White says:

    Solution of chapter- 7 Application based question question number 5 provide solution and also provide In the Lab answers

  2. Amrita joishi says:

    Thank u for giving all the notes with meaning

  3. Shubh says:

    A perfect site for computer answers with easy language

Leave a Reply

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

Call us now!

[one_third][/one_third][two_third]With a team of skilled software Developers, Ujudebug is the best IT Solution Company in Assam. Fully Customized Software Development services with multiple platforms. Our award winning team will we provide the best experience to customers at an affordable price.

Sales : +91-8011624355

Support : +91-8486201473

Our promise

We promise to provide exceptional service during your journey with us. Our experienced team delivers an unmatched level of client satisfaction from a broad range of software solutions, and IT consulting services. What are you waiting for:
  • - Give us a call on +918011624355
  • - Let our friendly team know your project
  • - Tell us how, when you want to start it
  • - Get it at the best possible price with all documentation
That’s it! The whole thing should only take a few minutes.[/two_third]

Call us now!

With a team of skilled software Developers, Ujudebug is the best IT Solution Company in Assam. Fully Customized Software Development services with multiple platforms. Our award winning team will we provide the best experience to customers at an affordable price.

Sales : +91-8011624355

Support : +91-8486201473

Our promise

We promise to provide exceptional service during your journey with us. Our experienced team delivers an unmatched level of client satisfaction from a broad range of software solutions, and IT consulting services. What are you waiting for:

  • - Give us a call on +918011624355
  • - Let our friendly team know your project
  • - Tell us how, when you want to start it
  • - Get it at the best possible price with all documentation

That’s it! The whole thing should only take a few minutes.