Compound Conditionals

Created By: Geoffrey Challen
/ Updated: 2022-08-17

Welcome back to CS 124!

Next we'll continue discussing conditionals. Last time we introduced simple conditional expressions and statements, and blocks. This lesson continues on those topics, introducing a few new wrinkles. We'll show how you can combine conditional expressions and statements, and discuss how blocks introduce scope to our variables. So let's get started!

Compound Conditional Expressions

Previously we discussed how you can use conditional operators (<, >, <=, >=, ==, and !=) to create conditional expressions. For example, to test whether the value stored in the variable speed is less than 70, we'd use the conditional expression speed < 70. Conditional expressions evaluate to boolean values:

So... great! But what if we want to determine if speed is both less than 80 and greater than 70? Here's how we would do this:

The example above introduces some new syntax, so let's look at it carefully.

Unpack example above, showing students how to identify unfamiliar bits of code.

The example above introduces compound conditional expressions. They are created from combining multiple individual conditional expressions using two new operators: and (&&) and or (||).

Both and (&&) and or (||) combine two conditional expressions, one on either side of the operator:

  • And (&&) evaluates to true if both the conditional expression on the right and the left are true.
  • Or (||) evaluates to true if either the conditional expression on the right or the left is true.

Let's look at how they work by example:

Walk through both and and or using all permutations of values.

Evaluating Compound Conditional Expressions

By combining simple conditional expressions using && and ||, we can express arbitrary decision-making logic. For example, if we want to determine if the value of a variable was both greater than 10, less than or equal to 20, and not equal to both 14 and 15:

We're typically going to keep our compound conditional expressions as simple as possible. But when evaluating a compound conditional expression, Kotlin follows a few rules:

  • &&s are evaluated before ||s
  • Otherwise compound conditional expressions are evaluated from left to right
  • Paratheses can and should be used to group expressions together for clarity
  • Evaluation stops as soon as the result is known—this is called short-circuit evaluation

Go through multiple examples of compound conditional expressions. Use at least one that uses grouping, and discuss evaluation order and short-circuit evaluation.

(The full set of rules is here. But we'll always use paratheses to group things to avoid confusion!)

Some of this may seem complicated or confusing! But don't worry—99% of the conditional expressions you'll find in real programs are extremely simple. If you find yourself writing something fairly complex in this course, please ask for help. There may be a simpler way!

Compound Conditional Statements

So we can combine multiple conditional expressions together using && and || to create more complex decision-making logic. We can also create compound conditional statements to put our conditional expressions to use. Here's an example. Let's go through it slowly and carefully.

Go through compound if statement slowly and carefully.

Our code is starting to look more complicated. But if you break it down line by line, it's still just made up of the same simple building blocks. Here's how Kotlin evaluates an if statement:

  • It starts at the top and works down
  • When it finds an if branch where the conditional expressions evaluates to true, it enters that branch and executes it
  • Otherwise it keeps going
  • If it reaches an else statement (just else, not if else), it always enters that block

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

Blocks and Variable Scope

Last time we introduced the idea of a block of code. We needed blocks so that we could set off the code that should or should not be executed by a conditional statement. And we'll be using them again later—both to indicate what part of a program should be repeated, and when we start to create reusable pieces of logic called functions.

But there is another important aspect of blocks that we need to discuss. Let's introduce it using an example:

Try to run the code above. What happens? An error occurs! Why? Because variables are not available outside of the block in which they were declared!

The part of a program in which a variable is available is known as its scope. For the variables that we've been using so far, their scope is limited to the block in which they were declared. Note that they can be accessed in blocks declared inside their block, just not outside of their block. That's hard to explain in words. So let's talk it through.

Discuss scoping rules and use examples. Present the visual indentation rule: variables can be used at the level they were declared or to the right, but not to the left.

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

More Practice

Need more practice? Head over to the practice page.