KotlinCS 124 LogoJava
PrevIndexNext
Kotlin
Java
  • Implementing a Map : 04/26/2024

  • map-reduce-filter : 04/25/2024

  • Generics : 04/24/2024

  • Hashing : 04/23/2024

  • Binary Search : 04/22/2024

  • MP3: Course Ratings : 04/19/2024

  • Quicksort : 04/18/2024

  • Merge Sort : 04/17/2024

  • Sorting Algorithms : 04/16/2024

  • MP Debugging Part 1 : 04/15/2024

  • MP2: Course Activity : 04/12/2024

  • Practice with Recursion : 04/11/2024

  • MP Debugging Part 0 : 04/10/2024

  • MP2: API Client : 04/09/2024

  • MP2: API Server : 04/08/2024

  • Trees and Recursion : 04/05/2024

  • Trees : 04/04/2024

  • Recursion : 04/03/2024

  • MP1: Filtering and Search : 04/02/2024

  • MP1: Loading and Sorting : 04/01/2024

  • Lists Review and Performance : 03/29/2024

  • Linked Lists : 03/28/2024

  • Algorithms and Lists : 03/27/2024

  • Continuing MP0 : 03/26/2024

  • Getting Started with MP0 : 03/25/2024

  • Lambda Expressions : 03/22/2024

  • Anonymous Classes : 03/21/2024

  • Practice with Interfaces : 03/20/2024

  • Implementing Interfaces : 03/19/2024

  • Using Interfaces : 03/18/2024

  • Working with Exceptions : 03/08/2024

  • Throwing Exceptions : 03/07/2024

  • Catching Exceptions : 03/06/2024

  • References and Polymorphism : 03/05/2024

  • References : 03/04/2024

  • Data Modeling 2 : 03/01/2024

  • Equality and Object Copying : 02/29/2024

  • Polymorphism : 02/28/2024

  • Inheritance : 02/27/2024

  • Data Modeling 1 : 02/26/2024

  • Companion Objects : 02/23/2024

  • Encapsulation : 02/22/2024

  • Constructors : 02/21/2024

  • Objects, Continued : 02/20/2024

  • Introduction to Objects : 02/19/2024

  • Compilation and Immutability : 02/16/2024

  • Practice with Collections : 02/15/2024

  • Maps and Sets : 02/14/2024

  • Lists and Type Parameters : 02/13/2024

  • Imports and Libraries : 02/12/2024

  • Multidimensional Arrays : 02/09/2024

  • Practice with Strings : 02/08/2024

  • null : 02/07/2024

  • Algorithms and Strings : 02/06/2024

  • Strings : 02/05/2024

  • Functions and Algorithms : 02/02/2024

  • Practice with Functions : 02/01/2024

  • More About Functions : 01/31/2024

  • Errors and Debugging : 01/30/2024

  • Functions : 01/29/2024

  • Practice with Loops and Algorithms : 01/26/2024

  • Algorithms : 01/25/2024

  • Loops : 01/24/2024

  • Arrays : 01/23/2024

  • Compound Conditionals : 01/22/2024

  • Conditional Expressions and Statements : 01/19/2024

  • Operations on Variables : 01/18/2024

  • Variables and Types : 01/17/2024

  • Welcome to CS 124 : 01/16/2024

Compound Conditionals

var speed = 65
if (speed <= 65) {
println("Wow. A law-abiding driver!")
} else if (65 < speed && speed < 75) {
println("You're speeding, but probably won't get a ticket")
} else if (75 <= speed && speed < 85) {
println("Get ready for a ticket...")
} else {
println("You may be walking home!")
}

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

var speed = 65
var safeDriver = speed < 70
println(safeDriver)

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:

var speed = 65
var riskyDriver = speed < 80 && speed > 70
println(riskyDriver)

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

var speed = 65
var riskyDriver = speed < 80 && speed < 70
println(riskyDriver)

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:

Let’s look at how they work by example:

println(true && true)
println(true && false)
println(false && true)
println(false && false)

Evaluating Compound Conditional Expressions
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:

var value = 18
var goodValue = value > 10 && value <= 20 && value != 14 && value != 15
println(goodValue)

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:

var firstHomework = 10
var secondHomework = 2
var thirdHomework = 10

(The full set of rules is here. But we’ll always use parentheses 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
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.

var example = 1028
if (example > 1024) {
println("Big value!")
} else {
println("Small value")
}

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:

Practice: Print Larger, Sometimes

Created By: Geoffrey Challen
/ Version: 2020.8.0

Computer programs can also make more complex decisions based on multiple pieces of data.

To illustrate this, let's complete the short snippet of code below. You should assume that three variables are already declared and have their values set: x and y, both Int values, and print, a Boolean. You do not need to declare or modify their values.

You should write a snippet of code containing a conditional expression to accomplish the following goal. If the value of x is strictly greater than y, you should print "Larger". Otherwise you should print "Smaller". However, if print is set to false you should not print anything, regardless of the values of x and y.

Blocks and Variable Scope
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:

var outside = 10
if (outside > 8) {
println(outside)
var inside = true
println(inside)
}
println(inside)

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.

var outside = 10
if (outside > 8) {
println(outside)
var inside = true
println(inside)
}
println(inside)

Course Design: Autograding
Course Design: Autograding

CS 124 is designed to give every student a chance to succeed. Below we explain one unusual aspect of the course. You can find more information on the syllabus.

CS 124 submissions are autograded.

With a tiny number of infrequent exceptions, all work submitted in CS 124 is automatically evaluated and feedback returned immediately. Submitted code is examined to determine whether it performs correctly, meets our style guidelines, and for other aspects of code quality. The tools and systems used to do this were developed specifically for CS 124, and provide higher quality and more comprehensive feedback than other similar courses. All code submitted in CS 124 can be submitted an unlimited number of times without penalty—until you reach a deadline (homework or project) or run out of time (on a quiz).

We have embraced autograding for several reasons. It’s not just because the course is large. Autograding provides you with immediate and actionable feedback on your work. Correcting small mistakes isn’t something you only do when learning to program. Correcting small mistakes is most of what programming actually is. We want you to get good at it. The more mistakes you make, the more you’re learning.

Autograding also frees staff time to provide direct student support. Grading code by hand is difficult, boring, time-consuming, and slow, and most humans are terrible at spotting mistakes that are trivial to uncover through automated testing. Put another way: Humans are bad at evaluating code, and computers are good at it. We want the humans in CS 124 doing what humans are good at—working with you one-on-one to support your success in the course. And our autograding tools are pushing the limits of what’s possible through automated analysis.

Homework: Print First-Year Or Not

Created By: Geoffrey Challen
/ Version: 2022.8.0

At the University of Illinois, we'll say that you're currently a first-year student if you entered in the year 2022. Given an already-declared Int variable named year containing the year a student entered, print "First-Year" if the year is 2022, and "Upper-Level" otherwise. However, if an already-declared Boolean variable advancedStanding is true, then don't print anything at all. (Because who knows at that point!)

More Practice

Need more practice? Head over to the practice page.