Introduction to Computer Science/3.4 The if/elif/else Statement

3.4 The if/elif/else Statement

MDX Parsing Error

[next-mdx-remote] error compiling MDX: Unexpected character `"` (U+0022) in attribute name, expected an attribute name character such as letters, digits, `$`, or `_`; `=` to initialize a value; whitespace before attributes; or the end of the tag More information: https://mdxjs.com/docs/troubleshooting-mdx

Raw Content:

<!-- === LEARNING GOALS HEADER === -->

<Callout title="🎯 Learning Goals for This Page" type="info">
- Use the `elif` keyword to check for multiple, sequential conditions.
- Write a complete `if/elif/else` "chain" to handle multiple outcomes.
- Understand that only the *first* true condition in a chain will be executed.
</Callout>

<!-- === MAIN CONTENT AREA === -->

## Handling More Than Two Options

An `if/else` statement is perfect for "either/or" decisions. But what about situations with three or more possible outcomes? For example, a number isn't just "positive or not positive"; it can be positive, negative, or zero. For this, we need another tool: the `elif` keyword.

### The `if/elif/else` Chain

`elif` is short for "else if." It lets you add as many additional checks as you need between your initial `if` and your final `else`.

<Callout title="The Syntax:" type="info">
if some_condition_is_true:
    # This block runs if the FIRST condition is True.
    do_action_A()
elif another_condition_is_true:
    # This block runs if the FIRST condition was False,
    # but this SECOND condition is True.
    do_action_B()
else:
    # This block runs if ALL of the above conditions were False.
    do_action_C()
</Callout>

<Callout title="💡 The "First One Wins" Rule" type="info">
In an `if/elif/else` chain, Python checks the conditions from top to bottom. As soon as it finds the **first** condition that is `True`, it runs that block of code and then skips the rest of the entire chain. Even if other `elif` conditions further down are also true, they will be ignored.
</Callout>

### Live Example: The Number Checker

The program below asks the user for a number and then determines if it is positive, negative, or zero. This is a classic use case for an `if/elif/else` chain.

**Your challenge:** Run the program three times to test all possible paths. Enter a positive number (like **10**), a negative number (like **-5**), and finally, enter **0**.

<PythonSandbox codeBase64="IyBHZW5lcmF0ZWQgYnkgQWlTdHVkaW8NCiMgVmVyaWZpZWQgYnkgSm9obiBIb2xtZXMNCiMgUmV2aXNlZCBPY3QgMjAsIDIwMjUNCg0KIyAtLS0gVGhlIE51bWJlciBDaGVja2VyIFByb2dy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IFJ1biBpdCBhIGZpbmFsIHRpbWUgYW5kIGVudGVyIDAuIikNCnByaW50KCIgICBOb3RpY2UgaG93IGEgZGlmZmVyZW50IGJsb2NrIHJ1bnMgZWFjaCB0aW1lISIpDQpwcmludCgiLSIgKiA0MCk=" />

<!-- === EMBEDDED TRINKET === -->
<!-- === KEY TAKEAWAY BOX === -->

<Callout title="Your Complete Selection Toolkit" type="info">
With `if`, `elif`, and `else`, you now have the complete toolkit for "selection" or "decision-making" in programming. You can now build programs that handle any number of different situations.
</Callout>

Interactive Sandbox

Use this space to practice writing code and complete the "Your Turn" activities above.

Python 3Initializing...
Loading code editor...
Program output will appear here...