Contents:
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
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
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
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:
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:
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:
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.
1. Write the following code using for loop:
ans:
2. Write the following code using do…while loop:
ans:
} while (i<=100)
</script>
</body>
</html>
3. What will be the output of the following code?
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:
If the initial value of i is 11, the output will be: Sum =11
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.
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:
That’s it! The whole thing should only take a few minutes.
7 Comments
Solution of chapter- 7 Application based question question number 5 provide solution and also provide In the Lab answers
Thank you so much for your interest in ujudebug… yes we are thinking of that as well. You can share the LAB Questions with us so that we can solve that for you. For further information, you can contact us at support@ujudebug.com
Thank u for giving all the notes with meaning
Thank you so much, it means a lot for us. As a support gesture you can spread the word with your friends.
A perfect site for computer answers with easy language
Thank you so much, it means a lot for us. As a support gesture you can spread the word with your friends. You can also share your view at google here – https://g.page/r/Ca8xfSF3B8QrEAg/review