Page 388 - Webapplication11_C11_Flipbook
P. 388
9. Differentiate between for and while loops.
Ans. for loop while loop
At the top of the loop, you’ll find initialization, At the top of the loop, just initialization and
condition checking, and an iteration statement. condition checks are performed.
The ‘for’ loop is only used when the number of When the number of iterations is based on a
iterations is already known. condition, the ‘while’ loop is utilised.
10. Explain a real-world situation where the break statement helps. [CBSE Handbook]
Ans. Suppose we are performing a search on a database. Once we find our record (that we were searching for), then, we can
use break statement to terminate the searching process (loop).
B. Long answer type questions.
1. List the various ways in which JavaScript code can be added to a program.
Ans. There are four different places in the HTML document where you can use the script tag:
• Page body: When the page is loaded in the browser, the output appears as part of the HTML content.
• Page header: The code is written in the form of a function (a collection of JavaScript statements that are handled as a
single unit) and referenced to in another script on the same page.
• Within the HTML tag: When JavaScript is used as an event handler (explained later in the chapter), it interacts with
HTML elements.
• As an external file: In this scenario, the JavaScript code is stored in a.js file. In the script tag, this file is included.
2. Explain if, if…else and if..else if …else control structures.
Ans. The if statement is used to examine a given condition and perform actions based on whether or not that condition is true.
It’s most commonly utilised in scenarios where we need to perform many operations for various situations.
The if-else statement is an expansion of the if statement that allows us to conduct two different operations, one for the
condition’s correctness and the other for its incorrectness. It is important to note that the if and else blocks cannot be
executed concurrently.
If one of the conditions in an if-else-if ladder statement is true, the statements in the if block will be executed; if another
condition is true, the statements in the else-if block will be executed; and finally, if none of the conditions are true, the
statements in the else block will be executed. There are a variety of else-if blocks that can be used.
3. Briefly explain operator precedence and operator associativity.
Ans. • Operator Precedence: This refers to how the operator interacts with its operand. It determines which operation takes
precedence over the others. The hierarchy of precedence operators is established, with the operator with the highest
precedence standing at the top. For example, because the multiplication operator has a higher precedence than the
addition operator, multiplication operation is performed before addition.
• Operator Associativity: The order in which an operator evaluates its operand, i.e. from left to right or right to left.
When two operators have equal precedence in an expression, the association is generally left to right.
4. Differentiate between alert( ) and prompt( ) methods.
Ans. alert( ) prompt( )
An alert box is used if we want information to be When we want the user to enter a value before
given (displayed) to the user. proceeding to the next page, we utilise a prompt box.
It always returns true as we must always click “OK” When a prompt box appears on a webpage, we must
to proceed. click “OK” or “Cancel” to advance after providing an
input value.
When an alert box appears, we must click “OK” to Prompt returns a null value if we click “Cancel.”
proceed.
386 Touchpad Web Applications-XI

