Back to Boolean Expressions and if Statements

Lesson PreviewLesson 2 of 8 in this unit

3.2 if Statements

Part of Boolean Expressions and if Statements in AP® Computer Science A. Read the opening section, then create a free account for the full interactive lesson, practice, and progress tracking.

A conditional statement is like a decision-maker in a program: it checks if something is true or false and then either runs certain code or skips it based on that result. Once you understand boolean expressions, you can use them in if statements—a type of conditional statement—to make decisions in your code. An if statement in Java begins with the keyword if, followed by a pair of parentheses ( ). Inside the parentheses is a boolean condition—something that can be either true or false. Then, you have a pair of curly brackets ({ }) containing the code you want to run only if the condition is true. If the condition is false, the code inside the braces is skipped.

java
int score = 85;
if (score >= 70) {
    System.out.println("You passed the test!");
}

Output

You passed the test!

Continue the full lesson in Stellar

Create a free account to unlock the remaining sections, checkpoints, course practice, and progress tracking for AP® Computer Science A.