KotlinCS 124 LogoJava
PrevIndexNext
Kotlin
Java
  • Compound Conditionals : 08/28/2026

  • Conditional Expressions and Statements : 08/27/2026

  • Operations on Variables : 08/26/2026

  • Variables and Types : 08/25/2026

  • Welcome to CS 124 : 08/24/2026

Compound Conditionals

int speed = 65;
if (speed <= 65) {
System.out.println("Wow. A law-abiding driver!");
} else if (65 < speed && speed < 75) {
System.out.println("You're speeding, but probably won't get a ticket");
} else if (75 <= speed && speed < 85) {
System.out.println("Get ready for a ticket...");
} else {
System.out.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:

int speed = 65;
boolean safeDriver = speed < 70;
System.out.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:

int speed = 65;
boolean riskyDriver = speed < 80 && speed > 70;
System.out.println(riskyDriver);

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

int speed = 65;
boolean riskyDriver = speed < 80 && speed < 70;
System.out.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:

System.out.println(true && true);
System.out.println(true && false);
System.out.println(false && true);
System.out.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:

int value = 18;
boolean goodValue = value > 10 && value <= 20 && value != 14 && value != 15;
System.out.println(goodValue);

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

One more rule is worth separating from those, because it is not about where to put your parentheses:

Evaluation stops as soon as the result is known. This is called short-circuit evaluation: once Java can tell how the whole expression comes out, it doesn’t look at the rest. One false settles an &&, and one true settles an ||.

Later in the course we’ll take advantage of this to write expressions whose second half would fail if the first half hadn’t already ruled it out. That’s a useful consequence of the rule, but it isn’t the rule. Evaluation stops as soon as the answer is known—whether or not anything would have gone wrong.

int firstHomework = 10;
int secondHomework = 2;
int thirdHomework = 10;

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 CS 124, 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.

int example = 1028;;
if (example > 1024) {
System.out.println("Big value!");
} else {
System.out.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 Java evaluates an if statement:

Practice: Solve 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 should not 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:

int outside = 10;
if (outside > 8) {
System.out.println(outside);
boolean inside = true;
System.out.println(inside);
}
System.out.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.

int outside = 10;
if (outside > 8) {
System.out.println(outside);
boolean inside = true;
System.out.println(inside);
}
System.out.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 course design document.

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 course tutor 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.

Course Design: Classical Programming and AI Collaboration
Course Design: Classical Programming and AI Collaboration

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 course design document.

CS 124 teaches you to write code by hand, and to build software with AI coding agents.

That can feel like a contradiction. You spend a morning working out a loop by hand—like the ones you’ll write this week—and then meet a machine that would have written it instantly and better. Students have told us that this disconnect is jarring, and they are right to notice it. So here is our reasoning.

AI collaboration is too important not to teach. It is how software is built now, and it is the skill that will let you build things that matter to you. But CS 124 also sits at the start of a degree program, and the courses after it still expect you to read and write code yourself. For now, we teach both.

The more interesting question is whether the two are connected. We think they are—though not in the way people usually assume. The claim is not that you will spend your career reading and writing source code by hand. Working with a coding agent you will see quickly how much of that is unnecessary, and the better you get at it, the less of it you will do.

The real argument is that writing code by hand builds habits of mind that make you a better collaborator: making a specification precise, anticipating how something might fail, tracing how data moves through a program, and verifying that a program does what you believe it does. Writing code by hand exercises these abilities in miniature. Coding agents let you deploy them at a scale you could never reach by typing.

We should be honest that this is our current best answer rather than a settled one. We do not know how much classical programming you need in order to become an effective AI collaborator. We hope it is not much. You can read our full reasoning on the course design document.

Getting Help: Connect Your AI
Getting Help: Connect Your AI

CS 124 gives you five ways to get help when you’re stuck. Each day this week we introduce one of them. You can find them all on the getting help page.

Set this up once, and your own AI can read the course with you.

Connect Your AI lets the AI tools you already use read our course—every lesson, and every word of the walkthrough videos. It works with Claude Code, which you’ll use for your project later this semester, and with Claude.ai, ChatGPT, and Gemini as well.

Then you can ask where something is covered, have a concept explained the way we taught it, or get it to quiz you before quiz day. Because it answers from our material rather than from programming in general, what it tells you matches what you’re actually being asked to learn.

A few things are worth knowing:

This is for learning the material beforehand. Quizzes are still taken in the CBTF without AI, as always.

That’s all five. More on the getting help page.

Homework: Solve 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!) Don't modify year or advancedStanding.

More Practice

Need more practice? Head over to the practice page.