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,
pointsis increased by1if thescoreis greater than or equal to25. - 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
scoreand see what the resulting value ofpointsis after theifstatement.
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,
pointsis increased by1if thescoreis greater than or equal to25and decreased by1otherwise. - As a reminder, the
-=operator works similarly to the+=operator. The value ofpointsis updated to be the current value minus1. - Predict the output.
- Run the code to determine if your prediction is correct.
- Modify the value of
scoreto see the effect onpoints.
Formatting If Statements
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
ifthere is a space and then parenthesis for the condition - the opening brace (
{) for theifstatement appears on the same line asif (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
elseit is on the same line as the closing brace for when theifstatement iftrueand there is a space between the closing brace (}) and theelse - the opening brace (
{) for theelseis on the same line as theelseand there is a space between theelseand the opening brace ({) - the body of the
ifandelsethat is between the braces ({ }) is indented
Resources
- Practice: Evaluate Expressions that Contain if Statements
- Practice: Writing Expressions that contain if statements
- Practice: AP Computer Science A Free Response 1 with if statements