Making Decisions¶
Learning Objectives:
Write programs that make decisions.
Introduce conditional execution with
if
statements.Give examples of logical expressions and relational operators.
Introduce complex conditionals (with
and
,or
, andnot
).Understand programs with multiple
if
statements.Introduce
if
andelse
.Understand common difficulties students have with conditionals.
We have discussed two major abilities that computers have. Computers can name values. Computers can repeat steps. The third major ability that a computer has is the ability to make decisions. We make decisions all the time. Before we leave the house we may check to see if it will rain that day, and if it will, we bring an umbrella.
Computers can also make decisions or take action when something is true. More specifically, (1) a computer can test data and (2) a computer can execute instructions if that test is true. The ability to test data and take actions on the result is what allows the computer to deal with input and take action on it (e.g., if the credit card is valid then charge the card), or deal with data from the world around (e.g., if I see a stop sign then stop.).
The computer’s ability to take action (execute some code) when something is true is also called conditional execution.