diff --git a/doc/src/guide.rst b/doc/src/guide.rst index caacf29..a91bbbe 100644 --- a/doc/src/guide.rst +++ b/doc/src/guide.rst @@ -2,7 +2,10 @@ Guide ===== In this guide, we're going to cover the main CCM features. We'll first start by the base data types CCM provides, -as well as their associated operators. +as well as their associated operators. +Then we'll see variables and mutability, in particular mutable and immutable variable declaration. +Then, we'll describe flow control expressions, that is to say conditionnals (``if``) and +loops (``while`` and ``for``). Statements VS Expressions ------------------------- @@ -168,5 +171,91 @@ To access array elements, the operator ``[]`` can be used directly after the var var x = [2, 4, 6] x[1] # equals 4 -I +Flow control expressions +------------------------ +Conditions +~~~~~~~~~~ +A condition is an expression that allows us to execute blocks of code +based on the value of another expression. + +.. code-block:: + :caption: if expression + + if cond + x + y + z + end + +The optional ``else`` branch is executed only +if the condition after ``if`` is false. + +.. code-block:: + :caption: if-else expression + + if cond + x + else + y + end + +Conditionals can support an arbitrary number of test using multiples ``else if`` branches. +The first true ``else if`` branch is executed, otherwise the ``else`` branch is +called (if any). + +.. code-block:: + :caption: if-else_if-else expression + + if cond + x + else if cond2 + y + else + z + end + +While loops +~~~~~~~~~~~ +CCM has two kinds of loops: while loop and for loop. +The first one execute a block of code until a condition is not met. +The former iterate over an array or a string. + +.. code-block:: + :caption: a while loop + + var n = 1 + while n < 256 + n = n + 1 + end + + # here, n value is 256 + +For loops +~~~~~~~~~ + +``while`` loops are not always handy: iterating over a collection +force us to: declare an iterator value, to increment it and to dereference it +to access the collection item we want to manipulate. + +That is why ``for`` loops exist: it is a faster way for collection iterations. + +.. code-block:: + :caption: a for loop over an array + + var sum = 0 + for i in [2, 4, 6] + sum = sum + i + end + + # here sum value is 12 + +.. code-block:: + :caption: a for loop over a string + + var word = 0 + for i in "pizza" + word = i + word + end + + # here word value is "azzip" diff --git a/doc/src/index.rst b/doc/src/index.rst index 29b537c..f2c90c4 100644 --- a/doc/src/index.rst +++ b/doc/src/index.rst @@ -7,7 +7,6 @@ CCM Documentation .. toctree:: - :maxdepth: 2 installation guide