Condition and Branching
Condition and Branching
Objective:
1. Comparison operators
2. Branching
3. Logical operators
1. Comparison operations
Comparison operations are essential in programming. They help compare values and make decisions based
on the results.
Equality operator
The equality operator == checks if two values are equal. For example, in Python:
1. 1
2. 2
3. 3
1. age = 25
2. if age == 25:
3. print("You are 25 years old.")
Copied!
Here, the code checks if the variable age is equal to 25 and prints a message accordingly.
Inequality operator
1. if age != 30:
2. print("You are not 30 years old.")
Copied!
Here, the code checks if the variable age is not equal to 30 and prints a message accordingly.
1. if age>= 20:
2. Print("Yes, the Age is greater than 20")
Copied!
Here, the code checks if the variable age is greater than equal to 30 and prints a message accordingly.
2. Branching
Branching is like making decisions in your program based on conditions. Think of it as real-life choices.
The IF statement
Consider a real-life scenario of entering a bar. If you're above a certain age, you can enter; otherwise, you
cannot.
1. 1
2. 2
3. 3
4. 4
5. 5
1. age = 20
2. if age >= 21:
3. print("You can enter the bar.")
4. else:
5. print("Sorry, you cannot enter.")
Copied!
Here, you are using the if statement to make a decision based on the age variable.
Sometimes, there are multiple conditions to check. For example, if you're not old enough for the bar, you can
go to a movie instead.
1. 1
2. 2
3. 3
4. 4
5. 5
6. 6
Copied!
When a user interacts with an ATM, the software in the ATM can use branching to make decisions based on
the user's input. For example, if the user selects "Withdraw Cash" the ATM can branch into different
denominations of bills to dispense based on the amount requested.
1. 1
about:blank 2/4
07/03/2024, 00:26 about:blank
2. 2
3. 3
4. 4
5. 5
6. 6
7. 7
8. 8
9. 9
Copied!
3. Logical operators
Logical operators help combine and manipulate conditions.
In a smartphone's notification settings, you can use the NOT operator to control when to send notifications.
For example, you might only want to receive notifications when your phone is not in "Do Not Disturb" mode.
1. 1
2. 2
3. 3
1. is_do_not_disturb = True
2. if not is_do_not_disturb:
3. send_notification("New message received")
Copied!
In a secure facility, you can use the AND operator to check multiple conditions for access. To open a high-
security door, a person might need both a valid ID card and a matching fingerprint.
The AND operator checks if all required conditions are true, like needing both keys to open a safe.
1. 1
2. 2
3. 3
4. 4
1. has_valid_id_card = True
2. has_matching_fingerprint = True
3. if has_valid_id_card and has_matching_fingerprint:
4. open_high_security_door()
Copied!
about:blank 3/4
07/03/2024, 00:26 about:blank
The OR operator
When planning a movie night with friends, you can use the OR operator to decide on a movie genre. You'll
choose a movie if at least one person is interested.
The OR operator checks if at least one condition is true. It's like choosing between different movies to watch.
1. 1
2. 2
3. 3
4. 4
5. 5
1. friend1_likes_comedy = True
2. friend2_likes_action = False
3. friend3_likes_drama = False
4. if friend1_likes_comedy or friend2_likes_action or friend3_likes_drama:
5. choose a movie()
Copied!
Summary
In this reading, you delved into the most frequently used operator and the concept of conditional branching,
which encompasses the utilization of if statements and if-else statements.
Author
Akansha Yadav
about:blank 4/4