Verified for the 2025 AP Computer Science A exam•Citation:
In programming, we often need our code to make decisions based on different conditions. This is where conditional statements come in, allowing our programs to execute different blocks of code depending on whether certain conditions are true or false. The if
statement is one of the most fundamental control structures in Java and other programming languages. By understanding how to use if
statements effectively, you'll be able to create programs that respond intelligently to different inputs and situations, making your code more dynamic and useful.
Control flow refers to the order in which statements are executed in a program. By default, Java executes statements sequentially (one after another, from top to bottom). However, conditional statements like if
allow us to interrupt this sequential flow and create branches in our code.
Sequential Flow: Branching Flow: ┌─────────────┐ ┌─────────────┐ │ Statement 1 │ │ Statement 1 │ └─────────────┘ └─────────────┘ ↓ ↓ ┌─────────────┐ ┌─────────────┐ │ Statement 2 │ │ Condition? │──→ True ──→┌─────────────┐ └─────────────┘ └─────────────┘ │ Statement A │ ↓ │ └─────────────┘ ┌─────────────┐ │ ↓ │ Statement 3 │ └──→ False ─→┌─────────────┐ └─────────────┘ │ Statement B │ └─────────────┘
The simplest form of the if
statement executes a block of code only if a specified condition is true:
if (condition) { // code to execute when condition is true }
For example:
int temperature = 75; if (temperature > 70) { System.out.println("It's a warm day!"); }
In this example, "It's a warm day!" will only be printed if the temperature is greater than 70.
true
or false
true
, the code block within the curly braces {}
is executedfalse
, the code block is skipped entirelyif
blockThe if-else
structure allows you to execute one block of code if the condition is true and a different block if it's false:
if (condition) { // code to execute when condition is true } else { // code to execute when condition is false }
Example:
int score = 85; if (score >= 60) { System.out.println("You passed!"); } else { System.out.println("You failed. Try again."); }
Since 85 is greater than or equal to 60, this code will print "You passed!"
When you need to check multiple conditions, you can use an if-else-if
ladder:
if (condition1) { // code to execute when condition1 is true } else if (condition2) { // code to execute when condition1 is false but condition2 is true } else if (condition3) { // code to execute when condition1 and condition2 are false but condition3 is true } else { // code to execute when all conditions are false }
Example (grading system):
int score = 85; char grade; if (score >= 90) { grade = 'A'; } else if (score >= 80) { grade = 'B'; } else if (score >= 70) { grade = 'C'; } else if (score >= 60) { grade = 'D'; } else { grade = 'F'; } System.out.println("Your grade is: " + grade);
With a score of 85, this code will assign 'B' to the grade variable and print "Your grade is: B".
You can place an if
statement inside another if
statement to create nested conditions:
if (outerCondition) { // code for when outerCondition is true if (innerCondition) { // code for when both outerCondition and innerCondition are true } }
Example:
int age = 25; boolean hasLicense = true; if (age >= 16) { System.out.println("Age requirement met."); if (hasLicense) { System.out.println("You can drive a car."); } else { System.out.println("You need to get a license first."); } } else { System.out.println("You're too young to drive."); }
When we have a single if
statement without an else
clause, this is called "one-way selection." The code block executes only when the condition is true; otherwise, it's skipped entirely.
if (temperature < 32) { System.out.println("Warning: Freezing temperatures!"); } // Program continues here regardless of the condition
This is useful when you only need to take an action in one specific case and do nothing special otherwise.
If you omit curly braces, only the first statement after the condition is controlled by the if
:
// INCORRECT (if you intended both lines to be controlled by the condition) if (isRaining) System.out.println("Take an umbrella."); System.out.println("Wear a raincoat."); // This always executes regardless of isRaining! // CORRECT if (isRaining) { System.out.println("Take an umbrella."); System.out.println("Wear a raincoat."); }
// INCORRECT - this assigns true to isRaining and then evaluates to true if (isRaining = true) { System.out.println("Take an umbrella."); } // CORRECT - this checks if isRaining equals true if (isRaining == true) { System.out.println("Take an umbrella."); } // Or better yet, simply: if (isRaining) { System.out.println("Take an umbrella."); }
// Redundant if (isRaining == true) { System.out.println("Take an umbrella."); } // Cleaner if (isRaining) { System.out.println("Take an umbrella."); } // Redundant if (isRaining == false) { System.out.println("Leave umbrella at home."); } // Cleaner if (!isRaining) { System.out.println("Leave umbrella at home."); }
Conditional statements are powerful because they alter the normal sequential flow of a program. Here's how different control structures affect program flow:
Control Structure | Effect on Program Flow |
---|---|
Sequential | Statements executed in order, top to bottom |
Selection (if ) | Program chooses between different paths based on conditions |
Iteration (loops) | Program repeats a section of code multiple times |
When writing if
statements, follow these best practices:
// Good style if (age >= 18 && hasValidID) { isEligibleToVote = true; }
if
statements allow your program to make decisions based on conditionsif
, if-else
, and if-else-if
if
without else
) is used when you only need to take action in one specific caseif
statements