Block Statements And Grouping

Blocking and grouping statements.

A Block statements are a list of statements that are executed sequentially. they can be helpful to execute a list of statement based on some condition. you can define a block using {}.

you can't access variables that are defined inside block scope outside of the block scope.

መለያ global_var = 5;
{
    መለያ local_var = global_var + 10; // i can access a global variable from inside a block scope the result of this statement will be 15.
}
global_var = local_var; // I cannot do this because local_var is out of scope

but you can access a local variable from another scope as long as that scope is defined at the same level scope or below, consider the following example

{
    መለያ local_var = 5;
    {
        local_var = local_var + 1; // 6
        {
            local_var = local_var + 1; // 7
        }
    }
}

The grouping ( ) operator controls the precedence of evaluation in expressions. It also acts as a container for arbitrary expressions in certain syntactic constructs, where ambiguity or syntax errors would otherwise occur.

consider the following example.

መለያ = 1;
መለያ = 2;
መለያ = 3;
 
// default precedence
አውጣ ሀ + ለ * ሐ; // will print 7
// evaluated by default like this
አውጣ ሀ + (ለ * ሐ); // will print 7
 
// now overriding precedence
// addition before multiplication
አውጣ (ሀ + ለ) * ሐ; // will print 9