If-Else Statements

Caleb Thomas
5 min read
Listen to this study note
Study Guide Overview
This study guide covers if-else statements in Java, including syntax, code blocks, and practical examples like a rounding method. It also provides practice questions with solutions and a scoring guide. Finally, it highlights key exam topics like Object-Oriented Programming, Arrays and ArrayLists, Recursion, and Sorting and Searching, along with common question types and last-minute exam tips.
AP Computer Science A: Ultimate Study Guide
Hey there! Let's get you prepped and confident for your AP Computer Science A exam. This guide is designed to be your go-to resource, especially the night before the test. We'll keep it clear, engaging, and super focused on what you need to know. Let's dive in!
Control Structures
If-Else Statements
So, we've tackled if
statements, but what happens when the condition is false? That's where the else
statement jumps in! Think of it as the backup plan. If the if
condition isn't met, the else
block executes. Together, they form an if-else statement, a two-way street for your code.
// 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
Remember those curly braces {}
? They're super important! They define the code blocks for both if
and else
. Always use them to avoid confusion and keep your code organized. Indentation also helps a lot to keep the code readable!
Example: Number Rounding
Let's put this into action with a rounding method. No more manual rounding! ๐
public static int round(double number) {
if (number >= 0) {
return (int) (number + 0.5);
} else {
return (int) (number - 0.5);
}
}
Think of if-else
as a fork in the road. If the condition is true, you go one way (the if
block); otherwise, you take the other path (the else
block).
Forgetting the curly braces {}
! This can lead to unexpected behavior and logic errors. Always use them to define your code blocks.
Practice Questions
Practice Question
Multiple Choice Questions
- What is the output of the following code snippet?
int x = 5;
if (x > 10) {
System.out.print("A");
} else {
System.out.print("B");
}
a) A
b) B
c) A B
d) No output
2. Which of the following is the correct syntax for an if-else statement?
a) if condition { } else { }
b) if (condition) { } else { }
c) if condition then { } else { }
d) if (condition) then { } else { }
Free Response Question
Write a method called checkEvenOdd
that takes an integer as a parameter and prints "Even" if the number is even and "Odd" if the number is odd. You must use an if-else statement.
public static void checkEvenOdd(int num) {
// your code here
}
Scoring Guide
- +1 point for correct method signature
- +1 point for checking if the number is even using the modulo operator
%
- +1 point for printing "Even" when the number is even
- +1 point for printing "Odd" when the number is odd
Sample solution
public static void checkEvenOdd(int num) {
if (num % 2 == 0) {
System.out.println("Even");
} else {
System.out.println("Odd");
}
}
Final Exam Focus
Alright, let's talk strategy for the big day. Hereโs what to keep top of mind:
- High-Value Topics:
- Object-Oriented Programming (OOP): Classes, objects, inheritance, polymorphism. It's a biggie!
- Arrays and ArrayLists: Manipulating and traversing data structures. Expect these!
- Recursion: Understanding recursive methods and base cases. Tricky but important.
- Sorting and Searching: Know the basic algorithms and their efficiency.
- Common Question Types:
- Code Tracing: Carefully follow code execution to predict output.
- Method Writing: Implement methods that solve specific problems.
- FRQs: Design and implement classes and methods.
- Last-Minute Tips:
- Time Management: Don't get stuck on one question. Move on and come back if needed.
- Read Carefully: Pay close attention to instructions and constraints.
- Practice: Work through practice problems to build confidence.
Remember, you've got this! Stay calm, focused, and trust your preparation.

How are we doing?
Give us your feedback and let us know how we can improve
Question 1 of 8
What will be the output of the following code snippet? ๐ค
int num = 7;
if (num < 5) {
System.out.print("Small");
} else {
System.out.print("Large");
}
Small
Large
Small Large
No output