Skip to main content
Engineering LibreTexts

5.5: Alternative execution

  • Page ID
    40882
  • A second form of the if statement is “alternative execution”, in which there are two possibilities and the condition determines which one runs. The syntax looks like this:

    if x % 2 == 0:
        print('x is even')
    else:
        print('x is odd')
    

    If the remainder when x is divided by 2 is 0, then we know that x is even, and the program displays an appropriate message. If the condition is false, the second set of statements runs. Since the condition must be true or false, exactly one of the alternatives will run. The alternatives are called branches, because they are branches in the flow of execution.

    • Was this article helpful?