(Expressions & Operators can be found in the Class-7 notes)
for StatementA for loop repeats until a specified condition evaluates to false and looks like the following:
for ([initialExpression]; [conditionExpression]; [incrementExpression])
statement
When a for loop executes, the following occurs:
initialExpression, if any, is executed. This expression usually initializes one or more loop counters, but the syntax allows an expression of any degree of complexity. This expression can also declare variables.conditionExpression expression is evaluated. If the value of conditionExpression is true, the loop statements execute. Otherwise, the for loop terminates. (If the conditionExpression expression is omitted entirely, the condition is assumed to be true.)({ }) to group those statements.incrementExpression is executed.function howMany(selectObject) {
let numberSelected = 0;
for (let i = 0; i < selectObject.options.length; i++) {
if (selectObject.options[i].selected) {
numberSelected++;
}
}
return numberSelected;
}
const btn = document.getElementById('btn');
btn.addEventListener('click', () => {
const musicTypes = document.selectForm.musicTypes;
console.log(`You have selected ${howMany(musicTypes)} option(s).`);
});
while Statementwhile statement executes its statements as long as a specified condition evaluates to true. A while statement looks as follows:while (condition)
statement
false, statement within the loop stops executing and control passes to the statement following the loop.statement in the loop is executed. If the condition returns true, statement is executed and the condition is tested again. If the condition returns false, execution stops, and control is passed to the statement following while.statements, use a block statement ({ }) to group those statements.let n = 0;
let x = 0;
while (n < 3) {
n++;
x += n;
}
With each iteration, the loop increments n and adds that value to x. Therefore, x and n take on the following values:
n = 1 and x = 1n = 2 and x = 3n = 3 and x = 6After completing the third pass, the condition n < 3 is no longer true, so the loop terminates.
Avoid infinite loops. Make sure the condition in a loop eventually becomes false—otherwise, the loop will never terminate! The statements in the following while loop execute forever because the condition never becomes false:
// Infinite loops are bad!
while (true) {
console.log('Hello, world!');
}