Emojicode Documentation 0.8
Guides Language Reference Package Index

Control Flow

Emojicode provides different types of control flow statements that allow you to structure the flow of the program.

πŸ‡πŸ‰ Code Block

Code blocks are used in conjunction with control flow structures to group statements that will be executed only under if a condition is met or not met or that will be repeated.

Syntactic definition:

block ⟢ πŸ‡ statements πŸ‰
statements ⟢ statement statements | statement

Examples of blocks can be seen below.

β†ͺ️ If

The β†ͺ️ statement is very important. It allows for conditional execution of a code block. The whole syntax is:

if ⟢ β†ͺ️ condition block [else-ifs] [else]
else-ifs ⟢ else-if else-ifs | else-if
else-if ⟢ πŸ™…β€β†ͺ️ condition block
else ⟢ πŸ™…β€ block
condition ⟢ expression | assignment

If the condition evaluates to πŸ‘, the code block will be executed, and if it evaluates to πŸ‘Ž it'll be ignored.

This example will display β€œa is greater than” b if the content for variabe a* is greater than *b:

β†ͺ️ a ▢️ b πŸ‡
  πŸ˜€ πŸ”€a is greater thanπŸ”€β—οΈ
πŸ‰

πŸ™…

πŸ™… extends an β†ͺ️ statement to execute an additional code block in case the expression in the if statement evaluates to false. For example, the following code would display a is greater than b if β€œa is greater than b” and β€œa is not greater than b” otherwise:

β†ͺ️ a ▢️ b πŸ‡
  πŸ˜€ πŸ”€a is greater thanπŸ”€β—οΈ
πŸ‰
πŸ™… πŸ‡
  πŸ˜€ πŸ”€a is not greater than bπŸ”€β—οΈ
πŸ‰

The πŸ™… statements is only executed if the β†ͺ️ statement evaluated to false, and if all πŸ™…β†ͺ️ statements evaluated to false too.

πŸ™…β†ͺ️

πŸ™…β†ͺ️ extends an β†ͺ️ statement to execute different statements in case the original β†ͺ️ condition evaluates to πŸ‘Ž. However, unlike β†ͺ️, it will execute that alternative expressions only if the β†ͺ️ expression is πŸ‘. For example, the following code would display β€œa is greater than”, β€œa equal to b” or β€œa is smaller than b”:

β†ͺ️ a ▢️ b πŸ‡
  πŸ˜€ πŸ”€a is greater thanπŸ”€β—οΈ
πŸ‰
πŸ™…β†ͺ️ a πŸ™Œ b πŸ‡
  πŸ˜€ πŸ”€a equal to bπŸ”€β—οΈ
πŸ‰
πŸ™… πŸ‡
  πŸ˜€ πŸ”€a is smaller than bπŸ”€β—οΈ
πŸ‰

The πŸ™…β†ͺ️ statement is only executed if the preceding β†ͺ️ expression and any preceding πŸ™…β†ͺ️ expressions evaluated to πŸ‘Ž, and the current πŸ™…β†ͺ️ expression evaluated to πŸ‘.

πŸ”‚ For In

The πŸ”‚ statement allows you to quickly iterate over an instance, that is repeatedly retrieving values from it until there are no more values to provide. For example, you can iterate over an 🍨 instance and you’ll receive all elements contained in the list. The πŸ”‚ statement can iterate over instances of any type which conforms to the πŸ”‚πŸšElementπŸ† protocol.

Its syntax is:

for-in ⟢ πŸ”‚ variable expression block

The compiler then transforms the statement into byte code equivalent to the statement rewritten to

🍑 iterable❗️ ➑️ iterator
πŸ” πŸ”½ iterator❓️ πŸ‡
  πŸ”½ iterator❗️ ➑️ variable
  πŸ’­ The provided block is executed here
πŸ‰

where iteratable is the instance to iterate over (the result from evaluating the expression) and variable the variable name provided. Evidently, the variable will be of the type that was provided to the generic argument Element when the type of iterable declared its conformance to πŸ”‚πŸšElementπŸ†.

Let’s take a look at an example:

πŸ¨πŸ”€treeπŸ”€ πŸ”€beeπŸ”€ πŸ”€leeπŸ”€ πŸ”€meπŸ”€πŸ† ➑️ list

πŸ”‚ name list πŸ‡
  πŸ˜€ name❗️
πŸ‰

In this example, the code block will be repeated for every value of the list and the values tree, bee, lee, and me will be printed. The type of name is naturally πŸ”‘. That’s due to the fact that 🍨🐚Element declared its conformance to πŸ”‚ as πŸ”‚πŸšElementπŸ† and therefore also returns an iterator of type 🍑🐚ElementπŸ† from which the type of the variable is inferred.

Hint

If you need to repeat something for a given amount of times you should use ⏩ Ranges in combination with πŸ”‚.

πŸ” Repeat While

πŸ” repeats a given code block as long as condition is πŸ‘. This also means that if the condition is never πŸ‘ the code block will never be executed. The syntax is:

repeat-while ⟢ πŸ” condition block

For example, this program will infinitely print β€œIt goes on and on and on”.

πŸ” πŸ‘ πŸ‡
  πŸ˜€ πŸ”€It goes on and on and onπŸ”€β—οΈ
πŸ‰

Due to the ease of use of the πŸ”‚ statement πŸ” is only used very seldom.

← Previous Next Up: β€œThe s package” β†’
Something not quite right? Improve this page