The if-else branch operator. Short form of if. Cascade if-else-if form. Nested statements
Contents
- 1. Types of branching operators
- 2. Short form: if. Example
- 3. Full form: if-else. Example
- 4. Stepped (cascade) form: if-else-if. Example
- 5. Nested if statements. Example
- Related topics
Search other resources:
1. Types of branching operators
In JavaScript, branch operators allow you to perform a sequence of operations (actions) depending on the fulfillment (not fulfillment) of a certain condition. Syntactically, branching operators in JavaScript are no different from the same operators in other C-oriented languages such as C++, C#, Java.
Branching statements like if-else have 3 varieties or forms:
- short form if;
- full form of if-else;
- stepped (cascade) form if-else-if.
⇑
2. Short form: if. Example
The abbreviated form of the if statement does not use the else keyword and looks like this:
if (condition) statement
or
if (condition) {
statement1
statement2
...
statementN
}
here
- condition – some condition that returns true or false;
- expression – expression to be executed if condition = true;
- expression1, expression2, expressionN – expressions to be executed if condition = true.
If condition = false, then the statements statement, statement1, statement2, statementN are not executed and the program goes to the statement following the execution of the if statement. So the program does nothing.
If several statements need to be executed when the condition is met, then these statements are combined into a block of statements using curly braces { }.
Example.
// Statement if. Shortened form var a = 8, b = 5 // One condition if (a > b) console.log("a>b"); if (a == b) console.log("a==b"); if (a < b) console.log("a<b") // Several conditions var month = 5 if ((month == 1) || (month == 3) || (month == 5) || (month == 7) || (month == 8) || (month == 10) || (month = 12)) console.log("31 days") if ((month == 4) || (month == 6) || (month == 9) || (month == 11)) console.log("30 days") if (month == 2) console.log("28 (29) days")
Result
a > b 31 days
⇑
3. Full form: if-else. Example
The second form of the branch operator is as follows
if (condition) statement1 else statement2
here
- conditon – some condition, the result of which is equal to true or false;
- statement1 – statement executed if condition = true;
- statement2 – statement executed if condition = false.
If several statements must be executed when the condition is met or not met, then these statements are taken in a block of curly braces { }. In this case, one of the possible options for the operator could be the following
if (condition) { // Statements // ... } else { // Statements // ... }
Example.
'use strict'; // The if-else statement. Full form // Solving a quadratic equation // 1. Coefficients are given var a = 8, b = 5, c = -4 // 2. Calculate discriminant var D = b * b - 4 * a * c // 3. Determine the roots of the equation if (D < 0) console.log("Уравнение не имеет корней") else { var x1 = (-b - Math.sqrt(D)) / (2 * a) var x2 = (-b + Math.sqrt(D)) / (2 * a) console.log("x1 = " + x1) console.log("x2 = " + x2) }
Result
x1 = -1.0855823048033113 x2 = 0.4605823048033113
⇑
4. Stepped (cascade) form: if-else-if. Example
The stepwise form of the conditional jump operator allows you to enumerate all possible options, excluding their overlap. The step form is similar to the OR operation. Only one of the options is executed.
The general form of the if-else-if statement is as follows:
if (condition1) statement_1 else if (condition2) else statement_2 else ... if (conditionN) statement_N else statement_N+1
here
- condition1, condition2, conditionN – some conditional expression that returns true or false;
- statement1, statement2, statementN – one or more statements that are executed if one of the expressions condition1, condition2, conditionN is true.
If several statements need to be executed instead of statement1, statement2, statementN, then these statements must be enclosed in curly braces { }.
Example.
'use strict'; // The if-else-if statement. Cascade form // By the number of the season, display the name of this season // 1. The number of the season is set (1..4) var season = 2 // 2. Use the if-else-if statement if (season == 1) console.log("Winter") else if (season == 2) console.log("Spring") else if (season == 3) console.log("Summer") else if (season == 4) console.log("Autumn") else console.log("Incorrect number of season")
Result
Spring
⇑
5. Nested if statements. Example
Branch operators can be nested. This means that a single if, if-else, or if-else-if statement in its own statement block can contain other complex statements, including branching statements. The number of nesting levels is not limited and depends on the condition of the problem and the logical skills of the programmer.
Example.
'use strict'; // Nested branch statements // Determine the average between three numbers // 1. Given 3 numbers that are not equal var a = 3, b = 2, c = 1 var avg // 2. Use nested operators if if (a > b) { if (a > c) { // a - max if (b > c) avg = b else avg = c } else // c - max avg = a } else { // a < b if (b > c) { if (a > c) avg = a else avg = c } else avg = b } console.log("avg(" + a + ", " + b + ", " + c + ") = " + avg)
Result
avg(3, 2, 1) = 2
⇑
Related topics
⇑