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!
Top comments (0)