Using If Statements
As we saw in our algorithms section, we will want our programs to do different things based on conditions. One way to have a program behave differently under different conditions is to use if statements.
If Statements
An if statement allows you to perform certain functions when a Boolean expression is true and other functions (or nothing at all) when a Boolean expression is false.
The structure of an if statement is as follows:
if (<boolean expression>) {
//statements to execute if the boolean expression is true
}
With an if statement, the statements in the body of the if statement, the ones enclosed in braces ({ }), are only executed in the boolean expression evaluates to true. If the boolean expression evaluates to false, these statements are skipped. Either way, the program continues with the next statement after the closing brace.
Tracing if Statements
Your Turn
Let's try it in the Java Playground.
- In the example below,
points is increased by 1 if the score is greater than or equal to 25.
- As a reminder,
+= is a compound operator that will update the variable to the sum of its current value and whatever is being added.
- Predict the output.
- Run the code to determine if your prediction is correct.
- Try different values for
score and see what the resulting value of points is after the if statement.
if..else Statements
Often we have a set of statements we want to execute when the boolean expression evaluates to true and a different set of statements when the boolean expression evaluates to false.
Tracing if..else Statements
Your Turn
Let's try it in the Java Playground.
- In the example below,
points is increased by 1 if the score is greater than or equal to 25 and decreased by 1 otherwise.
- As a reminder, the
-= operator works similarly to the += operator. The value of points is updated to be the current value minus 1.
- Predict the output.
- Run the code to determine if your prediction is correct.
- Modify the value of
score to see the effect on points.
The way code is formatted is mostly for those that are reading the code. If you are working with other programmers on a project or if you need to add to an existing project, the format of the code can help make the code more readable. Often, style guides exist to help make program code more uniform. A few style things to note:
- after
if there is a space and then parenthesis for the condition
- the opening brace (
{) for the if statement appears on the same line as if (boolean condition) and there is a space between the closing parenthesis ()) and the opening brace
- the closing brace (
}) is on a line all on it's own
- when we use an
else it is on the same line as the closing brace for when the if statement if true and there is a space between the closing brace (}) and the else
- the opening brace (
{) for the else is on the same line as the else and there is a space between the else and the opening brace ({)
- the body of the
if and else that is between the braces ({ }) is indented
Resources
Next Learn Tutorial
-Learn: Using Nested if Statements