Conditionals Statements

Table of contents
Understanding What Is Conditional means in programing language .
Let us see what a conditional statement does in a program. It simply creates two situations for any case or statement. For example, if you want to apply a filter during online shopping to show shirts only up to a maximum price of 1000 rupees, the conditional statement checks the price of the product. If the price is within the range, it shows the product (true), and the remaining products are hidden from you (false). Yes, you got it right; conditionals work on true and false.
What are Conditional Statements?
In simple terms, a conditional statement checks if something is true or false and then tells the program what to do based on that. Like in real life, if you are traveling to your friend's house and you get stuck at a junction, you ask your friend which direction to take, and they suggest one. Similarly, conditionals help a program decide which path to take, depending on the conditions you set..
Types Of Conditional Statement
If-else (in Hindi, we can call it Agar-Magar).
Let us understand with a real-life example, how conditional statements work. Imagine you are trying to decide if you should go outside for a walk. You might base your decision on the weather.
Here’s how it works:
Condition: If it’s sunny outside.
Action if True: You decide to go for a walk.
Action if False: You stay inside because it’s raining.
This decision-making process can be represented in a program using a conditional statement.
Basic Structure of Conditional Statements
The basic syntax of a conditional statement looks like this:
Copyif condition:
# action if the condition is true
else:
# action if the condition is false
Expanding with Multiple Conditions
In real life, we might have more complex decisions to make. For example, what if you also want to check if it’s too cold to go for a walk?
Here’s how you can expand the decision-making process:
Condition 1: If it’s sunny and not too cold, you’ll go for a walk.
Condition 2: If it’s sunny but too cold, you might decide to stay inside.
Condition 3: If it’s rainy, you’ll definitely stay inside.
Multiple Conditions in Code
Let’s add more conditions in JavaScript:
Copyweather = "sunny"
temperature = 10 # Temperature in Celsius
if weather == "sunny" and temperature >= 15:
console.log("You can go for a walk!")
else if weather == "sunny" and temperature < 15:
console.log("It's sunny, but too cold for a walk. Stay inside.")
else:
console.log("It's raining. Stay inside!")
First Condition: If the weather is "sunny" and the temperature is 15°C or higher, you go for a walk.
Second Condition (else if): If it’s sunny but too cold (less than 15°C), you stay inside.
Else: If it’s not sunny, and likely raining, you stay inside.
Types of Conditional Statements
If Statement: This is the simplest form of condition, checking if something is true.
Copy if x > 10: console.log("x is greater than 10")
If-Else Statement: This gives two possible outcomes—one if the condition is true, and another if it’s false.
Copylet x = 22 if x > 10: print("x is greater than 10") else: print("x is 10 or less")
Real-World Example in Programming
Imagine you are building a simple website for a bank where users can check their account balance. You want to display a message depending on the account balance:
If the balance is above $1000: You show a message saying "You are a premium customer!"
If the balance is between $500 and $1000: You show a message saying "You are a regular customer."
If the balance is below $500: You show a message saying "Your account balance is low."
Here’s how that might look in JavaScript:
Copy let balance = 700
if balance > 1000:
print("You are a premium customer!")
else if balance >= 500:
print("You are a regular customer.")
else:
print("Your account balance is low.")
Switch Case
What is Switch-case ?
Switch case is a second type of conditional statement used in programming languages. Now you might wonder why we use switch case when we already have if-else. The answer to this question relates to a real-life scenario. Imagine you go to a restaurant for lunch. I know you are a fitness freak, but let's assume this, okay? Now, suppose you have 200 thousand rupees, and you look at the menu to find two items whose total price is under or equal to 200. If nothing is listed around 200, then you just walk out of the restaurant, which is your default case.
In the same way, when a program has various cases for one expression, we use switch case.
The Switch-Case Structure
In most programming languages that support the switch-case statement, the structure looks like this:
Copyswitch (expression or variable) {
case value1:
// Code to run if variable matches value1
break;
case value2:
// Code to run if variable matches value2
break;
case value3:
// Code to run if variable matches value3
break;
default:
// Code to run if no case matches (optional)
}
Real-Life Example: Deciding What to Wear
Let’s say you’re deciding what to wear based on the day of the week. Here’s a real-world breakdown:
Monday: Wear a suit .
Tuesday: Wear casual.
Wednesday: Wear a suit again.
Thursday: Wear something sporty (for the gym).
Friday: Wear a T-shirt (casual day before the weekend).
Saturday and Sunday: Holiday( weekend vibes)
let's rewrite this in a switch-case statement to simplify things:
Copyday = "Monday"
switch(day):
case "Monday":
console.log("Wear a suit.")
break
case "Tuesday":
console.log("Wear something comfortable.")
break
case "Wednesday":
console.log("Wear a suit again.")
break
case "Thursday":
console.log("Wear something sporty.")
break
case "Friday":
console.log("Wear a T-shirt.")
break
case "Saturday":
case "Sunday":
console.log("Wear whatever you like.")
break
default:
console.log("Invalid day!")
Why Use Switch-Case?
Here’s why switch-case is useful:
Clean and Readable: It’s easier to read and follow compared to many
if-else-if
statements, especially when you have many conditions to check.Efficient: For large numbers of conditions, a switch-case is more efficient, as it avoids checking each condition one by one.
Flexible: Switch-case can be used to handle multiple different values for a single variable (like days of the week, menu options, or choices).
Subscribe to my newsletter
Read articles from ANKESH KUMAR SINGH directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
