0% found this document useful (0 votes)
2 views3 pages

Conditional Statements

The document explains the importance of conditional statements in programming for decision-making and dynamic logic. It differentiates between 'if' and 'switch' statements, provides examples of 'if-else' and 'elif' constructs, and discusses nesting conditional statements. Additionally, it includes scenario-based questions demonstrating the application of conditional statements in various programming contexts.

Uploaded by

Abhay Bhardwaj
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Conditional Statements

The document explains the importance of conditional statements in programming for decision-making and dynamic logic. It differentiates between 'if' and 'switch' statements, provides examples of 'if-else' and 'elif' constructs, and discusses nesting conditional statements. Additionally, it includes scenario-based questions demonstrating the application of conditional statements in various programming contexts.

Uploaded by

Abhay Bhardwaj
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1. What is a conditional statement in programming, and why is it important in writing code?

A
conditional statement is essentially a way to let a program make decisions based on certain
criteria. Without them, code would just run in a straight line from start to finish. They are important
because they allow us to create dynamic logic—like showing a "Login" button if a user is out, or a
"Logout" button if they are already in.

2. Explain the difference between an "if" statement and a "switch" statement. When would you
use one over the other?

 "if" statement: Best for complex logic, checking ranges (like score > 90), or evaluating
multiple different variables at once.

 "switch" statement: Best for "multi-way" branching where you are comparing a single
variable against many specific constant values. I’d use an if for something like checking if
a person is old enough to drive AND has a license, but I’d use a switch for a menu where
the user picks option 1, 2, or 3.

3. How do you write an "if-else" statement in your preferred programming language, and what is
its purpose? In Python, the purpose of an if-else is to provide two clear paths: one for when a
condition is true, and a "fallback" path for when it’s false.

Python

age = 18

if age >= 18:

print("You can vote.")

else:

print("Too young to vote.")

4. What is the significance of the "else if" or "elif" construct, and how is it used in conditional
statements? The elif (short for "else if") lets you check multiple conditions in a sequence. It’s
significant because it stops the program from checking further conditions once it finds a true one,
which is much more efficient than writing ten separate if statements.

5. Describe the concept of nesting conditional statements and provide an example of when and
how it can be useful. Nesting is just putting one if statement inside another one. It’s useful when a
second condition only matters if the first one is already true. Example: You check if a user is
logged in. If they are, you then check if they are an Admin. You don't need to check if they are an
Admin if they aren't even logged in yet.

Scenario-Based Questions

6. You are developing a feature that assigns a priority level to customer support tickets based on
their urgency and importance. How would you use a conditional statement to determine the
priority of a ticket? I would use an if-elif-else block to categorize the ticket based on a score or a
string value.

Python

urgency = "high"

if urgency == "high":

priority = "Level 1 (Immediate)"

elif urgency == "medium":

priority = "Level 2 (24 hours)"

else:

priority = "Level 3 (48 hours)"

7. In a web application, you need to check if a user is logged in and if they have the necessary
permissions to access a certain page. How would you use logical operators (AND, OR, NOT) to
perform this check? I’d use the and operator to ensure both requirements are met simultaneously.

Python

if is_logged_in and has_permission:

show_page()

else:

print("Access Denied")

8. You are implementing a system that awards points based on a user's actions (e.g., signing up,
making a purchase, sharing on social media). How would you use an "if-else" statement to award
points for specific actions? I would check the action type and increment the points variable
accordingly.

Python

if action == "signup":

points += 50

elif action == "purchase":

points += 100

elif action == "share":

points += 20

else:
points += 0

9. In a game development scenario, you need to determine whether a player's score qualifies for
a bonus. The bonus is based on certain conditions, such as a high score and completion of a
specific achievement. How would you use nested conditional statements to evaluate if the player
qualifies for the bonus? I’d use nesting to check the score first, and then check the achievement
status only if the score is high enough.

Python

if score > 1000:

if achievement_unlocked == True:

print("Bonus Awarded!")

else:

print("High score! But you need the achievement.")

else:

print("Keep playing!")

10. You are building a quiz application, and the answers are evaluated using a switch statement.
How would you implement the switch statement to evaluate different possible answers and
provide feedback accordingly? In modern Python (3.10+), we use match-case for this. It’s cleaner
than multiple if statements for checking specific answers.

Python

user_answer = "B"

match user_answer:

case "A":

print("Incorrect. Try again!")

case "B":

print("Correct! Well done.")

case "C":

print("Incorrect. That's a common mistake.")

case _:

print("Invalid option selected.")

You might also like