if...else Summary The if statement will execute statements when its condition evaluates to true. Optionally, it will execute the statements in its else clause if its condition evaluates to false. Syntax if (condition) { statementA_1; statementA_2; } [else { statementB_1; statementB_2; }] Parameters condition The condition to evaluate and check if true. The condition can be any valid expression. statementA_N The statement(s) to execute if the condition evaluates to true. statementB_N The statement(s) to execute if the condition evaluates to false. Description The if statement will evaluate its condition expression, and, if the condition evaluates to true, a statement associated with the if statement will be executed. The statement can be any valid statement including a single statement by itself or a block (to group statements). If an else clause is specified, and the condition evaluates to false, the statement associated with the else clause will be executed. The statement can be any valid statement including a single statement by itself or a block (to group statements). Furthermore, the statement associated with the else clause can be another if statement, achieving an "else if" effect. Basic if Statements The most basic if statement simply evaluates if a condition is true and executes associated statements: 123456import System; int x = 5;if (x == 5) { Console.log("x is equal to 5");} Be careful with mixing the equality operator (==) with the assignment operator (=) inside the condition for if statements as they can be the source of hard-to-find bugs. The compiler will raise a warning for this usage. Unimplemented if statements can also be written without a block. The block is used for grouping multiple statements together. If we only have one statement, we can also write: 1234import System; int x = 5;if (x == 5) Console.log("x is equal to 5"); Be careful without the block! If we use multiple statements, any statement beyond the first statement will always execute regardless of whether the if statement evaluates to true. For instance: 1234import System; int x = 5;if (x == 5) Console.log("x is equal to 5"); Console.log("This statement will always execute"); For code readability, it is suggested to always use the block. if-else Statements We can also add an else clause to our if statements to execute some code if the condition evaluates to false. The else clause must come immediately after the statement associated with the if clause. A simple if-else statement might look like: 123456789import System; int x = 5;if (x == 4) { Console.log("This will never run.");}else { Console.log("This will run. 'x' is equal to 4.");} "else-if" "else-if" can be accomplished with a combination of the else clause and a new if statement. Technically, there is no "else if" statement in JS++ in that there is no special "else if" (or the one-word "elseif") keyword in the language. Instead, an else-if statement is just an else clause where the associated statement is a new if statement. Here's how to achieve "else if": 123456789101112131415import System; int x = 3;if (x == 1) { Console.log("x equals 1.");}else if (x == 2) { Console.log("x equals 2.");}else if (x == 3) { Console.log("x equals 3.");}else { Console.log("'x' is not equal to 1, 2, or 3.");} Note that when we are comparing an expression to multiple possible values, it may be preferable to use a switch statement instead. Nesting if-else Statements We can also "nest" if-else statements inside another: 1234567891011121314151617181920import System; int x = 1, y = 2, z = 3;if (x == 1) { Console.log("x equals 1."); if (y == 2) { Console.log("y equals 2."); if (z == 3) { Console.log("z equals 3."); } else { Console.log("z does not equal 3."); } } else { Console.log("y does not equal 2."); }} Nesting if statements can be useful to evaluate whether multiple conditions are true or false. However, it's better to use the logical && (AND) and || (OR) operators when possible. Combining Conditions While we can combine conditions using nested if statements, this can become cumbersome quite quickly. Instead, we can use the && (AND) operator to test if two conditions both evaluate to true: 123456import System; int x = 1, y = 2;if (x == 1 && y == 2) { Console.log("x equals 1, and y equals 2.");} If we only want to test if one or another condition evaluates to true, we can use the || (OR) operator. The || (OR) operator will check if at least one condition (on either side of the operator) evaluates to true. 123456import System; int x = 1, y = 2;if (x == 1 || y == 1) { Console.log("x equals 1, or y equals 1.");} Negating Conditions (if NOT true) Using the logical ! (NOT) operator, we can check if a condition is not true without an else clause: 123456import System; bool flag = false;if (!flag) { Console.log("flag is not on.");} See Also Block switch Conditional Operator Logical Operators Comparison Operators Share HTML | BBCode | Direct Link