Making Decisions
To make proper decisions in programming, as in create different pathways to our instructions, it requires the following:
Creating Conditions
Evaluating Conditions
Events to occur depending on if a condition is true.
To simulate the following requirements, let's examine a movie theatre pricing
situation.
If we as a programmer were to create a price checking program
, we would have to ask the user for their age, and the program should be able to generate with an appropriate pricing.
Our Conditions
There are 3 conditions in this scenario:
Are they a senior?
Are they an adult?
Are they a child?
Evaluation of the Condition
To determine which scenario they fall into, we must compare their age against our threshold.
They are a senior if their age is 65 or over
They are an adult if they are not a senior and their age is 18 or over
They are a child if they are not a senior or an adult, and their age must be 17 or under
Events to Occur based on a Condition Being true
true
Possible Event #1 -> Their age is 65 or over; therefore, we output price of $12.00
Possible Event #2 -> Their age 18 to 64 inclusively; therefore, we output price of $15.50
Possible Event #3 -> Their age is 17 and under; therefore, we output price of $10.00
How do we do this in Programming?
Conditions are created with if statements
.
We compare values with comparison operators
We can combine multiple situations with logical operators
Both comparison
and logical operators
result to Boolean values
(true
or false
) which forces the if statements to trigger their event code only if their related conditions are true
.
Last updated