You have 3 free guides left 😟
Unlock your guides
You have 3 free guides left 😟
Unlock your guides

4.2 If-else statements

3 min readjune 24, 2024

Conditional statements are the backbone of decision-making in Python programs. They allow code to execute different blocks based on specific conditions, enabling dynamic responsive program flow.

, , and statements form the core of conditional logic. By combining these with comparison and , programmers can create complex decision trees, guiding program execution through various scenarios based on input and state.

Conditional Statements

Conditional statements for program flow

Top images from around the web for Conditional statements for program flow
Top images from around the web for Conditional statements for program flow
  • if
    statements enable conditional execution of code blocks based on whether a specified condition evaluates to
    [True](https://www.fiveableKeyTerm:TRUE)
    [False](https://www.fiveableKeyTerm:FALSE)
    • Code block under an
      if
      statement executes only if the condition is
      True
      (
      x > 5
      )
    • If the condition is
      False
      , the code block is skipped and program execution continues with the next statement after the
      if
      block
  • if-else
    statements provide an alternative code block to execute when the
    if
    condition is
    False
    • Code block under the
      if
      statement executes if the condition is
      True
      (
      temperature > 30
      )
    • Code block under the
      else
      statement executes if the
      if
      condition is
      False
      (
      temperature <= 30
      )
  • elif
    (else if) statements allow for checking multiple conditions if the preceding
    if
    or
    elif
    conditions are
    False
    • First condition that evaluates to
      True
      will have its corresponding code block executed (
      grade >= 90
      ,
      grade >= 80
      , etc.)
    • If none of the conditions are
      True
      , the code block under the
      else
      statement (if present) will be executed

Syntax of if and if-else statements

  • if
    statements start with the
    if
    keyword, followed by a condition and a colon
    • Condition is an expression that evaluates to either
      True
      or
      False
      (
      count == 0
      ,
      name != "John"
      )
    • Code block under the
      if
      statement is indented (typically by 4 spaces)
  • else
    statements follow an
    if
    statement and begin with the
    else
    keyword and a colon
    • Code block under the
      else
      statement is also indented
  • elif
    statements can be used between an
    if
    and
    else
    statement to check additional conditions
    • elif
      statements begin with the
      elif
      keyword, followed by a condition and a colon
    • Code block under the
      elif
      statement is indented
  • Proper is crucial for defining the scope of each code block within conditional statements
  • Example syntax:
if age >= 18:
    print("You are eligible to vote")
elif age >= 16:
    print("You can pre-register to vote")
else:
    print("You are [not](https://www.fiveableKeyTerm:not) eligible to vote yet")

Implementation of conditional code blocks

  • create conditions that evaluate to
    True
    or
    False
    • ==
      (equal to),
      !=
      (not equal to),
      <
      (less than),
      >
      (greater than),
      <=
      (less than or equal to),
      >=
      (greater than or equal to)
  • Logical operators combine multiple conditions
    • and
      (both conditions must be
      True
      ),
      or
      (at least one condition must be
      True
      ),
      not
      (inverts the boolean value)
  • Conditions can include variables, constants, and function calls that return boolean values
    • Variable:
      if score > 100:
    • Constant:
      if MAX_VALUE == 100:
    • Function call:
      if is_valid_email(email):
  • Example implementation:
temperature = 25
if temperature > 30:
    print("It's a hot day")
    print("Drink plenty of water")
elif temperature > 20 and temperature <= 30:
    print("It's a nice day")
else:
    print("It's a cold day")
    print("Wear warm clothes")

Advanced Conditional Concepts

  • in programs is determined by the sequence of conditional statements
  • occurs when the program execution takes different paths based on conditions
  • involve placing conditional statements within other conditional statements
    • Allows for more complex decision-making structures
    • Example:
    if outer_condition:
        if inner_condition:
            # Code block for both conditions true
        else:
            # Code block for outer true, inner false
    else:
        # Code block for outer false
    
© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.


© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.

© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.
Glossary
Glossary