DEV Community

Cover image for Emojicode😎 (Conditionals)
LeafKn
LeafKn

Posted on • Updated on

Emojicode😎 (Conditionals)

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
Enter fullscreen mode Exit fullscreen mode
β†ͺ️ πŸ‘Ž πŸ‡
  πŸ˜€ πŸ”€Hello World!πŸ”€β—οΈ
πŸ‰

πŸ’­ Result: *Nothing is printed*

Enter fullscreen mode Exit fullscreen mode

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.πŸ”€β—οΈ
πŸ‰
Enter fullscreen mode Exit fullscreen mode

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"

Enter fullscreen mode Exit fullscreen mode

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.

Enter fullscreen mode Exit fullscreen mode

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.

salad

πŸ‘ ➑️ 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!

Enter fullscreen mode Exit fullscreen mode

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.

Enter fullscreen mode Exit fullscreen mode

Other stuff that is not covered in this series.

a. You can group expressions like this:

🀜 3 βœ–οΈ 2 πŸ€› βž• 🀜 2 βœ–οΈ 2 πŸ€›
Enter fullscreen mode Exit fullscreen mode

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
πŸ”šπŸ’­

Enter fullscreen mode Exit fullscreen mode

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!

cya

Top comments (0)