JS control statements are categorized into –
(A) Decision Statements (B) Looping Statements (C) Jumping Statements
(A) Decision/Conditional Statements:
JavaScript supports the following decision statements:-
(I) If statement
-
- This statement executes a block of code if a specified condition is true.
- For example –
let num = 20;
if (num > 0) {
document.write(“The number is positive”);
}
(II) If-else statement
-
- This statement executes one block of code if a specified condition is true, and another block of code if the condition is false.
- For example –
let num = -15;
if (num > 0) {
document.write(“The number is positive”);
} else {
document.write(“The number is negative”);
}
(III) Else-if statement
-
- For example –
let num = 0;
if (num > 0) {
document.write(“The number is positive”);
} else if (num < 0) {
document.write(“The number is negative”);
} else {
document.write(“The number is zero”);
}
(IV) Switch case statement
-
- This statement is used to perform different actions based on different conditions.
- For example –
let day = 3;
switch (day) {
case 1:
document.write(“Monday”);
break;
case 2:
document.write(“Tuesday”);
break;
case 3:
document.write(“Wednesday”);
break;
default:
document.write(“Invalid choice”);
}
(B) Looping Statements:
JavaScript supports the following looping statements:-
(I) For loop statement
- The for loop consists of three optional expressions (starting point, ending point, increment/decrement interval value)separated by a semicolon, followed by a block of statements executed in the loop.
- For Loop statements are executed repeatedly again and again until the condition is false.
- The for loop is used when we know in advance how many times the script code should run.
- This loop is used to repeat a block of code for a specific number of times. It is commonly used when you know the exact number of times you want to loop.
- The Syntax is –
{
code statements
}
- For example –
console.log(“The value of i is ” + i);
}
(II) While loop statement
- This loop is used to repeat a block of code while a certain condition is true.
- The while loop statement is simpler than the for loop.
- It consists of a condition and block statement.
- Here, the condition is evaluated before each pass through the loop. If the condition is true then it executes the block statement of codes otherwise out from the block statement.
- Syntax :
{
statements of codes
}
- For example –
let i = 0;
while (i < 5) {
console.log(“The value of i is ” + i);
i++;
}
(III) Do While loop statement
- This loop is similar to the while loop, but the condition is checked after the block of code is executed, so the block of code is guaranteed to execute at least once.
- The do…while loop is much like a while loop.
- It will repeat the loop until the specified condition is false.
- This loop always executes at least once, even if the condition is false because the block of statements is executed before the condition is tested.
- In this loop statement, curly braces are optional.
- Syntax :
{
statements of codes
}while (ending condition);
- For example –
let i = 0;
do {
console.log(“The value of i is ” + i);
i++;
} while (i < 5);
(IV) For-in loop statement
- This loop is used to iterate over the properties of an object.
- The for-in loop is specially used for an object’s properties.
- In each iteration of this loop, one property from the object is assigned to a defined variable name and this loop continues till all the properties of the object are exhausted.
- Syntax :
}
- For example:-
for (const val in person) {
document.write(person[val]);
}
person
object and log the key-value pairs to the console.(V) For Each statement
- This loop is used to iterate over the elements of an array.
- For example –
var fruits = [“Apple”, “Banana”, “Orange”, “Mango”];
fruits.forEach(function(val) {
document.write(” I like ” + val);
});
(VI) For…of statement
- This loop is used to iterate over the values of an iterable object (such as an array).
- For example –
<script>
const arr = [10, 20, 30, 40, 50];
for (let value of arr) {
document.write(” “+value);
}
</script>
Output:
10 20 30 40 50
(VII) With statement
- The statement is also used for an object mainly to refer to an object’s properties or methods.
- The object specified as an argument to with becomes the default object for the duration of the block that follows.
- The properties and methods for the object can be used without naming the object.
- Syntax :
code for properties without the object name and dot
}
(C) Jumping Statements:
- These statements are mainly used to control the flow of looping statements.
- JavaScript provides full control to handle looping statements and switch statements. These statements are used to immediately come out forcibly of any loop or to start the next iteration of any loop respectively.
- The looping control statements are – (a) Break statement (b) Continue statement (c) Label statement
(a) Break statement :
-
- The break statement is used to exit a loop early.
- The keyword ‘break’ is used to perform this operation.
- For example – As mentioned in the Switch case statement.
(b) Continue statement
-
- The
continue
statement is used within loops in JavaScript to skip over the current iteration and move on to the next iteration. - The continue statement is used to immediately start the next iteration of the loop and skip the remaining code block when the condition is met.
- When a continue statement is encountered during the operation, the program flow moves to the loop check expression immediately and if the condition remains true, then it starts the next iteration, otherwise the control comes out of the loop.
- The keyword ‘continue’ is used to perform this operation.
- For example –
- The
for (let i = 0; i < 5; i++) {
if (i === 3) {
continue;
}
document.write(i);
}
(c) Label statement
-
- A label is simply an identifier followed by a colon (:) that is applied to a statement or a block of code.
- A label can be used with a break and continue to control the flow more precisely.
- The
label
statement in JavaScript is used to provide a label for a statement, which can then be referenced using abreak
orcontinue
statement. - The
label
the statement can also be used with acontinue
statement in a similar way to skip over certain iterations of a loop. - It is also suggested that the use of labeled statements should be used sparingly and with caution, as it can make code harder to read and understand.
- For example –
outerloop:
for (let i = 0; i < 3; i++) {
innerloop:
for (let j = 0; j < 3; j++) {
if (i === 1 && j === 1) {
break outerloop;
}
document.write(`i = ${i}, j = ${j}`);
}
}
Output:
i = 0, j = 0i = 0, j = 1i = 0, j = 2i = 1, j = 0
0 Comments