Here you will learn about conditional branching such as JavaScript’s IF statement and switch statement.
It also provides detailed explanations of the basic syntax and usage of the IF statement, as well as syntax related to IF statements such as the ELSE IF statement and SWITCH statement.
How to make conditional branch with IF statement
By using the IF statement , you can branch processing based on specific conditions.
if (conditional expression) { processing 1; }
For the conditional expression, specify an expression that returns a true or false value. If the conditional expression is true, the processing inside the if block is executed . If false, it will not be executed.
Sample code using IF statement
In the example below, if the variable score is 60 or higher, “Pass!” is output.
let score = 75;
if (score >= 60) {
console.log("Passed!");
}
How to make conditional branch with else if statement
The ELSE IF statement is used to branch processing by specifying multiple conditions.
if (conditional expression 1) { processing 1; } else if (conditional expression 2) { processing 2; } else { processing 3; }
By writing as above, if conditional expression 1 is true, process 1 will be executed, otherwise if conditional expression 2 is true, process 2 will be executed. Finally, else is executed.
Sample code for else if statement
In the example below, “Excellent” is output when the score is 90 or higher, “Good” is output when the score is 80 or higher, and “Acceptable” is output otherwise.
let score = 75;
if (score >= 90) {
console.log("superior");
} else if (score >= 80) {
console.log("good");
} else {
console.log("acceptable");
}
How to make a conditional branch with a switch statement
The SWITCH statement is used to branch processing depending on a specific value.
switch (expression) {
case value1:
process1;
break;
case value2:
process2;
break;
default:
process3;
}
By writing the above, if the expression is equal to value 1, process 1 will be executed, and if the expression is equal to value 2, process 2 will be executed.
If the expression has a value other than value 1 or value 2, the processing in the default block is executed.
break is used to terminate a switch statement after performing the specified processing.
Sample code for switch statement
In the example below, “stop!” is output when the variable signal is “red”, “caution!” when it is “yellow”, and “go!” when it is “green”. Also, if a value other than “red”, “yellow”, or “green” is entered, a “wrong signal” will be output.
let signal = "green";
switch (signal) {
case "red":
console.log("stop!");
break;
case "yellow":
console.log("caution!");
break;
case "green":
console.log("go!");
break;
default:
console.log("wrong signal");
}
summary
The following is a summary of “conditional branching of IF/ELSE IF/SWITCH statements”.
- The IF statement allows you to branch processing based on specific conditions.
- The ELSE IF statement can specify multiple conditions and branch processing.
- The SWITCH statement can branch processing depending on a specific value.
By using the IF statement, ELSE IF statement, and SWITCH statement, you can control the program in more detail and create more complex programs.
I understood that by using the IF statement, ELSE IF statement, and SWITCH statement, you can have more detailed control over your program!
In particular, I found the SWITCH statement to be very useful when branching processing depending on a specific value.
Comments