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 a/an_____________loop.

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.__________statement skips the remaining code of the current iteration and transfers the control to the next iteration of the loop.

Ans:- continue

2. Write T for the True statement and F for the False one.
a. There is no difference between while and do…while loops.

Ans:-F
b. A break statement cannot be written inside a for loop.

Ans:-F
c. The while loop is executed once for the false condition.

Ans:-F
d. A while loop may not contain a continue statement.

Ans:-T
e. Three statements control a for loop.

Ans:-T

3. Multiple Choice Questions.

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:-i. 1
b. What will be the output of the following code?
I = 10, s = 1
while (I > = 5) {
I + S = S
I = I – 2

}
document.write (S)
i.24                ii.25               iii.20            iv.22

Ans:-ii. 25

Explanation:-

I = 10, s = 1
while (I > = 5) {
I + S = S
2 – I = I

}
document.write (S)

Case 1-

I = 10, s = 1
while (10> = 5) {
{ S = 1+10= 11
I = 2 – 10= 8

}

Case-2

I = 8, s = 11
while (8> = 5) {
{ S = 11+8= 19
I = 8 – 2= 6

}

Case-3

I = 6, s = 19
while (6> = 5) {
{ S = 19 + 6 = 25
I = 6 – 2= 4

}

Case-4

I = 4, s = 25
while (4> = 5){…..}

Not true, so the loop will not be executed and hence

document.write (S) will be executed where S = 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:-  iv. None of these

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: The three types of loops available in Javascript are:-
(i)while loop.
(ii)do..while loop
(ii)for loop

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

Ans: The three statements comprising a for loop are:-
(i)Initialization
(ii)Test expression
(iii)The increment/decrement statement

d. Name the two entry-controlled loops.

Ans: The two entry-controlled loops are:-
(1)for loop
(ii)while loop

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

Ans: (I) while loop – When the condition evaluates to false in while loop, the program comes out of the loop and executes the next statements.

(ii) do… while loop – When the condition evaluates to false in do..while loop, the program comes out of the loop and execute the next statements. but in any case, the loop body executes at least once.

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

Ans: (1) while loop.-

while (condition){statement (s)}

(2) do… while loop –

do{statement (s)} while (condition)

(3) for loop –

for (initialization; condition; increment/decrement){statement (s)}

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

Ans: The break statement is used to get out of the loop before the normal end of the loop, while the continue statement skips the remaining code of the current iteration and transfers the control to the next iteration of the loop.

Example of break statement:

``````
while (x< 10) {
if (x == 5){
break ; // breaks out of the loop completely
x = x + 1;
document.write ( "Counter = " + x + "<br>")
}``````

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

Example of continue statement:-

``````
while (x < 10)

{

X = x +1

if (x == 5){

continue // skip rest of the loop body

}
document.write ( "Counter = "+x+ ")

}
``````

Here, 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: No, a for loop will not execute without initialization.
Example:-
for (number = 1; number <= 10; number ++)

Here, the first expression number = 1 initializes the number variable. It is always executed once, as soon as the loop is entered. In this case, we initialize number to 1. If this expression number = 1 is not present in Javascript code then the Javascript code will not execute.

d. What is an infinite loop? Explain with an example.

Ans: The loop that never ends is known as an infinite loop.
Example:-
while (number <=10){

document.write (“number = ” + number + “<br>”)

number++

}
If we omit the line

number++

the loop would never end since the 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 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:
i.X-=1
ii.X+=2
iii. x+=3
iv. x-=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><h1>SEBAHELP.COM</h1></body>
<script>
for(var i =10;i>=1;i--)
{
document.write (i + "<br>");
}
</script>
</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><h1>SEBAHELP.COM</h1></body>
<script>
var i =1
do{
document.write (i + "<br>");
i = i +2
}while (i<=100)
</script>
</html>
``````

3.What will be the output of following code?
var i = 1; S=0;
while (i <= 10)
{
S =S + 1;
i =i + 2;
}
document.write (“Sum = ” + S);
Also, write how many times the loop will be executed.

Ans:-

``````<!DOCTYPE html >
<html>
<body><h1>SEBAHELP.COM</h1></body>
<script>
var i = 1; S=0;
while (i <= 10)
{
S =S + i;
i =i + 2;
document.write ("Sum = " + S);
}
</script>
</html>``````

Output:- Sum = 25

Loop will be executed 5 times.

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

Ans:-

``````<!DOCTYPE html >
<html>
<body><h1>SEBAHELP.COM</h1></body>
<script>
var i = 1; S=0;
do {
S =S + i;
i =i + 2;
}while (i <= 10)
document.write ("Sum = " + S);
</script>
</html>``````

If the initial value of i is 11 then Output :-Sum = 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:- Nothing will display in the browser

#### 1 COMMENT

1. I am very happpy to see these answers . They were really helpfull to me. I was struglling to ger the explanation of 3. b. question . But, your website solved my problem . Thank you very very much.