Conditionals: Making Decisions
Programs that choose
If statements
A program often needs to make decisions. A conditional says, in effect, if this condition is true, then do this; otherwise do something else.
Simple decision flow
-
1
Step 1: Check a condition.
-
2
Step 2: If it is true, run one set of instructions.
-
3
Step 3: If it is false, run a different set of instructions.
Conditions are questions
A condition is usually a yes/no question such as is the temperature above 20? or is the password correct?
What does a conditional help a program do?
Conditionals let programs choose between actions.
Correct answer: Make decisions
What is the result of a condition being true?
True means the condition is satisfied.
Correct answer: The program follows the branch associated with true
True branch vs false branch
True branch
- Runs when the condition is satisfied
- Represents one possible path
False branch
- Runs when the condition is not satisfied
- Represents the alternative path
Which of these is a yes/no condition?
A condition should have a true or false answer.
Correct answer: Is the light on?
Give an example of a real-life decision a program could make.
Any valid if-then style example works.
Correct answer: If the battery is low, show a warning