In this lesson we’ll talk about how computers make decisions. We’ll start simple, and get a bit more complicated tomorrow. But simple computer decision making is what gives birth to all of the more complex computer behaviors.
An important reminder: you are not alone. Our calendar is packed with opportunities to get help and meet the course staff and fellow students.
Let’s warm up with your first graded debugging challenge!
Computers can make decisions based on data.
They do this by evaluating whether a given expression is true
or false
.
We can do this with only literal values:
Try changing the literal values in the example above and see what happens.
You should be able to get goodGrade
to be either true
or false
, depending on the literal values that you choose.
But the example above is a bit silly. We don’t need a computer to tell us whether 95 is greater than 90! So usually our conditional expressions involve at least one variable:
In the examples above we’re using a conditional expression to set the value of a boolean
variable.
What makes the expresion a conditional expression is the use of the >
operator.
For example:
Java includes a variety of useful conditional operators that you can use on numeric variables and literals: <
, >
, <=
, >=
, ==
, and !=
.
Tomorrow we’ll look at how we can chain multiple conditional expressions together to make more complicated decisions. For now, we’ll keep things simple.
Last time we emphasized how =
is an assignment operator in Java and does not test equality.
The way that we do test for equality is using a slightly-different but similar-looking operator: ==
.
When you are getting started it can be very hard to see the difference between =
and ==
.
(You’ll get way, way better at this.)
But it’s worth emphasizing the difference now and starting to be on the lookout for it.
Conditional expressions form the basis for computer decision making because they are the basis for conditional statements. A conditional statement allows you to tell the computer that it should do one thing or another thing depending on the result of a conditional expression. Put more simply, they allow our programs to make decisions based on data.
Let’s dive right in and look at our first example. There are multiple new ideas emerging here all at once—so don’t worry, we’ll go over it slowly and carefully below.
Parts of this snippet should be familiar to us.
The first line is a standard variable declaration and initialization (double temperature = 88.8
).
The middle of the second line is a conditional expression (temperature > 80
).
It evaluates to true
if the value in the variable temperature
is greater than 80 and false
otherwise.
But the rest is new—and represents our first conditional statement.
Run the example above and see what happens.
Now, change the value of temperature
or the threshold (80
) and see what happens.
See if you get the program to not display any output.
Now let’s go through it carefully together:
if
statements introduce us to our first block of code.
Blocks begin with a {
and end with a }
.
In the case of an if
statement, the block contains the code that should be executed if the condition is true:
Blocks of code can contain all of the basic building blocks that we’ve already seen. We can declare variables inside blocks, manipulate them, and even include other conditional statements:
We use blocks to help organize our programs. In the case above, blocks identify the code that should be executed if the condition is true. Over the next few lessons we’ll see how blocks can be used to identify code that should be repeated, and name parts of the code that we want to reuse.
if-else
Statementsif-else
StatementsTo conclude, let’s look at one useful addition to the if
statement that we introduced above.
We’ve seen how it can be used to decide what to do:
In the example above, either the block is executed or it is not. But we can also extend this idea to make more complicated decisions. Here’s an example:
While the decision-making capabilities that we’ve introduced may seem simple, they are the basis for all more complex computer decision making. When you combine lots of tiny decisions, the end result can be astonishing…
Need more practice? Head over to the practice page.