ReviseAlgo Logo

Control Flow

If Statements

Conditional execution with if, elif, else

Interview: Basic control flow

Last Updated: June 12, 2026 6 min read

Conditional statements control which code blocks execute based on boolean conditions. Python uses if, elif, and else with indentation-based blocks — no parentheses or curly braces needed.

Syntax & Patterns

  • if condition: — executes when condition is truthy
  • elif: — else-if, checked sequentially after the first if
  • else: — fallback when no conditions are met
  • Ternary expression: value_if_true if condition else value_if_false
  • Chained comparisons: if 0 < x < 10:

Best Practices

  • Prefer early returns (guard clauses) over deeply nested if-else blocks
  • Use truthiness: if items: instead of if len(items) > 0:
  • Use is None for None checks, not == None
  • Consider match/case (Python 3.10+) for complex branching

Use Cases

Input validation with guard clauses

Feature flags and configuration-based branching

Replacing switch-case with dictionary dispatch

Conditional expressions for inline logic

Common Mistakes

Deeply nested if-else instead of guard clauses/early returns

Using == None instead of is None

Forgetting that empty collections are falsy

Not using elif when conditions are mutually exclusive