Emojicode Documentation 1.0 beta 2
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 decorator block [else-ifs] [else]
else-ifs ⟢ else-if else-ifs | else-if
else-if ⟢ πŸ™…β€β†ͺ️ condition decorator block
else ⟢ πŸ™…β€ decorator 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 variable a* is greater than *b:

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

πŸ™…

πŸ™… 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 bπŸ”€β—οΈ
πŸ‰
πŸ™… πŸ‡
  πŸ˜€ πŸ”€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 b”, β€œa is equal to b”, or β€œa is smaller than b”:

β†ͺ️ a ▢️ b πŸ‡
  πŸ˜€ πŸ”€a is greater than bπŸ”€β—οΈ
πŸ‰
πŸ™…β†ͺ️ a πŸ™Œ b πŸ‡
  πŸ˜€ πŸ”€a is 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.

Interlude: ⏩ Ranges

The s package provides a type ⏩, representing a range. A range is an immutable sequence of integers and is defined by three values: start, stop and step.

If step is positive, every number f(x) = start + x * step that matches the constraint start ≀ f(x) < stop is an element of the range. If step is negative the constraint stop < f(x) ≀ start applies instead.

Ranges are helpful in if you need to repeat something for a specific number of times:

πŸ”‚ i πŸ†•β© 0 10 2❗️ πŸ‡
  πŸ˜€ πŸ”‘ i❗️❗️  πŸ’­ Prints numbers 0 through 8 (including).
πŸ‰

πŸ”‚ i πŸ†•β© 0 10❗️ πŸ‡
  πŸ˜€ πŸ”‘ i❗️❗️  πŸ’­ Prints numbers 0 through 9 (including).
πŸ‰

πŸ”‚ i πŸ†•β© 10 0❗️ πŸ‡
  πŸ˜€ πŸ”‘ i❗️❗️  πŸ’­ Prints numbers 10 through 1 (including).
πŸ‰

πŸ”‚ i πŸ†•β© 100 -10 -10❗️ πŸ‡
  πŸ˜€ πŸ”‘ i❗️❗️  πŸ’­ Prints numbers 100 through 0 (including).
πŸ‰

πŸ” Repeat While

πŸ” repeats a given code block as long as a 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.

🎍🐌 🎍🏎 Branch Speed

Hint

Don’t bother adding these decorators. The effects are minimal and only useful in very specific cases. If used improperly they might even harm performance.

The decorators 🎍🐌 (slow) and 🎍🏎 (fast) allow you to specify to the compiler which branches in an β†ͺ️ will be slow or fast. This can enable better optimizations in performance sensitive code.

The example below shows an if statement whose only branch has been marked as slow:

β†ͺ️ size πŸ™Œ count πŸŽπŸŒπŸ‡
  size β¬…οΈβœ–οΈ 2
  ☣️ πŸ‡
    πŸ— data sizeβœ–οΈβš–οΈElement❗️
  πŸ‰
πŸ‰
← Previous Next Up: β€œClasses & Value Types” β†’
Something not quite right? Improve this page