The AP Computer Science A 5-hour live stream review is here!Β πŸ’»

Join us on May 5, 2021 for the 🌢️ AP Computer Science A Cram Finale for a last minute review to get all your questions answered!


All Subjects

Β >Β 


AP Comp Sci A

Β >Β 


Unit 3

3.3 If-Else Statements

1 min readβ€’november 16, 2020

Peter Cao

Caroline Koffke

3.3: If-Else Statements

We have just covered if statements, but what if we want something to be done if the condition that we are checking is false? This is where the else statement comes into play. The header for the else does not have a condition and is just "else" while the body is what is performed if the condition for the if statement is false.
Together, these create an if-else statement, or a two-way selection. Here is the anatomy of an if-else statement:
some code that runs before conditional statement if (condition) { code that runs if the condition is true } else { code that runs if the condition is false } some code that runs after

Example: Number Rounding

Remember the rounding we learned in 1.5? Now with an if-else statement, we can finally write a method that implements this as follows:
public static int round(double number) { if (number >= 0) { return (int) (number + 0.5); } else { return (int) (number - 0.5); } }

Was this guide helpful?

πŸ’ͺ🏽 Are you ready for the Comp Sci exam?
Take this quiz for a progress check on what you’ve learned this year and get a personalized study plan to grab that 5!
Hours Logo
Studying with Hours = the ultimate focus mode
Start a free study session
FREE AP comp sci a Survival Pack + Cram Chart PDF
Sign up now for instant access to 2 amazing downloads to help you get a 5
Browse Study Guides By Unit
Exam Reviews
Unit 10: Recursion
Unit 1: Primitive Types
Unit 2: Using Objects
Unit 4: Iteration
Unit 5: Writing Classes
Unit 6: Array
Unit 7: ArrayList
Unit 8: 2D Array
Unit 9: Inheritance
Join us on Discord
Thousands of students are studying with us for the AP Computer Science A exam.
join now
πŸ“± Stressed or struggling and need to talk to someone?
Talk to a trained counselor for free. It's 100% anonymous.
Text FIVEABLE to 741741
Β© 2021 Fiveable, Inc.