Erin R. Hoffman, February 2022
Before this lesson, students should already be able to:
- Create variables in Java, particularly integers and doubles
- Use expressions to manipulate and combine variables
- Follow the appropriate order of operations in Java expressions, including math operations
By the end of this lesson, students will be able to:
- Define the term control flow and explain why it is a useful tool in programming
- Describe the two values of the boolean data type in Java
- Apply operators to variables in order to form boolean expressions
- Utilize boolean expressions in conditional statements that determine whether or not a given code block will be executed
- Practice translating English-language word problems into conditional statements and code blocks
When do we need control flow?
Workflow icons created by Uniconlabs - Flaticon
A better question might be...when do we not need control flow?
Some data types you have seen already include:
// Declaring and assigning variable
int month = 12;
// Printing variable value
System.out.println(month);
// Performing math operation on variable
System.out.println(month - 2);
// Performing multiple math operations, with parentheses for order of operations
System.out.println((month - 2) % 2);
// Declaring and assigning variable
double length = 1.5;
// Printing variable value
System.out.println(length);
// Performing math operation on variable
System.out.println(length * 7);
// Performing multiple math operations, with parentheses for order of operations
System.out.println(10 / (length + 0.5));
boolean isSunny = true;
isSunny
boolean isRaining = false;
isRaining
Clarification (click to expand)
Confusingly, there is also a wrapper object called Boolean
(note the capital "B"). We won't be using it, but you can read the documentation here if you're curious
- A data type is something like
int
ordouble
, which defines the "kind of thing" being stored in a variable - You have just been introduced to a data type called
boolean
- It can only have two values:
true
orfalse
Ok but...why?
Let's set up some example variables:
int lower = 0;
int upper = 100;
Is 5 the minimum?
5 == lower
Is the maximum 100?
upper == 100
Are the minimum and maximum the same?
lower == upper
Is 5 not the minimum?
!(5 == lower)
5 != lower
The user has input a value of 50. Is that more than the minimum?
50 > lower
Is it less than the maximum?
50 < upper
Now the user has input 100. Is that less than the maximum?
100 < upper
What if we want to define the valid range as 0 to 100 inclusive? Is 100 a valid input?
100 >= lower
100 <= upper
Can we check whether an input is valid in a single line?
(lower <= 100) && (100 <= upper)
(lower <= 500) && (500 <= upper)
(lower <= -5) && (-5 <= lower)
What if, instead of an inclusive range, inputs are valid if they're either below the lower bound or above the upper bound?
(100 <= lower) || (upper <= 100)
(500 <= lower) || (upper <= 500)
(-5 <= lower) || (upper <= -5)
- Boolean expressions are expressions that result in a boolean, although they can (and typically do) compare non-boolean values
- Boolean operators are used to make comparisons in boolean expressions. The Java boolean operators are:
==
(is equal to)!=
(is not equal to)>
(is greater than)<
(is less than)>=
(is greater than or equal to)<=
(is less than or equal to)&&
(and)||
(or)!
(not)
Now we can finally start to implement control flow!
Process icons created by Uniconlabs - Flaticon
User input is invalid if it's smaller than the minimum.
int userInput = -5;
if (userInput < lower) System.out.println("Input too small");
User input is also invalid if it's larger than the maximum.
if (userInput < lower) System.out.println("Input too small");
else if (userInput > upper) System.out.println("Input too big");
userInput = 500;
if (userInput < lower) System.out.println("Input too small");
else if (userInput > upper) System.out.println("Input too big");
Otherwise, user input is valid.
if (userInput < lower) System.out.println("Input too small");
else if (userInput > upper) System.out.println("Input too big");
else System.out.println("Valid input!");
userInput = 75;
if (userInput < lower) System.out.println("Input too small");
else if (userInput > upper) System.out.println("Input too big");
else System.out.println("Valid input!");
if (userInput < lower) {
System.out.println("Input too small");
} else if (userInput > upper) {
System.out.println("Input too big");
} else {
System.out.println("Valid input!");
}
- A conditional statement = a boolean expression +
if
,else if
, orelse
keyword - Conditional statements determine whether a given piece of code will be executed
- Typically this code is contained in a code block surrounded by curly braces
A tricky but exemplary conditional statement word problem is FizzBuzz, which comes from a children's game.
It goes like this:
- Given a number, print it out
- Except if the number is divisible by 3, print out "Fizz" instead
- And if the number is divisible by 5, print out "Buzz" instead
- Finally, if the number is divisible by both 3 and 5, print out "FizzBuzz" instead
// Given a number, print it out
// Except if the number is divisible by 3, print out "Fizz" instead
// And if the number is divisible by 5, print out "Buzz" instead
// Finally, if the number is divisible by both 3 and 5, print out "FizzBuzz" instead
Now you should be able to:
-
Define the term control flow and explain why it is a useful tool in programming
-
Describe the two values of the boolean data type in Java
-
Apply operators to variables in order to form boolean expressions
-
Utilize boolean expressions in conditional statements that determine whether or not a given code block will be executed
-
Practice translating English-language word problems into conditional statements and code blocks
That's all, folks! Up next, another type of control flow: loops.