Kotlinlearncs.online LogoJava

    ← Prev

    Index

    Next →

    Kotlin
    Java
    • Functions : 9

    • Practice with Loops and Algorithms : 8

    • Algorithms I : 7

    • Loops : 6

    • Arrays : 5

    • Compound Conditionals : 4

    • Conditional Expressions and Statements : 3

    • Operations on Variables : 2

    • Variables and Types : 1

    • Hello, world! : 0

    Practice with Loops and Algorithms

    In this lesson, we’ll pause to reinforce what we’ve learned about the basic building blocks of computer science: storing data, making decisions, and repeating operations. We’ll also get some more practice with loops!

    Debugging Practice
    Debugging Practice

    Let’s warm up with a bit of debugging practice.

    continue
    continue

    Last time we discussed how we can exit a loop early using the break statement:

    for (int i = 0; i < 10; i++) {
    if (i > 5) {
    break;
    }
    System.out.println(i);
    }

    break causes the enclosing loop—either while or for—to immediately exit, regardless of whether the loop condition is true or not.

    continue is another control statement that we can use inside a loop. Let’s explore how it works together!

    for (int i = 0; i < 8; i++) {
    System.out.println(i);
    }

    So while break causes the loop to exit, continue causes the code to return to the top of the loop immediately, check the loop condition, and then continue if appropriate.

    continue versus if
    continue versus if

    continue is used less frequently than break. One of the reasons is that, we can always rewrite any loop that uses continue to instead use an if statement. Let’s see how:

    for (int i = 0; i < 8; i++) {
    System.out.println(i);
    }

    What’s more clear: continue versus an if statement? It really depends on the problem. If the loop body is long and complicated, it can be better to use continue at the beginning to avoid values that shouldn’t be considered by the rest of the loop. However, overall continue can make your code hard to read. So for shorter loops, it’s usually better to just put the entire loop body inside an if statement instead.

    Solve: Array Sum Even Snippet (Practice)

    Created By: Geoffrey Challen
    / Version: 2022.1.0

    Given an int[] named array that is already declared and initialized, write a snippet of code (not a method), that prints a line with the sum of the values in array that are even.

    Counting Array Values
    Counting Array Values

    To get some additional practice, let’s work through an example together that is similar to this lesson’s homework. It also provides us a chance to practice with a very common array and loop programming pattern.

    Given an array of values, how would we count the number of values that have some property? For example, let’s say that we need to count the number of values which are greater than zero.

    Solution Outline
    Solution Outline

    Let’s work step-by-step. First, let’s outline what we need to do.

    int[] array = {-1, 42, 8, -12, 7};
    // Goal: count the number of positive values in array

    for Loop Setup
    for Loop Setup

    Next, let’s get our loop set up! It’s always a good idea to make sure that we can actually access all of the values in the array before doing anything more complicated.

    int[] array = {-1, 42, 8, -12, 7};

    Testing Values Using if
    Testing Values Using if

    As the third step, let’s review how we determine if a value is greater than zero, and integrate that into what we have so far.

    int[] array = {-1, 42, 8, -12, 7};

    Identifying a Design Pattern
    Identifying a Design Pattern

    Finally, let’s examine the design pattern that emerges from this example: a combination of iteration (our for loop) and selection (our if statement). By combining these two and using different conditions and data, we can solve a lot of different problems! We can also combine results in different ways as well.

    int[] array = {-1, 42, 8, -12, 7};

    Even and Odd
    Even and Odd

    Before we wrap up, let’s review one useful bit of Java that we’ll use regularly during some of our upcoming examples: How to test whether a value is even or odd.

    int even = 6;
    int odd = 5;

    Solve: Array Count Odd Snippet

    Created By: Geoffrey Challen
    / Version: 2022.1.0

    Given an int[] named array that is previously declared and initialized, write a snippet of code (not a method), that prints a line with the count of the number of values in array that are odd.

    More Practice

    Need more practice? Head over to the practice page.