This content originally appeared on DEV Community and was authored by Fahim ul Haq
Learning conditionals was an “aha” moment for me. I hope it’ll be the same for you.
This blog was originally published on Substack. Subscribe to ‘Letters to New Coders’ to receive free weekly posts.
Imagine this: It’s an autumn morning, and you’re about to leave the house. You pause at the door and think, “If it’s raining, I’ll grab my rain boots; otherwise, sneakers will do.”
That simple decision-making process mirrors how conditionals work in programming.
Learning conditionals was something of an “aha!” moment for me as a student. I felt like I had unlocked a new dimension in programming. Suddenly, my code could do more than follow a linear sequence — it came alive and could adjust to different inputs. That’s when I knew I could create things capable of solving complex, real-world problems. What a feeling!
Learning conditionals will do that for you, too. Conditionals are a fundamental part of computer programming that allow your code to respond dynamically to different situations. Instead of following fixed instructions, your code can change behavior depending on specific circumstances — making it more intelligent, flexible, and responsive.
Today, I’ll explore how conditionals can help you solve real-world challenges, using examples in Python. Then I’ll give you some next steps to start practicing your use of conditionals.
When is it time to learn conditionals?
So, when would you be ready to learn conditionals? Once you’ve gotten the hang of programming basics like sequential code, variables, data types, and handling input and output.
As you encounter more complex situations where your code needs to make decisions, conditionals are your next logical step.
Types of conditionals
Conditionals come in various forms, allowing you to:
- Specify actions if a condition is met
- Specify alternative actions if said condition isn’t met
- Specify actions if certain combinations of conditions are met
Conditionals check for a condition and evaluate as either True or False. Depending on that result, the program can run different blocks of code.
In Python, basic conditionals include:
- if: Checks if a condition is True, and runs if True
- elif: Checks for a second condition if previous one is False
- else: Executes if the previous if or elif are False
We can also use logical operators like and, or, and not to achieve combinations of conditions (but we won't cover that for today's introduction).
For example, you can see if , elif, and else working together below.
We can also use logical operators like and, or, and not to achieve combinations of conditions (but we won't cover that for today's introduction).
For example, you can see if , elif, and else working together below.
x=4
if x>4:
print("x is greater than 4")
elif x==4:
print("x is equal to 4")
else:
print("x is less than 4")
While these conditionals consist of two lines, as a whole they are referred to as blocks (e.g., “if block”). For statements like if and elif, the indented block will only run if the checked condition is True. The else block, on the other hand, runs only if all preceding conditions are False.
In this blog, we’ll focus on if and else. Let’s get started with a real-world example.
Coding for the real world
As the problems you solve become more complex, your code will have to adapt. Let’s look at an example where conditionals make a simple program capable of handling bigger questions.
Picture a ticketing kiosk at an event. First, we’ll explore how the program operates without conditionals, and then we’ll dig into how it changes once we add them.
In a basic sequential program, everyone is charged the same ticket price, regardless of any unique factors.
Here’s what that looks like in a flow chart:
In coding, that flow chart translates to three lines of code, all executed in order from line 1 to 3. Here’s what that code would look like in Python:
This sequence is pretty straightforward! But coding, like real life, is rarely that simple.
Sometimes, events charge customers a different price for their tickets. For example, prices are sometimes conditional upon the age of the attendee (e.g., children, adults, and seniors).
Enter: conditionals.
For the different ticket prices, conditionals will help our program decide ticket prices based on the customer’s age.
Here’s how it works:
- If the customer is older than 12, they’ll pay the full adult ticket price.
- Else, if the customer is 12 or younger, they’ll pay half the adult price.
Next, let’s update our ticket pricing program using these conditionals.
Here’s the updated program sequence:
- It checks if age > 12.
- If the condition is true, the if block runs.
- If the condition is false, the else block runs.
In a flow chart, that looks like this:
And in Python code, that translates to:
Each time this program runs, either the if condition or the else condition will be executed — but never both.
The do’s and don’ts of using conditionals
Getting comfortable with conditionals
Mastering conditionals will greatly expand the kinds of programs you can write — your whole brain will light up when they click and you realize that you’ve written flexible, intelligent code that can make a real-world difference.
Now that you understand what conditionals are and how they work, it’s time to get some practice in. A great way to work on what you’ve learned is to modify small programs and make them more dynamic.
Some beginner-friendly examples of conditionals you can build are:
- Using conditionals to check if a number is even or odd:
- Checking the temperature for weather advice (rain boots again!)
- A simple password checker
Conditionals are just the foundation to learn more advanced concepts later on in your learning journey. And remember, programming is all about practice and improvement.
For more in-depth learning, Educative’s Learn to Code resources are a great place to get hands-on with programming fundamentals like conditionals in various programming languages.
As always, happy learning!
- Fahim
This content originally appeared on DEV Community and was authored by Fahim ul Haq
Fahim ul Haq | Sciencx (2024-09-25T07:45:43+00:00) Conditionals 101: The building blocks of adaptive programs. Retrieved from https://www.scien.cx/2024/09/25/conditionals-101-the-building-blocks-of-adaptive-programs/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.