Textbook Exercise-G6 - M5
Textbook Exercise-G6 - M5
4. The break keyword can stop the loop before it has looped
through all the items.
5. The continue keyword can skip the remaining part of the
code and pass control to the beginning of the loop.
1. We can use optional else block with while and for loops in
JavaScript. - True
2. continue statement passes control to the beginning of the loop. -
True
3. A computer can make decisions by itself. - False
4. The code inside the if block is executed when the test expression
evaluates to true. - True
5. ‘for loop’ can’t be used for iterating through the elements of a
sequence.- False
Match the Following
1. Write down the syntax of Javascript if... else if ladder with an
example.
if (condition1) {
// Code block executed if condition1 is true
} else if (condition2) {
// Code block executed if condition2 is true
} else {
// Code block executed if none of the conditions are true
}
Example:
let num = 10;
if (num > 10) {
console.log("Number is greater than 10");
} else if (num < 10) {
console.log("Number is less than 10");
} else {
console.log("Number is equal to 10");
}
2. What is the difference between ‘nested if’ and if... else if ladder?
The difference between nested if and if...else if ladder is that
nested if statements are multiple if statements within another if
statement, creating a branching structure, whereas if...else if ladder
is a series of if statements followed by optional else if blocks,
where each condition is checked sequentially until one is true,
providing a linear decision-making process.
3. Write a program to input a number, and find out whether it is
greater than or less than 100.
let num = parseInt(prompt("Enter a number:"));
if (num > 100) {
console.log("Number is greater than 100");
} else if (num < 100) {
console.log("Number is less than 100");
} else {
console.log("Number is equal to 100");
}
4. What is the use of Loop in programming. What are the loops
available in JavaScript?
Loops in programming are used to repeat a block of code multiple
times. In JavaScript, common types of loops include the for loop,
while loop, and do...while loop. They allow efficient execution of
repetitive tasks, iterating over arrays or sequences, and
implementing conditional logic.