Selection is one of three main constructs of programming (along with Sequence and Iteration). An if statement is a conditional statement that performs a certain action based on conditional values. Essentially, if thing A is true, then thing B will happen.
If the user answers yes to this question then an appropriate statement is printed. Remember that all commands in Python are lowercase (if / input / print etc.). The double equals stands for ‘is equal to‘. The colon stands for THEN and the line after an if statement must be indented (press tab key once).
But what if the window is not open? At the moment nothing will happen if you type no:
The elif command stands for ‘else if’. Essentially: If thing A is true then do thing thing B, else if thing C is true then do thing D:
But what about any other answer than yes or no? The else command will submit a response if the value is anything else:
The if and elif commands have a colon at the end, but else has it at the start. Also, else does not need to be on a new line.
Nested If Statements
Complex programs may require you to have if statements within if statements! When something is within another thing in Python, it is nested. You must ensure that the related if, elif and else statements line up with each other.
One possible output for this program is: