Comparison Operators ππΊ
Comparison operators are the ultimate decision-makers in programming. They let you compare values and decide which one is bigger, better, or just plain equal. From checking if two values are the same to finding out whoβs taller in a room full of shorties, comparison operators have got you covered. Letβs explore their quirks with some spicy examples that will bring your pseudo-code and Python skills to life. π
What are Comparison Operators?β
Hereβs the cast of characters in the world of comparison:
=or==: Equal to.<>or!=: Not equal to.>: Greater than.<: Less than.>=: Greater than or equal to.<=: Less than or equal to.
Examples with Comparison Operatorsβ
Example 1: Whoβs Got the Bigger Pizza Slice? πβ
Pseudo Code:
DECLARE MySlice, FriendSlice : INTEGER
MySlice β 8
FriendSlice β 6
IF MySlice > FriendSlice THEN
OUTPUT "My slice is bigger! π"
ELSE
OUTPUT "Friend's slice is bigger... unfair! π’"
ENDIF
Python:
MySlice = 8
FriendSlice = 6
if MySlice > FriendSlice:
print("My slice is bigger! π")
else:
print("Friend's slice is bigger... unfair! π’")
Output:
My slice is bigger! π
"Always grab the bigger slice, folks!" π΄
Example 2: Is Your Crush Single? πβ
Pseudo Code:
DECLARE RelationshipStatus : STRING
RelationshipStatus β "Single"
IF RelationshipStatus = "Single" THEN
OUTPUT "Youβve got a chance! π₯°"
ELSE
OUTPUT "Move on, mate. π"
ENDIF
Python:
RelationshipStatus = "Single"
if RelationshipStatus == "Single":
print("Youβve got a chance! π₯°")
else:
print("Move on, mate. π")
Output:
Youβve got a chance! π₯°
"Keep the charm, but respect the boundaries!" π
Example 3: The Mysterious Age Game πβ
Pseudo Code:
DECLARE Age : INTEGER
Age β 18
IF Age >= 18 THEN
OUTPUT "Welcome to adulthood! π"
ELSE
OUTPUT "Still a kiddo! π"
ENDIF
Python:
Age = 18
if Age >= 18:
print("Welcome to adulthood! π")
else:
print("Still a kiddo! π")
Output:
Welcome to adulthood! π
"Age is just a number, but donβt tell that to the bouncer!" π₯
Example 4: Bank Balance Check π°β
Pseudo Code:
DECLARE MyBalance : REAL
MyBalance β 100.50
IF MyBalance < 0 THEN
OUTPUT "You're broke, mate. π"
ELSE
OUTPUT "Living the rich life! πΈ"
ENDIF
Python:
MyBalance = 100.50
if MyBalance < 0:
print("You're broke, mate. π")
else:
print("Living the rich life! πΈ")
Output:
Living the rich life! πΈ
"Savings: the ultimate glow-up!" π
Example 5: Who Gets the Last Chocolate? π«β
Pseudo Code:
DECLARE YourChocolate, MyChocolate : INTEGER
YourChocolate β 2
MyChocolate β 2
IF YourChocolate = MyChocolate THEN
OUTPUT "Letβs share the last one! π"
ELSE
OUTPUT "It's mine! π€"
ENDIF
Python:
YourChocolate = 2
MyChocolate = 2
if YourChocolate == MyChocolate:
print("Letβs share the last one! π")
else:
print("It's mine! π€")
Output:
Letβs share the last one! π
"Sharing is caringβ¦ sometimes." π¬
Example 6: Height Comparison at the Club πΊπβ
Pseudo Code:
DECLARE MyHeight, MinHeight : INTEGER
MyHeight β 170
MinHeight β 165
IF MyHeight >= MinHeight THEN
OUTPUT "Youβre tall enough to enter! πΊ"
ELSE
OUTPUT "Sorry, not tall enough. π"
ENDIF
Python:
MyHeight = 170
MinHeight = 165
if MyHeight >= MinHeight:
print("Youβre tall enough to enter! πΊ")
else:
print("Sorry, not tall enough. π")
Output:
Youβre tall enough to enter! πΊ
"Wear heels next time if youβre borderline!" π
Example 7: The Better Exam Score πβ
Pseudo Code:
DECLARE MyScore, FriendScore : INTEGER
MyScore β 85
FriendScore β 90
IF MyScore > FriendScore THEN
OUTPUT "I aced it! π"
ELSEIF MyScore = FriendScore THEN
OUTPUT "We both nailed it! π―"
ELSE
OUTPUT "Better luck next time. π
"
ENDIF
Python:
MyScore = 85
FriendScore = 90
if MyScore > FriendScore:
print("I aced it! π")
elif MyScore == FriendScore:
print("We both nailed it! π―")
else:
print("Better luck next time. π
")
Output:
Better luck next time. π
"Itβs not about winning; itβs about pretending you didnβt care!" π
Example 8: Late Night Pizza Orders πβ
Pseudo Code:
DECLARE PizzaCount : INTEGER
PizzaCount β 5
IF PizzaCount <> 0 THEN
OUTPUT "Thereβs still pizza left! π"
ELSE
OUTPUT "All gone. π"
ENDIF
Python:
PizzaCount = 5
if PizzaCount != 0:
print("Thereβs still pizza left! π")
else:
print("All gone. π")
Output:
Thereβs still pizza left! π
"Pizza is eternalβ¦ until itβs not." π΄
Final Thoughts on Comparison Operators πβ
Comparison operators arenβt just mathβtheyβre about understanding lifeβs little truths. Whether youβre comparing your slice of pizza to your friendβs or checking if youβre tall enough to enter the club, these operators have your back. Play around with these examples and become the ultimate logic master! π