Conditionals and Control Flow
Intoduction to Control Flow
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 prerequisite courses.
For example, a student cannot take Physics II without first completing Physics I.
We represent this kind of decision-making in our program using conditional or control flow statements. Before this point, our code runs line-by-line from the top down, but conditional statements allow us to be selective in which portions will run.
Conditional statements check a boolean
condition and run a block of code depending on the condition. Curly braces mark the scope of a conditional block similar to a method or class.
Here's a complete conditional statement:
1 2 3 |
|
true
, then the block is run, which results in Hello World!
being printed. But suppose the condition is different:
1 2 3 |
|
false
, then the block does not run. This code is also called if-then statements: "If (condition)
is true
, then do something".
If-Then Statement
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.
1 2 3 |
|
-
the
if
keyword marks the beginning of the conditional statement, followed by parentheses()
. -
The parentheses hold a
boolean
datatype.
For the condition in parentheses we can also use variables that reference a boolean, or comparisons that evaluate to a boolean.
The boolean condition is followed by opening and closing curly braces that mark a block of code. This block runs if, and only if, the boolean is true
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
1 |
|
If-Then-Else
We've seem 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 they enroll in the selected course, else they're enrolled in the prerequisite course instead.
We create an alternate conditional branch with the else
keyword:
1 2 3 4 5 |
|
hasPrerequisite
, is false
, the block after else
runs. There are now two separate code blocks in our conditional statement. The first block runs if the condition evaluates to true
, the second block runs if the condition evaluates to false
.
This code is also called an if-then-else statement:
-
If condition is true, then do something.
-
Else, do a different thing.
If-Then-Else-If
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 student. We'll check their submission to find the correct course enrollment.
The conditional statement now has multiple conditions that are evaluated from the top down:
1 2 3 4 5 6 7 8 9 10 11 |
|
true
will have that code block run. Here's an example demonstrating the order: 1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
true
. Because testScore >= 70
comes before testScore >= 60
, only the earlier code block is run. Note: Only one of the code blocks will run.
Nested Conditional Statements
We can create more complex conditional structures by creating nested contitional statements, which is created by placing conditional statements inside other conditional statements:
1 2 3 4 5 |
|
true
, then the inner, nested statement is evaluated. Let's create a program that helps us decide what to wear based on the weather:
1 2 3 4 5 6 7 8 9 10 11 |
|
if-then
statement: temp < 60
. Since temp
has a value of 45
, this condition is true
; therefore, our program will print Wear a jacket!
. Then, we'll evaluate the condition in the nested if-then
statement: raining == true
. This condition is also true
, so Bring your umbrella
is also printed to the screen.
Note that, if the first condition was false
, the nested condition would not be evaluated.
Switch Statement
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 there is a match.
Here's an example of our course selection conditional as a switch
statement instead:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
course
, against each of the case
labels. If the value after the case lable matches the value within the parentheses, the switch block is run. In the above example, course
references the string "History"
, which matches case "History":
.
When no value matches, the default
block runs. Think of this as the else
equivalent.
Switch blocks are different than other code blocks because they are not marked by curly braces and we use the break
keyword to exit the switch statement.
Without break
, code below the matching case
label is run, including code under other case labels, which is rareley the desired behavior.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
Conditional/Control Flow Review
Conditional statements add branching paths to our programs. We use conditionals to make decisions in the program so that different inputs will produce different results.
Conditionals have the general structure:
1 2 3 4 5 |
|
-
if-then
:- Code block runs if the condition is true.
-
if-then-else
:-
One block runs if condition is true.
-
Another block runs if condition is false.
-
-
if-then-else
chained:- Same as
if-then
but an arbitrary number of conditions.
- Same as
-
switch
:- Switch block runs if condition matches the
case
value.
- Switch block runs if condition matches the
Introduction to Conditional Operators
Java includes operators that only use boolean values. These conditional operators help simplify expressions containing complex boolean relationships by reducing multiple boolean values to a single use: true
or false
.
For example, what if we want to run a code block only if multiple conditions are true. We could use the AND operator: &&
.
Or, we want to run a code block if at least one of two conditions are true
. We could use the OR operator: ||
.
Finally, we can produce the opposite value, where true
becomes false
and false
becomes true
, with the NOT operator: !
.
In the following notes, we'll go over each of these conditional operators to see how they can be implemented into our conditional statements.
AND
A | B | A&&B |
---|---|---|
true | true | true |
true | false | false |
false | true | false |
false | false | false |
OR
A | B | A||B |
---|---|---|
true | true | true |
true | false | true |
false | true | true |
false | false | false |
NOT
A | !A |
---|---|
true | false |
false | true |
Conditional-AND: &&
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 conditionals that must be true
before we enroll the student.
Here is one way we can write the code:
1 2 3 4 5 |
|
if-then
statements. This does the job but we can be more concise with the AND operator: 1 2 3 |
|
&&
, is used between two boolean values and evaluates to a single boolean value. If the values on both sides are true
, then the resulting value is true
, otherwise the resulting value is false
. This code illustrates every combination:
true && true
// true
false && true
// false
true && false
// false
false && false
// false
Conditional-OR: ||
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 prerequisite, they should be allowed to enroll.
Here's one way we could write the code:
1 2 3 4 5 6 7 |
|
if-then
statements with the same code block. We can be more concise with the OR operator: 1 2 3 |
|
||
, is used between two boolean values and evaluates to a single boolean value. If either of the two values are true
, then the resulting value is true
, otherwise the resulting value is false
. This code illustrates every combination:
true || false
// true
true || false
// true
false || true
// true
false || false
// false
Logical NOT: !
The unary operator NOT, !
, works on a single value. This operator evaluates to the opposite boolean to which it is applied:
!false
// true
!true
// false
if-then
: run a code block only if the condition is false
. 1 2 3 4 5 6 7 |
|
The logical NOT operator cleans up our example:
1 2 3 4 5 |
|
Combining Conditional Operators
We have the ability to expand our boolean expressions by using multiple conditional operators in a single expression.
For example:
boolean foo = true && !(false || !true)
-
Conditions placed in parentheses -
{}
-
NOT -
!
-
AND -
&&
-
OR -
||
Using this information, let's dissect the expression above to find the value of foo
:
true && !(false || !true)
(false || !true)
because it is enclosed within parentheses. Following the order of evaluation, we will evaluate !true
, which equals false
:true && !(false || false)
(false || false)
which equals false
. Now our expression looks like this: true && !false
!false
because it uses the NOT operator. This expression equals true
making our expression the following:true && true
true && true
evaluates to true
; therefore, the value of foo
is true
. Conditional Operators Review
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 the booleans on both sides are true
.
Conditional-OR, ||
, evaluates to true
if one or both oif the booleans on either side is true
.
Conditional-NOT, !
, evaluates to the opposite boolean value to which it is applied.