Lesson 3
Conditional Statements and Loop Control in Java
Topic Overview

Welcome! In this lesson, we're exploring special instructions in the Java language: Conditional Statements, along with the break and continue statements. As we've learned, loops allow us to execute a block of code numerous times. By combining these loops with conditional statements and incorporating the useful break and continue instructions, we achieve more robust and efficient code. Let's dive in!

The 'if' Statement

In Java, the if statement triggers actions in our code based on a specific condition. Consider this straightforward example where the if statement determines which message to print based on the value of temperature:

Java
1class Solution { 2 public static void main(String[] args) { 3 int temperature = 15; 4 if (temperature > 20) { 5 System.out.println("Wear light clothes."); // This message will print if the temperature is over 20. 6 } else { 7 System.out.println("Bring a jacket."); // This message will print otherwise. 8 } 9 } 10}

We can evaluate multiple conditions using else if. This phrase means, "If the previous condition isn't true, then check this one":

Java
1class Solution { 2 public static void main(String[] args) { 3 int temperature = 15; 4 5 if (temperature > 30) { 6 System.out.println("It's hot outside!"); // This will print if the temperature is over 30. 7 } else if (temperature > 20) { 8 System.out.println("The weather is nice."); // This will print if the temperature is between 21 and 30. 9 } else { 10 System.out.println("It might be cold outside."); // This will print if the temperature is 20 or below. 11 } 12 } 13}
The 'break' Statement

We use the break statement whenever we want to exit a loop prematurely once a condition is met:

Java
1import java.util.ArrayList; 2import java.util.Arrays; 3 4class Solution { 5 public static void main(String[] args) { 6 ArrayList<Integer> numbers = new ArrayList<>(Arrays.asList(1, 3, 7, 9, 12, 15)); 7 8 for (int i = 0; i < numbers.size(); i++) { 9 if (numbers.get(i) % 2 == 0) { 10 System.out.println("The first even number is: " + numbers.get(i)); // This prints the first even number. 11 break; // This stops the loop after printing the first even number. 12 } 13 System.out.println("Number: " + numbers.get(i)); 14 } 15 } 16}
The 'continue' Statement

The continue statement bypasses the rest of the loop code for the current iteration only:

Java
1class Solution { 2 public static void main(String[] args) { 3 for (int i = 0; i < 6; i++) { 4 if (i == 3) { 5 continue; // This skips the print command for '3'. 6 } 7 System.out.println(i); // This prints the numbers from 0 to 5 except 3. 8 } 9 } 10}
Use-case with a For Loop

By utilizing the tools we've covered so far, we can craft more flexible loops. Here's a snippet where we terminate the loop once we find "Charlie":

Java
1import java.util.ArrayList; 2import java.util.Arrays; 3 4class Solution { 5    public static void main(String[] args) { 6        ArrayList<String> names = new ArrayList<>(Arrays.asList("Alice", "Bob", "Charlie", "David")); 7 8        for (String name : names) { 9            if (name.equals("Charlie")) { 10                System.out.println("Found Charlie!"); // This prints when 'Charlie' is found. 11                break; // This stops the loop after finding 'Charlie'. 12            } 13        } 14    } 15}
Lesson Summary and Practice

Congratulations! You are now familiar with Java's if statement, as well as the break and continue statements and their applications in loops. We encourage you to reinforce your learning through the upcoming practice exercises. Happy coding!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.