Fiveable
Fiveable
pep
Fiveable
Fiveable

or

Log in

Find what you need to study


Light

3.2 If Statements and Control Flow

2 min readdecember 19, 2022

Athena_Codes

Athena_Codes

Milo Chang

Milo Chang

Athena_Codes

Athena_Codes

Milo Chang

Milo Chang

Introduction to Conditional Statements

Sometimes, we want our code to perform an action only if a certain condition is met (e.g., the integer is even), while other conditions will result in different actions. This requires us to use a conditional statement. These usually consist of a conditional statement header with the condition in it and a body that includes the code that is run if the conditional statement header is true.

If Statements

The simplest conditional statement is the if statement, also called a one-way selection. The if statement occurs if a block of code is only run if the condition is true. Here is the anatomy of an if statement:

some code before the if statement if (condition) { do this only if condition is true } do this after regardless of whether the condition was true or not

For the if condition, the convention is to keep the condition in parentheses in order to group the condition together and provide clarity. It is also a convention to keep the body of the if statement between brackets and indent the code in the body to show what statements are run. The brackets are essential. If the body has multiple lines but brackets are not placed around all of the lines, the computer will assume that the body consists only of the first line.

Example: Even Number Checker

Here, we are going to write a method to halve a number if it is even, but leave it unchanged if it is not.

public static int numberHalver(int number) { if (number % 2 == 0) { number /= 2; } return number; }

We can also write a method to check if a number is even as well, which will lead to an important fact about return statements.

public static boolean isEven(int number) { if (number % 2 == 0) { return true; } return false; }

Remember we said that the code following the conditional statement is run no matter what the condition evaluates to in the anatomy of an if statement? This is the one exception to that. If there is a return statement inside the conditional body, the method ends with that return value and never gets to the last part of the program. This is why we can put return false; after the conditional statement. False will only be returned if the conditional wasn't true (i.e., the number wasn't even) so the code inside the conditional body was not run (i.e., true was not returned).

Key Terms to Review (5)

Block of Code

: A block of code refers to a group of statements that are grouped together and treated as a single unit. It is often used to organize and control the flow of execution in a program.

Conditional Statement

: A conditional statement is a programming construct that allows the execution of certain code blocks based on whether a specific condition is true or false.

Indentation

: Indentation refers to adding spaces or tabs at the beginning of lines of code to visually organize and structure it. In Python, indentation plays an important role in defining blocks of code within control structures like loops and conditionals.

Method

: A method is a named sequence of instructions that can be called or invoked to perform a specific task or action. Methods are used for code reusability, organization, and abstraction.

Return Statement

: A return statement is used in functions/methods to specify what value should be sent back as output when the function is called. It terminates the execution of a function and returns control back to where it was called from.

3.2 If Statements and Control Flow

2 min readdecember 19, 2022

Athena_Codes

Athena_Codes

Milo Chang

Milo Chang

Athena_Codes

Athena_Codes

Milo Chang

Milo Chang

Introduction to Conditional Statements

Sometimes, we want our code to perform an action only if a certain condition is met (e.g., the integer is even), while other conditions will result in different actions. This requires us to use a conditional statement. These usually consist of a conditional statement header with the condition in it and a body that includes the code that is run if the conditional statement header is true.

If Statements

The simplest conditional statement is the if statement, also called a one-way selection. The if statement occurs if a block of code is only run if the condition is true. Here is the anatomy of an if statement:

some code before the if statement if (condition) { do this only if condition is true } do this after regardless of whether the condition was true or not

For the if condition, the convention is to keep the condition in parentheses in order to group the condition together and provide clarity. It is also a convention to keep the body of the if statement between brackets and indent the code in the body to show what statements are run. The brackets are essential. If the body has multiple lines but brackets are not placed around all of the lines, the computer will assume that the body consists only of the first line.

Example: Even Number Checker

Here, we are going to write a method to halve a number if it is even, but leave it unchanged if it is not.

public static int numberHalver(int number) { if (number % 2 == 0) { number /= 2; } return number; }

We can also write a method to check if a number is even as well, which will lead to an important fact about return statements.

public static boolean isEven(int number) { if (number % 2 == 0) { return true; } return false; }

Remember we said that the code following the conditional statement is run no matter what the condition evaluates to in the anatomy of an if statement? This is the one exception to that. If there is a return statement inside the conditional body, the method ends with that return value and never gets to the last part of the program. This is why we can put return false; after the conditional statement. False will only be returned if the conditional wasn't true (i.e., the number wasn't even) so the code inside the conditional body was not run (i.e., true was not returned).

Key Terms to Review (5)

Block of Code

: A block of code refers to a group of statements that are grouped together and treated as a single unit. It is often used to organize and control the flow of execution in a program.

Conditional Statement

: A conditional statement is a programming construct that allows the execution of certain code blocks based on whether a specific condition is true or false.

Indentation

: Indentation refers to adding spaces or tabs at the beginning of lines of code to visually organize and structure it. In Python, indentation plays an important role in defining blocks of code within control structures like loops and conditionals.

Method

: A method is a named sequence of instructions that can be called or invoked to perform a specific task or action. Methods are used for code reusability, organization, and abstraction.

Return Statement

: A return statement is used in functions/methods to specify what value should be sent back as output when the function is called. It terminates the execution of a function and returns control back to where it was called from.


© 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.