Unit 2.3: if Statements

Learning Objective

2.3.A: Develop code to represent branching logical processes by using selection statements and determine the result of these processes.

Essential Knowledge

  • Selection statements change the sequential execution of statements
  • if statements affect the flow of control by executing different segments of code based on Boolean expressions
  • One-way selection (if statement) executes code under certain conditions
  • Two-way selection (if-else statement) chooses between two code segments

Selection statements allow programs to make decisions and respond dynamically to different conditions!

Sequential vs Selection Execution

Sequential Execution (Default)

Normally, Java executes statements one after another in order:

System.out.println("First statement"); System.out.println("Second statement"); System.out.println("Third statement");

Selection Execution

Selection statements change the sequential execution by allowing the program to choose which statements to execute based on conditions:

System.out.println("Always executed"); if (condition) { System.out.println("Only if condition is true"); } System.out.println("Always executed after if");

Selection statements give programs the power to make decisions and branch to different code paths!

One-way Selection (if Statement)

Syntax

if (Boolean_expression) { // Code to execute if condition is true }

How it Works

  • The Boolean expression is evaluated
  • If true: the code inside the braces executes
  • If false: the code inside the braces is skipped
  • Program continues with the next statement after the if block

Example

int score = 95; if (score >= 90) { System.out.println("Excellent work!"); System.out.println("You earned an A!"); } System.out.println("End of program");

Output: Since 95 >= 90 is true, both messages inside the if block print, followed by "End of program".

Interactive: One-way Selection

int temperature = /* your input */; if (temperature > 80) { System.out.println("It's hot outside!"); System.out.println("Stay hydrated!"); } System.out.println("Weather check complete.");
Click "Run Code" to see the result

Try These Values:

  • 85 (should print the hot weather messages)
  • 75 (should skip the if block)
  • 80 (exactly 80 - what happens?)

Two-way Selection (if-else Statement)

Syntax

if (Boolean_expression) { // Code to execute if condition is true } else { // Code to execute if condition is false }

How it Works

  • The Boolean expression is evaluated
  • If true: execute the if block, skip the else block
  • If false: skip the if block, execute the else block
  • Exactly one of the two blocks will always execute

Example

int age = 16; if (age >= 18) { System.out.println("You can vote!"); } else { System.out.println("You cannot vote yet."); System.out.println("Wait " + (18 - age) + " more years."); }

Output: Since 16 >= 18 is false, the else block executes.

Interactive: Two-way Selection

int grade = /* your input */; if (grade >= 70) { System.out.println("Congratulations! You passed!"); System.out.println("Your grade: " + grade); } else { System.out.println("You need to retake the exam."); System.out.println("You need " + (70 - grade) + " more points."); }
Click "Run Code" to see the result

Try These Values:

  • 85 (should show passing message)
  • 65 (should show retake message)
  • 70 (exactly 70 - boundary condition)

Common Patterns and Best Practices

Nested if Statements

if (score >= 90) { if (score >= 97) { System.out.println("A+"); } else { System.out.println("A"); } } else { System.out.println("Below A level"); }

Multiple Conditions with else if

if (score >= 90) { System.out.println("A"); } else if (score >= 80) { System.out.println("B"); } else if (score >= 70) { System.out.println("C"); } else { System.out.println("F"); }

Best Practices

  • Always use braces { } even for single statements
  • Test boundary conditions (like exactly equal values)
  • Use meaningful variable names in conditions
  • Keep conditions simple and readable

Practice and Review

Key Concepts Review

One-way selection: Use when you want to execute code only under certain conditions

Two-way selection: Use when you want to choose between two different actions

Practice Problem

Write an if-else statement that:

  • Takes a variable called password
  • If the password length is at least 8 characters, print "Strong password"
  • Otherwise, print "Password too short"
String password = "myPass123"; // Your code here if (password.length() >= 8) { System.out.println("Strong password"); } else { System.out.println("Password too short"); }

AP Exam Tips

  • Trace through code carefully with given input values
  • Pay attention to boundary conditions (>=, >, <=, <)
  • Remember that exactly one branch executes in if-else
  • Watch for nested if statements and proper indentation
1 / ?