If else

Updated: 04/26/2017 by Computer Hope

An if else statement in programming is a conditional statement that runs a different set of statements depending on whether an expression is true or false. A typical if else statement would appear similar to the one below (this example is JavaScript, and would be very similar in other C-style languages).

var x = 1;
if (x === 1) {
window.alert("The expression is true!");
}
else {
window.alert("The expression is false!");
}

In this case, with the variable x being equal to the number 1, the statement in the first set of curly braces {} is executed. You would receive a pop-up "The expression is true!" alert message. If you were to change the first line of the above code to var x = 42;, you would receive a pop-up alert message with the text "The expression is false!"

Conditional expression, Else, Else if, If statement, Programming terms, Switch statement, Ternary operator