Fiveable
Fiveable
pep
Fiveable
Fiveable

or

Log in

Find what you need to study


Light

3.3 If-Else Statements

2 min readdecember 19, 2022

Athena_Codes

Athena_Codes

Athena_Codes

Athena_Codes

If-Else Statements

We have just covered if statements, but what if we want something to be done if the condition that we are checking is false? This is where the else statement comes into play. The header for the else does not have a condition and is just "else" while the body is what is performed if the condition for the if statement is false.

Together, these create an , or a two-way selection. Here is the anatomy of an :

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 to place around all of the code that should run if the condition is true and around all of the code that should run if the condition is false. Without , the computer may get confused about what code it should run and do something you're not expecting. You can prevent a lot of headaches by remembering to add around each body and indenting the code inside!

Example: Number Rounding

Remember the rounding we learned in 1.5? Now with an , we can finally write a method that implements this as follows:

public static int round(double number) { if (number >= 0) { return (int) (number + 0.5); } else { return (int) (number - 0.5); } }

Key Terms to Review (2)

Brackets

: Brackets are symbols used in programming to enclose and group together elements, such as variables or expressions, within a statement. They are typically represented by the characters "[" and "]".

if-else statement

: An if-else statement is a programming construct that allows the program to make decisions based on certain conditions. It checks a condition and executes one block of code if the condition is true, and another block of code if the condition is false.

3.3 If-Else Statements

2 min readdecember 19, 2022

Athena_Codes

Athena_Codes

Athena_Codes

Athena_Codes

If-Else Statements

We have just covered if statements, but what if we want something to be done if the condition that we are checking is false? This is where the else statement comes into play. The header for the else does not have a condition and is just "else" while the body is what is performed if the condition for the if statement is false.

Together, these create an , or a two-way selection. Here is the anatomy of an :

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 to place around all of the code that should run if the condition is true and around all of the code that should run if the condition is false. Without , the computer may get confused about what code it should run and do something you're not expecting. You can prevent a lot of headaches by remembering to add around each body and indenting the code inside!

Example: Number Rounding

Remember the rounding we learned in 1.5? Now with an , we can finally write a method that implements this as follows:

public static int round(double number) { if (number >= 0) { return (int) (number + 0.5); } else { return (int) (number - 0.5); } }

Key Terms to Review (2)

Brackets

: Brackets are symbols used in programming to enclose and group together elements, such as variables or expressions, within a statement. They are typically represented by the characters "[" and "]".

if-else statement

: An if-else statement is a programming construct that allows the program to make decisions based on certain conditions. It checks a condition and executes one block of code if the condition is true, and another block of code if the condition is false.


© 2024 Fiveable Inc. All rights reserved.

AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.


© 2024 Fiveable Inc. All rights reserved.

AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.