IF-ELSE Statement

tags: #python/documentation/python_conditionals

What is a Two-Way IF Statement?

The if-else statement is a two-way if statement and is sometimes referred to as binary selection since there are two possible paths of execution.

Basic Syntax

if condition:
    # Code to be executed if the condition is True
else:
    # Code to be executed if the condition is False
The Logic

The IF-ELSE statement evaluates test expression and will execute the body of IF only when the test condition is True. If the condition is False, the body of else is executed.

Since the condition must either be True or False, exactly one of the alternatives will be executed.

Example:

x = 10

if x % 2 == 0:
    print("The value of x is even.")
else:
    print("The value of x is odd.")
The value of x is even.

Here we are specifying that if the remainder when x is divided by 2 is 0, then we know that x is even, and the program will display the respective message. If the condition is false, then the second set of code is executed.

Powered by Forestry.md