Skip Top Navigation Bar

Using If Statements

As we saw in our algorithms section, we will want our programs to do different things based on conditions. Conditions are represented using Boolean expressions. 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.

In the example below, points is increased by 1 if the score is greater than or equal to 25. Try different values for score and see what the resulting value of points is after the if statement.

As a reminder, += is a compound operator that will update the variable to the sum of its current value and whatever is being added.

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.

In the example below, points is increased by 1 if the score is greater than or equal to 25 and decreased by 1 otherwise. Try it out and modify the value of score to see the effect on points.

As a reminder, the -= operator works similarly to the += operator. The value of points is updated to be the current value minus 1.

Formatting Of 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:

Nested If Statements

In some cases, you want to include an if statement within an if statement. This is referred to as a nested if statement.

In the example below, players who have accumulated 10 or more points will get a bonus that is two times the value of points. The if statement that adjusts bonus is nested in the if statement that adjusts the value of points if score is greater than or equal to 25. Try it out and adjust the initial value of points to be 10.

Equivalent Boolean Expressions

Sometimes there are many different ways to write boolean expression to get the result that you want. If you attempt to write a solution and it doesn't look exactly the way we present it in these tutorials, that doesn't mean that you are wrong necessarily. This is also true if you are working with another programmer. In this section, we will look at how to write statements that all do the same thing in a variety of ways.

In this example, we want to increase the grade level of a student, if they passed and are not a senior. If they passed and are a senior we want to print, "Congratulations!".

Recall that ++ updates the value of the variable to the current value plus 1.

Alternate Solution

The following is an alternate solution.

Not Equivalent Solution

The following solution is not equivalent. Update the code to initialize gradeLevel to 11. The first boolean condition will evaluate to true and gradeLevel will have the value 12. Then the second boolean condition will also evaluate to true and the program will print "Congratulations!".

Testing Your Code

As you can see, this last solution looks good on the surface but does not function the same way in the case of gradeLevel having an initial value of 11. Writing out the expected behavior of your program code before you start can be helpful when you test and debug your code later. For the code provided, here are some test cases that would help us identify whether the program code is function as we expect it to or not. Take a few minutes to run each test case on the original code, alternate solution, and the not equivalent alternate solution to see which case causes the not equivalent solution to fail.

Test Case Expected Result Description
  • gradeLevel = 9
  • passed = true
prints: 10 The value of gradeLevel will be increased to 10 and Congratulations! is not printed.
  • gradeLevel = 12
  • passed = true
prints: Congratulations! 12 The value of gradeLevel will stay 12 and Congratulations! will be printed.
  • gradeLevel = 11
  • passed = true
prints: 11 The value of gradeLevel will be increased to 12 and Congratulations! is not printed.
  • gradeLevel = 9
  • passed = false
prints: 10 The value of gradeLevel will stay the same and Congratulations! is not printed.
  • gradeLevel = 12
  • passed = false
prints: 12 The value of gradeLevel will stay the same and Congratulations! is not printed.
  • gradeLevel = 11
  • passed = false
prints: 11 The value of gradeLevel will stay the same and Congratulations! is not printed.

Resources