Using 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.
Your Turn
Let's try it in the Java Playground.
- In the example below, players who have accumulated
10or more points will get a bonus that is two times the value ofpoints. Theifstatement that adjustsbonusis nested in the if statement that adjusts the value ofpointsifscoreis greater than or equal to25. - Predict the output of the code.
- Run the code to determine if your prediction is correct.
- Adjust the initial value of
pointsto be10. - Predict the output of the code.
- Run the code to determine if your prediction is correct.
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