When designing programs, there are often points where a decision must be made. This decision is known as selection, and is implemented in programming using IF statements.
For selectionA decision within a computer program when the program decides to move on based on the results of an event., PythonA high-level programming language. uses the statementThe smallest element of a programming language which expresses an action to be carried out.if and else (note the lowercase syntax that Python uses):
Consider the age-related algorithmA sequence of logical instructions for carrying out a task. In computing, algorithms are needed to design computer programs. using Python. The steps are:
Ask how old you are
if you are 70 or older, say “You are aged to perfection!”
else say “You are a spring chicken!”
This algorithm would be written in Python (3.x) as:
age = int(input("How old are you?"))
if age >= 70: print("You are aged to perfection!")
else: print("You are a spring chicken!")
The program examines the value of age. If the inputted age is 70 or over, the programSequences of instructions for a computer. prints one message. Otherwise (else) it prints another.