If Statements and Control Flow¶
The statements in a Java main method normally run or execute one at a time in the order they are found. If statements (also called conditionals or selection) change the flow of control so that certain lines of code only run when something is true. An if statement checks a boolean condition that is either true or false. A block of statements will execute if the condition is true and will be skipped if the condition is false.
A conditional uses the keyword if
followed by Boolean expression inside of an
open parenthesis (
and a close parenthesis )
and then followed by a single statement or block of statements.
The single statement or block of statements are only executed if the condition is true.
The open curly brace {
and a close curly brace }
are used to group a block of statements together.
It is recommended to always put in the curly braces even if you have just one statement under the if statement.
1 statement | 1 statement | 2+ statements |
---|---|---|
{} optional | {} optional | {} required |
if (x<10) | if (x<10) | if (x<10) |
count++; | { | { |
count++; | count++; | |
} | System.out.println(count); | |
} |
Note
Note that there is no semicolon (;) at the end of the boolean expression in an if statement even if it is the end of that line. The semicolon goes at the end of the whole if statement, often on the next line. Or { } are used to mark the beginning and end of the block of code under the if condition.
Imagine that your cell phone wanted to remind you to take an umbrella if it was currently raining in your area when it detected that you were leaving the house. This type of thing is going to become more common in the future and it is an area of research called Human Computer Interaction (HCI) or Ubiquitous Computing (computers are everywhere).
Examine the code in the E01TestRaining
program. Play with the isRaining
variable and make sure you understand what is happening.
You can test for a false value using the !
operator, which is read as “not”. We will see a better way to test for both
true and false in the next lesson. However, the code below shows how to print different messages based on whether a value is true or false.
The
E02TestMidterm
program reads in a boolean value from standard input and tests whether the value is trueif (passedExam)
or falseif (!passedExam)
. Use the debugger to step through the program. Change the value in the standard input window to test the program with each possible boolean value.
Note
In an if statement
, it is good style to indent the lines of code nested between the curly braces.
- Because x < 2 is always true
- Incorrect, 3 < 2 is always false so the print statement should never execute
- Because there is a semicolon ; at the end of the condition "if (x < 2) ;"
- Correct. Get rid of the ; at the end of the condition "if (x < 2) "
- Because Java makes a random decision
- Incorrect, 3 < 2 is always false so the print statement should never execute
3-2-1: Consider the following code segment. Why does it always print “ERROR”?
int x = 3;
if (x < 2) ; {
System.out.println("ERROR");
}
Relational Operators in If Statements¶
Most if statements have a boolean condition that uses relational operators like ==, !=, <, >, <=, >=, as we saw in the last lesson.
Run the
E03TestNumbers
program a couple times until you see all the possible outputs. It prints out whether a random number is positive or equal to 0. Add another if statement that tests if it is a negative number.
Note
A common mistake in if statements is using = instead of == in the condition by mistake. You should always use ==, not =, in the condition of an if statement to test a variable. One equal sign (=) assigns a value to a variable, and two equal signs (==) test if a variable has a certain value.
- 3
- x is changed by the if statements.
- 6
- What happens when x is greater than 2 and then greater than 4? Do both if statements.
- 0
- If x is greater than 2, it's always doubled, and then that result is always greater than 4, so it's set to 0 in the second if statement.
- 4
- x is changed by the if statements.
- The code will not compile
- This code will compile.
3-2-2: Consider the following code segment. What is printed as a result of executing the code segment?
int x = 3;
if (x > 2) {
x = x * 2;
}
if (x > 4) {
x = 0;
}
System.out.print(x);
Conditional Control Flow¶
Recall the program to compute the number of pizza slices per person from Module 1.
Run the
E04PizzaCalculatorInput
program to confirm that it fails when a value of 0 is entered for numPeople (second input value).
To avoid division by 0, the calculation for slicesPerPerson
and leftoverSlices
should be skipped when the number of people is 0.
In fact, the calculation is nonsense if the number of people is a negative number so the program should check if a positive value is read from input.
The program should actually ensure positive values are input for both the number of people and number of pizza slices, but for now you will only test the number of people.
You will see how to test compound boolean expressions in a later lesson.
The flowchart in Figure 2 demonstrates the desired control flow based on an if
statement, represented with a diamond symbol.
If the condition numPeople > 0
is true, the process follows the path labelled true, which contains the 4 steps to calculate and
print slicesPerPerson
and leftoverSlices
.
The 4 statements along the true
branch must be nested within curly braces in a Java program otherwise only the first step would be considered part of the true branch.
If the condition numPeople > 0
is false, the false branch is followed and the 4 statements for calculating and printing are skipped.
Update the
E04PizzaCalculatorInput
program based on the conditional control flow shown in Figure 2. Add an if statement to test the value stored in numPeople. Don’t forget curly braces around the 4 lines for computing and printing slicesPerPerson and leftoverSlices.Run the program multiple times with negative, 0, and positive values for number of people. The program should no longer result in a divide by zero exception.
Common Errors with If Statements¶
Here are some rules to follow to avoid common errors:
Always use curly brackets
{
and}
to enclose the block of statements under the if condition. Java doesn’t care if you indent the code – it goes by the { }.Don’t put in a semicolon
;
after the first line of the if statement, if (test);. The if statement is a multiline block of code that starts with the if condition and then { the body of the if statement }.Always use ==, not =, in the condition of an if statement to test a variable. One = assigns, two == tests!
The
E05TestCold
program doesn’t work as expected. It has 2 errors. Run the program with input true, then change the input to false and run again. Even when the input is false, the program still prints both messages. Fix it to only print both “Wear a coat” and “Wear gloves” when isCold is true. Nothing should print when isCold is false.
Summary¶
if statements test a boolean expression and if it is true, go on to execute the following statement or block of statements surrounded by curly brackets { } like below.
// A block if statement
if (boolean expression) {
Do Statement1;
Do Statement2;
...
Do StatementN;
}
Java boolean expressions can compare primitive values and reference values with the relational operators == and != and arithmetic expression values with the relational operators (i.e., <, >, <=, >=).
Conditional (if) statements affect the flow of control by executing different statements based on the value of a Boolean expression.