Conditionals
Table of contents
Description
Conditionals allow our programs to make choices based on the result of analyzing certain information. In more technical terms, conditionals execute alternate sets of code (branches) based on the evaluation of a boolean expression.
- Python Docs: if Statments, The if statement, Truth Value Testing
Composition
if
: A conditional must have only oneif
statement. It must have a boolean expression.elif
: A conditonal may have 0 to infinityelif
statements. It must have a boolean expression.else
: A conditional may have 0 or 1else
statements. It must never have a boolean expression.
Examples
if/else Statements
elif Statements
Compound if Statement
Python Control Structures
Conditionals are one of three program control structures. These control structures dictate the order in which a program’s code executes:
- Sequential: Code is executed linearly, one after the other.
- Selection: Different block(s) of code are executed based on the results of a condition.
- Iteration: Repeating a block of code several times.