Mr.Mou @ ShiShi AP Center

AP CSA 2.3:if 语句

Topic 2.2 introduced Boolean expressions that evaluate to true or false. Now Java can use those results to decide which statements should run.

在 2.2 中,我们学习了结果为 truefalse 的布尔表达式。现在,Java 可以根据这些结果决定哪些语句应该执行。

An if statement checks a Boolean expression. The result determines which code runs next. if 语句会检查一个布尔表达式,并根据结果决定接下来执行哪些代码。

Core Concepts:
核心知识点

1. Trace Every if in Order
按顺序追踪每一个 if

What is printed?

下面的代码会输出什么?

int value = 4;

if (value > 3)
{
    value *= 2;
}

if (value > 6)
{
    value -= 5;
}

System.out.println(value);

Answer:

3

Trace the code from top to bottom:

按照从上到下的顺序追踪代码:

value starts at 4

4 > 3 → true
value becomes 8

8 > 6 → true
value becomes 3

The second condition uses the updated value of value, not its original value.

第二个条件使用的是 value 更新后的值,而不是它最开始的值。

Both if statements are independent, so Java checks both of them.

这两个 if 语句彼此独立,因此 Java 会分别检查它们。

2. One-Way Selection: if
单向选择:if

A one-way selection runs a block only when its condition is true.

单向选择只会在条件为真时执行对应的代码块。

boolean isRaining = false;

if (isRaining)
{
    System.out.println("Take an umbrella.");
}

System.out.println("Drive carefully.");

What is printed?

输出是什么?

Drive carefully.

Because isRaining is false, Java skips the body of the if statement.

因为 isRainingfalse,Java 会跳过 if 语句的代码块。

The statement after the if is not part of the selection, so it still runs.

if 后面的语句不属于这个选择结构,因此仍然会执行。

The general form is:

基本结构如下:

if (booleanExpression)
{
    statements;
}

Java follows these steps:

Java 会按照下面的过程执行:

  1. Evaluate the Boolean expression. 计算布尔表达式。

  2. If the result is true, run the body. 如果结果为 true,执行代码块。

  3. If the result is false, skip the body. 如果结果为 false,跳过代码块。

  4. Continue with the next statement. 继续执行后面的语句。

3. Two-Way Selection: if-else
双向选择:if-else

An if-else statement chooses exactly one of two paths.

if-else 语句会从两条路径中选择一条执行。

int score = 72;

if (score >= 60)
{
    System.out.println("Pass");
}
else
{
    System.out.println("Keep trying");
}

System.out.println("Done");

Output:

Pass
Done

Because score >= 60 is true, the if branch runs and the else branch is skipped.

因为 score >= 60 为真,所以执行 if 部分,并跳过 else 部分。

After the selection finishes, execution continues with "Done".

选择结构结束后,程序继续输出 "Done"

The general form is:

基本结构如下:

if (booleanExpression)
{
    statementsForTrue;
}
else
{
    statementsForFalse;
}
Condition Branch that runs
true if body
if 代码块
false else body
else 代码块

In a single if-else statement, both branches cannot run.

在同一个 if-else 结构中,两条分支不会同时执行。

4. Separate if Statements Are Not if-else
两个独立的 if 不等于 if-else

Compare these two code segments.

比较下面两段代码。

Two independent if statements
两个独立的 if

int x = 8;

if (x > 5)
{
    System.out.print("A");
}

if (x < 10)
{
    System.out.print("B");
}
AB

Both conditions are true, so both blocks run.

两个条件都为真,因此两个代码块都会执行。

One if-else statement
一个 if-else

int x = 8;

if (x > 5)
{
    System.out.print("A");
}
else
{
    System.out.print("B");
}
A

Only one branch can run.

只能执行其中一条分支。

This difference is important in AP output-tracing questions.

这个区别是 AP 输出追踪题中的常见考点。

Structure How Java checks it Possible number of executed branches
Two separate if statements
两个独立的 if
Checks both conditions independently.
分别检查两个条件。
0, 1, or 2
One if-else statement
一个 if-else
Chooses one branch.
选择其中一条分支。
Exactly 1

5. Conditions Must Produce a Boolean Value
条件必须产生布尔值

The expression inside the parentheses must evaluate to true or false.

括号中的表达式必须得到 truefalse

A condition may use a relational operator:

条件可以使用关系运算符:

if (age >= 16)
{
    System.out.println("Eligible");
}

It may also use a Boolean variable directly:

也可以直接使用布尔变量:

boolean loggedIn = true;

if (loggedIn)
{
    System.out.println("Welcome");
}

Relational operators from Topic 2.2 commonly appear inside if conditions:

2.2 中学习的关系运算符经常出现在 if 条件中:

Operator Meaning
== equal to
等于
!= not equal to
不等于
< less than
小于
> greater than
大于
<= less than or equal to
小于或等于
>= greater than or equal to
大于或等于

Remember:

记住:

x = 5;     // assignment: changes x
x == 5     // comparison: produces true or false

6. Braces Control the Body
大括号决定代码块范围

Curly braces group the statements controlled by an if.

大括号用来标记哪些语句受到 if 条件的控制。

if (temperature < 10)
{
    System.out.println("Wear a coat.");
    System.out.println("Wear gloves.");
}

System.out.println("Leave home.");

When the condition is false, both statements inside the braces are skipped.

条件为假时,大括号中的两条语句都会被跳过。

"Leave home." is outside the braces, so it always runs.

"Leave home." 位于大括号外,因此总会执行。

Java uses braces—not indentation—to determine the body.

Java 根据大括号判断代码块,而不是根据缩进。

The AP exam normally shows braces even when the body contains only one statement. Using braces also makes the controlled block easier to trace.

AP 考试通常会使用大括号,即使代码块中只有一条语句。使用大括号也能让执行范围更加清楚。

7. Do Not Put a Semicolon After the Condition
条件后面不要加分号

Consider this code:

观察下面的代码:

int score = 40;

if (score >= 60);
{
    System.out.println("Pass");
}

This code prints:

这段代码会输出:

Pass

The semicolon immediately ends the if statement:

条件后的分号立刻结束了 if 语句:

if (score >= 60);

Java treats that as an if statement with an empty body. The following block is separate and always runs.

Java 会把它理解为一个代码块为空的 if 语句。后面的大括号代码块与 if 无关,因此总会执行。

This code compiles, but its behavior is wrong. It contains a logic error.

代码可以通过编译,但运行结果不符合预期。这属于逻辑错误

Correct code

int score = 40;

if (score >= 60)
{
    System.out.println("Pass");
}

No semicolon belongs after the Boolean expression.

布尔表达式后面不能加分号。

8. Test Both Possible Paths
测试两种可能的执行路径

Suppose this code checks an age requirement:

假设下面的代码检查年龄要求:

int age = 16;

if (age >= 16)
{
    System.out.println("Eligible");
}
else
{
    System.out.println("Not eligible");
}

A useful test should include:

测试时至少应该包括:

Test value Condition Expected branch
16 16 >= 16true if
15 15 >= 16false else

Testing 16 is especially important because it is the boundary value used by the condition.

测试 16 特别重要,因为它是条件中的边界值

For an if-else statement, use at least:

对于一个 if-else 语句,至少应该测试:

For conditions containing <= or >=, also check the exact boundary.

如果条件中包含 <=>=,还应该检查边界值本身。

9. Common Beginner Mistakes
常见初学者错误

Mistake What happens Correct understanding
Using = instead of ==.
= 当成 ==
Assignment is performed instead of equality testing.
执行了赋值,而不是相等比较。
Use == to compare values.
使用 == 判断值是否相等。
Adding ; after the condition.
在条件后添加分号。
The controlled body becomes empty.
if 控制的代码块变为空。
Write if (condition) with no semicolon.
条件后不要加分号。
Believing indentation controls the body.
认为缩进决定代码块。
Java follows braces, not visual spacing.
Java 根据大括号而不是缩进执行。
Check the exact positions of { and }.
检查 {} 的位置。
Treating two if statements as if-else.
把两个 if 当作 if-else
Both conditions may be true and both blocks may run.
两个条件可能都为真。
Trace every independent if separately.
分别追踪每一个独立的 if
Using the original variable value throughout.
始终使用变量初始值。
A later condition may be evaluated incorrectly.
后面的条件可能判断错误。
Update the value immediately after an assignment.
赋值后立刻更新变量值。
Thinking a false condition ends the program.
认为条件为假时程序结束。
Only the controlled body is skipped.
只会跳过该条件控制的代码块。
Continue with the statement after the selection.
继续执行选择结构后面的语句。

10. Debugging Example
调试例子

The following code is supposed to print both messages only when isCold is true:

下面的代码原本应该只在 isCold 为真时输出两条消息:

boolean isCold = false;

if (isCold = true);
{
    System.out.println("Wear a coat.");
    System.out.println("Wear gloves.");
}

However, both messages are printed.

但是,两条消息仍然会被输出。

There are two problems:

这里有两个问题:

  1. isCold = true assigns true instead of testing the variable. isCold = true 执行了赋值,而不是判断。

  2. The semicolon ends the if statement before the block. 分号在代码块开始前就结束了 if 语句。

Fixed code

boolean isCold = false;

if (isCold)
{
    System.out.println("Wear a coat.");
    System.out.println("Wear gloves.");
}

Because isCold is false, nothing is printed.

因为 isCold 为假,所以不会输出任何内容。

Bug Why it is wrong Error type
isCold = true Changes the variable to true instead of checking it.
把变量改成了 true,而不是判断它。
Logic error
逻辑错误
if (isCold); Creates an empty if body.
创建了一个空的 if 代码块。
Logic error
逻辑错误

11. Mini Practice
小练习

Practice 1: Output Tracing
练习一:输出追踪

What is printed?

下面的代码会输出什么?

int x = 3;

if (x > 2)
{
    x *= 2;
}

if (x > 4)
{
    x = 0;
}

System.out.println(x);

Answer:

0

The first condition changes x from 3 to 6. The second condition then uses 6, so it is also true.

第一个条件把 x3 改成 6。第二个条件使用更新后的 6,因此也为真。


Practice 2: One-Way Selection
练习二:单向选择

What is printed?

下面的代码会输出什么?

boolean ready = false;

if (ready)
{
    System.out.println("Start");
}

System.out.println("Wait");

Answer:

Wait

The if body is skipped, but the statement after it still runs.

if 代码块被跳过,但后面的语句仍然会执行。


Practice 3: Two-Way Selection
练习三:双向选择

What is printed?

下面的代码会输出什么?

int number = 7;

if (number % 2 == 0)
{
    System.out.println("Even");
}
else
{
    System.out.println("Odd");
}

Answer:

Odd

7 % 2 is 1, so the condition is false and the else branch runs.

7 % 2 的结果是 1,因此条件为假,执行 else 分支。


Practice 4: Find the AP Trap
练习四:找出 AP 陷阱

int x = 80;

if (x >= 80)
{
    System.out.println("High");
}

if (x >= 50)
{
    System.out.println("Middle");
}
else
{
    System.out.println("Low");
}

What is printed?

输出是什么?

Answer:

High
Middle

The two if statements are independent. The else belongs only to the second if.

两个 if 语句彼此独立。else 只与第二个 if 组成双向选择。

When x is 80, both x >= 80 and x >= 50 are true.

x80 时,两个条件都为真。


Practice 5: Fix the Code
练习五:修改代码

What is wrong with this statement?

下面的语句有什么问题?

if (score >= 60);
{
    System.out.println("Pass");
}

Answer: Remove the semicolon.

答案:删除分号。

if (score >= 60)
{
    System.out.println("Pass");
}

With the semicolon present, "Pass" is printed regardless of the value of score.

存在分号时,无论 score 的值是多少,都会输出 "Pass"

Quick Checklist
快速检查清单

Before answering a Topic 2.3 question, check:

做 2.3 的题目之前,检查: