Chevron Left Icon
Conditionals and Control Flow
Lesson 1 of 2
Chevron Right Icon
  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 execute some code based on it. if (flip == 1) { System.out.println(“Heads!”); } - T…
  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
    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…
  6. 6
    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…
  1. 1
    Java includes operators that only use boolean values. These conditional operators help simplify complex boolean relationships by reducing multiple boolean values to a single value. For example, …
  2. 2
    Let’s return to our student enrollment program. We’ve added an additional requirement: not only must students have the prerequisite, but their tuition must be paid up as well. We have two conditi…
  3. 3
    The requirements of our enrollment program have changed again. Certain courses have prerequisites that are satisfied by multiple courses. As long as students have taken at least one prerequisit…
  4. 4
    The unary operator NOT, !, works on a single value. This operator evaluates to the opposite boolean to which it’s applied: !false // true !true // false NOT is useful for expressing our i…
  5. 5
    Conditional operators work on boolean values to simplify our code. They’re often combined with conditional statements to consolidate the branching logic. Conditional-AND, &&, evaluates to true if …

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