Conditional statements play a major role in programming because they allow programs to make decisions based on specific conditions. Therefore, these statements determine which parts of the code run and which parts are skipped. This concept is also called selection coding because it directs your program to choose a path depending on whether a condition is true or false.
Conditional statements are central to creating dynamic and efficient algorithms. However, they are not just restricted to coding languages. In fact, algorithms can use selection without requiring any particular programming syntax. As a result, learning about conditional statements will make problem-solving easier, regardless of the language or technology used.
What We Review
Introduction to Conditionals
Conditional statements help guide the flow of a program by executing different outcomes. In everyday life, decisions depend on whether certain conditions are satisfied. For example, a decision to take an umbrella depends on whether it is raining. In a similar way, selection in algorithms checks if the condition is true (it’s raining) before taking the corresponding action (grab the umbrella).
Selection involves Boolean expressions, which evaluate to either true or false. When the expression is true, one set of instructions runs. When the expression is false, a different set of instructions may run, or sometimes no further action occurs.
What Are Conditional Statements?
Definition
In programming, conditional statements (often called “if-statements”) allow a program to decide which steps to take next, based on whether a Boolean expression is true or false. A Boolean expression is any comparison or check that results in true or false. For instance, checking if a number is greater than five might be written as x > 5, which returns true when x is 6 but false when x is 4.
These conditional pathways add flexibility to an algorithm. Without them, programs would run the same steps regardless of what data they receive, which would limit their usefulness.
Types of Conditional Statements
- If Statements
- Executes a set of actions only if the condition is true.
- If-Else Statements
- Executes one set of actions if the condition is true and a different set if the condition is false.
- Else-If Statements
- Allows checking multiple conditions in sequence, running only the set of actions for the first true condition.

How Conditional Statements Work
The Logic Behind Conditionals
Conditional statements rely on Boolean expressions. A Boolean expression might be something like age >= 16 if deciding whether a person can drive. When the expression is evaluated, it gives true or false. If the expression is true, certain lines of code run. Otherwise, the code might skip those lines or run another set of lines.
This process is similar to a security screening at an airport: if your ticket matches your ID, you proceed to your gate. Otherwise, you’re sent to a special check. The gate assignments change based on whether the condition is satisfied.
Example of an If Statement
Consider this simple example:
IF(rainy = true)
{
DISPLAY("Take an umbrella!")
}
- The condition IF(rainy = true) checks if the variable rainy is true.
- If that condition is true, the program outputs “Take an umbrella!”.
- If the condition is false, no action is taken.
Therefore, the action only occurs if the condition evaluates to true.
Example of an If-Else Statement
Sometimes, you need a fallback for when the condition is false. In that case, an if-else statement is useful:
IF(isExamDay = true)
{
DISPLAY("Review study notes!")
}
ELSE
{
DISPLAY("Relax and play a game!")
{
- The condition (isExamDay = true) checks if it is exam day.
- If it’s true, the program prints “Review study notes!”.
- Otherwise, the program prints “Relax and play a game!”.
As a result, the code always runs exactly one of those two blocks, ensuring an appropriate outcome for each scenario.
Using Conditional Statements in Algorithms
Designing Algorithms with Selection
It is possible to design selection in algorithms without focusing on a specific programming language. For instance, a flowchart can represent how decision points guide the algorithm:
- Start at the first step.
- Reach a decision point by checking if a condition is true or false.
- If true, follow arrow A to the next block of instructions.
- If false, follow arrow B to the alternative block of instructions.
- Continue until the algorithm ends.
Flowcharts visually depict outcomes branching in different directions based on the condition. This technique helps programmers plan the step-by-step logic before coding.
Example Algorithm
Consider a simple decision-making algorithm for turning in homework:
- Read “Assignment Deadline” from the calendar.
- Ask: “Is today the deadline?”
- If yes (true), submit finished homework to the teacher.
- If no (false), review progress and keep working.
- End.
This selection coding concept is otherwise known as the “if and if else statements” approach. The critical component is the question “Is today the deadline?” If the answer is true, the homework is submitted. Otherwise, the next action is different. This approach is helpful for building more complex applications as well.
Practice Problems
Providing Scenarios
Try to create or express algorithms using conditional statements:
1. A recipe-check algorithm:
- If there are enough eggs and flour, bake the cake.
- Otherwise, go to the store to buy more ingredients.
2. A password check program:
- If the entered password matches the saved password, grant access.
- Else, show an “Access Denied” message.
- For fun, ask: What happens if you need to check multiple saved passwords?
3. A temperature-based system:
- If the temperature is below 32 degrees, display “Wear a heavy coat.”
- Else if the temperature is below 50, display “Grab a light jacket.”
- Else, display “No jacket needed.”
These practice problems let you see how conditions can trigger unique outcomes. Therefore, building a habit of writing out conditions in plain English first is often helpful.
Vocabulary Reference Chart
Use this list to quickly review essential terms about conditional statements:
- Algorithm – A step-by-step procedure to solve a problem or perform a computation
- Boolean – A data type that represents either true or false
- Conditional Statement (If-Statement) – A statement that allows a program to execute certain code only if a specified condition is true
- If-Else Statement – A conditional structure that executes one block of code if a condition is true and another block if the condition is false
- Else-If – An extension to an if-else that checks additional conditions after the initial if condition fails
- Selection – A concept in algorithms where some steps are taken only when a condition is met
Conclusion
Conditional statements are powerful tools for making decisions in code. Therefore, they give programs the ability to adapt and respond to different situations. This dynamic flow is critical to problem-solving in computer science. By understanding how selection works, it becomes easier to design flexible algorithms, either with or without a specific programming language.
In summary, conditional statements control the flow of an algorithm by testing conditions. When a condition is true, a specific path executes. When it is false, another path (or nothing) executes. This behavior is the heart of “if else statement” usage. It is also why selection coding is such an important concept in AP® Computer Science Principles.
Sharpen Your Skills for AP® Computer Science Principles
Are you preparing for the AP® Computer Science Principles test? We’ve got you covered! Try our review articles designed to help you confidently tackle real-world AP® Computer Science Principles questions. You’ll find everything you need to succeed, from quick tips to detailed strategies. Start exploring now!
- AP® Computer Science Principles 3.3 Review
- AP® Computer Science Principles 3.4 Review
- AP® Computer Science Principles 3.5 Review
Need help preparing for your AP® Computer Science Principles exam?
Albert has hundreds of AP® Computer Science Principles practice questions and full-length practice tests to try out.