Conditionals and Control Flow
Conditionals and control flow in Java programs.
StartKey Concepts
Review core concepts you need to learn to master this subject
else Statement
else if Statements
if Statement
Nested Conditional Statements
AND Operator
NOT Operator
The OR Operator
Conditional Operators - Order of Evaluation
else Statement
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.
- 1Imagine 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…
- 2The 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!”); } - …
- 3We’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…
- 4The 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…
- 5We 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…
- 6An 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…
What you'll create
Portfolio projects that showcase your new skills
A Simple Car Loan Payment Calculator
It's time to build fluency in Java Control Flow. In this next Pro Project, we're going to practice conditionals in Java so you can hone your skills and feel confident taking them to the real world. Why? Mastering combining basic fundamentals such as conditionals and arithmetic expressions will help you to build effective programs. What's next? Car payment tracker, math fun, more Java. You got this!
Continents and Cities
In this Pro project, we're going to practice the switch statement in Java so you can hone your skills and feel confident taking them to the real world. 7 continents, 7 cities, 1 expression. You got this!
How you'll master it
Stress-test your knowledge with quizzes that help commit syntax to memory