Kotlinlearncs.online LogoJava

    ← Prev

    Index

    Next →

    Kotlin
    Java
    • Arrays : 5

    • Compound Conditionals : 4

    • Conditional Expressions and Statements : 3

    • Operations on Variables : 2

    • Variables and Types : 1

    • Hello, world! : 0

    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!");
    }

    Let’s 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:

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

    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: Two Magical Numbers

    Created By: Nikita Neledov
    / Version: 2023.7.0

    Consider a double value magical if it is strictly between 0 and 1. Given two double values first and second, write a snippet of code (not a method) to test whether first and second are both magical numbers. If they are, the program shall joyously exclaim (print) "True!" revealing their magical nature. Otherwise, it will print “False” encouraging you to seek the arcane secrets elsewhere.

    Note that both first and second are already declared and initialized for you, and you should not modify their values.

    Blocks and Variable Scope
    Blocks and Variable Scope

    Previously 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);

    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.

    Homework: Print Adult Or Not

    Created By: Geoffrey Challen
    / Version: 2021.8.0

    In the United States, you are legally an adult once you turn 18 years old. Given an already-declared int variable named age containing a person's age, print "Adult" if they are legally an adult and "Not Adult" if they are not. However, if an already-declared boolean variable whisper is true, then don't print anything at all.

    More Practice

    Need more practice? Head over to the practice page.