Emojicode๐ (Conditionals)
LeafKn
Posted on March 11, 2022
Wuhuuu.. here comes the last part of the Emojicode series.
In this part, I'll introduce you to conditionals and how to make more complex programs with Emojicode ๐ช.
Conditionals ๐โ
You are feeling hungry, what should you do? Well, eat! This is a conditional.
The condition here is - If in this world I ever feel hungry then I should get up and eat ๐ else I should continue doing stuff that I actually procrastinate ๐ช.
In Emojicode, Conditionals take an expression, which is code that evaluates to determine a value, and checks if it is ๐ (true) or ๐ (false).
Using Emojicode, we can add this decision making ability into our program using the โช๏ธ statement. โช๏ธ checks if the condition is ๐ or ๐ and if the condition evaluates to ๐, it runs a block of code.
โช๏ธ ๐ ๐
๐ ๐คHello World!๐คโ๏ธ
๐
๐ญ Result: Hello World
โช๏ธ ๐ ๐
๐ ๐คHello World!๐คโ๏ธ
๐
๐ญ Result: *Nothing is printed*
Now, what happens when your condition is not ๐. Like you have gone to your kitchen and you have nothing to eat. Oof, now you'll have to prepare yourself to go out and bring something to eat (hard work ๐ช).
Here comes ๐
, this executes code for the backup plan.
๐ โก๏ธ foodPresentInKitchen
โช๏ธ foodPresentInKitchen ๐
๐ ๐คTake food, eat and live.๐คโ๏ธ
๐
๐
๐
๐ ๐คGo out and bring something to eat.๐คโ๏ธ
๐
Comparison Operators:
What about other data types, like ๐ค(strings) and ๐ฏ(numbers).
We use comparison operators.
One of them is ๐.
Expression | Evaluates to |
---|---|
6 ๐ 6 | ๐ |
๐คHello๐ค ๐ ๐คHello๐ค | ๐ |
2 ๐ 3 | ๐ |
๐คEmoji๐ค ๐ ๐คcode๐ค | ๐ |
Now, you are in the market and you go to a shop. You need a pizza and a coke bottle. You are so extreme lazy that you don't want to go to two different shops to buy them. You look for a shop selling both. This is how you'll work:
๐คYes๐ค โก๏ธ shopHasCoke
๐คYes๐ค โก๏ธ shopHasPizza
โช๏ธ shopHasCoke ๐ shopHasPizza ๐
๐ ๐คNo more hard work, get into the shop right away!๐คโ๏ธ
๐
๐
๐
๐ ๐คSadly, look for another shop ๐ญ๐คโ๏ธ
๐
๐ญ Prints "No more hard work, get into the shop right away!" because both have equal values that is "Yes"
Other comparison operators:
Operator | Operation |
---|---|
โ๏ธ | Less than |
โถ๏ธ | Greater than |
โ๏ธ๐ | Less than or equal to |
โถ๏ธ๐ | Greater than or equal to |
Now, for the sake of continuing this senseless story, you are unable to find any such shop ๐ญ. You can only get greens now.
Oof, you will need to chain one more conditional here that will execute if the above conditions are false but the new condition is true.
๐คNo๐ค โก๏ธ shopHasCoke
๐คNo๐ค โก๏ธ shopHasPizza
๐ โก๏ธ shopHasGreens
โช๏ธ shopHasCoke ๐ shopHasPizza ๐
๐ ๐คNo more hard work, get into the shop right away and eat happily.๐คโ๏ธ
๐
๐
โช๏ธ shopHasGreens ๐
๐ ๐คGet right into the shop and eat sadly.๐คโ๏ธ
๐
๐
๐
๐ ๐คFellow human, sleep hungry today ๐ด๐คโ๏ธ
๐
๐ญ Prints: Get right into the shop and eat sadly.
AND and OR (Logical Operators):
Sometimes we need to run a piece of code if all conditions provided are true but sometimes we need to run a piece of code even if one of the provided conditions is true.
You are lucky enough to manage to get greens. You are in your kitchen trying to create a salad. You need salt, greens(of-course) and a plate to serve in.
๐ โก๏ธ hasGreens
๐ โก๏ธ hasSalt
๐ โก๏ธ hasPlate
โช๏ธ hasGreens ๐ค hasSalt ๐ค hasPlate ๐
๐ ๐คLet's make a sal.. sal.. salad!๐คโ๏ธ
๐
๐
๐
๐ ๐คFirst, get all the stuff in front of you.๐คโ๏ธ
๐
๐ญ Prints: Let's make a sal.. sal.. salad!
So you have two plates. One is white and other is purple. You are unable to find one but anyway anyone will work. This is where we will use ๐ operator. This will execute the code once any of the condition evaluated to ๐.
๐ โก๏ธ hasWhite
๐ โก๏ธ hasPurple
โช๏ธ hasWhite ๐ hasPurple ๐
๐ ๐คLet's serve our salad.๐คโ๏ธ
๐
๐
๐
๐ ๐คAt-least go and find either plate.๐คโ๏ธ
๐
๐ญ Prints: Let's serve our salad.
Other stuff that is not covered in this series.
a. You can group expressions like this:
๐ค 3 โ๏ธ 2 ๐ค โ ๐ค 2 โ๏ธ 2 ๐ค
This is similar to: (3*2) + (2*2)
b. There are loops
๐ ๐
๐ญ Creating a list of text
๐ฟ๐คhello๐ค ๐คhi๐ค ๐คhowdy๐ค ๐คhiii๐ค๐ โก๏ธ list
๐ญ Looping through the list
๐ typeOfHello list ๐
๐ typeOfHelloโ๏ธ
๐
๐
๐ญ Prints:
๐ญ๐
hello
hi
howdy
hiii
๐๐ญ
etc..
Every syntax of Emojicode can be found in the official docs. To become an emojicode master, go to their official docs and play with emojis ๐ช.
Yay, we have completed the Emojicode series.
See you again!
Posted on March 11, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.