You are here: >

If else

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, since the variable x is equal to the number 1, the statement within the first set of curly braces {} is executed. You would receive a pop-up alert message with the text "The expression is true!" 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!"

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