Key Concepts

Review core concepts you need to learn to master this subject

else Statement

boolean condition1 = false; if (condition1){ System.out.println("condition1 is true"); } else{ System.out.println("condition1 is not true"); } // Prints: condition1 is not true

The else statement executes a block of code when the condition inside the if statement is false. The else statement is always the last condition.

Conditionals and Control Flow
Lesson 1 of 2
  1. 1
    Imagine we’re writing a program that enrolls students in courses. - If a student has completed the prerequisites, then they can enroll in a course. - Else, they need to take the prerequisit…
  2. 2
    The if-then statement is the most simple control flow we can write. It tests an expression for truth and executes some code based on it. if (flip == 1) { System.out.println(“Heads!”); } - …
  3. 3
    We’ve seen how to conditionally execute one block of code, but what if there are two possible blocks of code we’d like to execute? Let’s say if a student has the required prerequisite, then t…
  4. 4
    The conditional structure we’ve learned can be chained together to check as many conditions as are required by our program. Imagine our program is now selecting the appropriate course for a stude…
  5. 5
    We can create more complex conditional structures by creating nested conditional statements, which is created by placing conditional statements inside other conditional statements: if (outer con…
  6. 6
    An alternative to chaining if-then-else conditions together is to use the switch statement. This conditional will check a given value against any number of conditions and run the code block where t…
  7. 7
    Before this lesson, our code executed from top to bottom, line by line. Conditional statements add branching paths to our programs. We use conditionals to make decisions in the program so that di…

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo