Kotlin
Java

Conditional Expressions and Statements
Kotlin

Created By: Geoffrey Challen
/ Updated: 2021-08-19

Today we'll talk about how computers make decisions. We'll start simple today, 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.

Conditional Expressions

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:

Identify the conditional expression that is being used to set the value of the variable grade.

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:

Kotlin includes a variety of useful conditional operators that you can use on numeric variables and literals: <, >, <=, >=, ==, and !=.

Review the various conditional operators listed above and show them used in a snippet of code.

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.

Assignment v. Equality Testing

Yesterday we emphasized how = is an assignment operator in Kotlin 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.

Discuss the difference between assignment and equality.

Conditional Statements

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 (var temperature = 88.8) of a variable that Kotlin will infer to be a Double since we initialize it with a Double literal. 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:

Do a very close read of the code above, introducing the new syntax if and block with braces. Slow down here, since this is important, and a lot of it is new.

Blocks

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:

Walk through nested conditional above.

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 Statements

To 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:

Walk through the if-else example above.

Big Little Decisions

Show an example of really cool decision making using computers, probably involving AI. One idea: walk through how the Cognitive Services API works.

While the decision-making capabilities that we've introduced today 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...

Show how to complete the homework problem above. Feel free to cover multiple approaches!

Solution Walkthrough

Show how to complete the homework problem above. Feel free to cover multiple approaches!

Solution Walkthrough