If-Else

Noun · Development

Definitions

  1. The basic conditional statement: evaluate a condition, run one block of code if it is true and, optionally, another if it is false. Chains of else if, written elif in Python and elsif in Ruby and Perl, test several conditions in order and run the first that matches. The construct took its familiar structured form in ALGOL 60 and is present in nearly every programming language. In some languages it is a statement, while in others, such as Rust, Kotlin, and functional languages, if-else is an expression that produces a value, playing the role of the ternary operator condition ? a : b. Long chains are often better expressed with switch or pattern-matching constructs, lookup tables, or polymorphism, and deep nesting is commonly flattened with guard clauses that return early. A classic parsing problem, the dangling else, arises when it is unclear which if an else belongs to. At the hardware level, conditionals become branches, whose cost depends on how well the CPU's branch predictor guesses them.

    In plain English: The basic 'if this, do that; otherwise, do this other thing' decision-making structure that every programmer learns on day one.

    Example: "The function was a 200-line if-else chain. We refactored it into a strategy pattern and everyone could finally read it."

Related Terms