Statements
A block is a sequence of declarations and statements within curly braces. Blocks can be nested into each other.
Variable declarations
Any function variable must be described before it can be used. A variable declaration creates one or more variables and assign to each an initial value. A variable can be defined in any block. The scope of the variable extends to the block in which it is defined and to all nested blocks. You cannot create variables with the name of existing functions and "visible" variables. Also, the variable name must contain at least one lowercase letter, because uppercase names are used for constants. A variable declaration begins by specifying its type. There are two types of initialization - you can define a single variable with a value assigned to it or several variables of the same type with default initialization.
You can define the optional parameters-variables by specifying ? after the variable type. If you initialize the optional parameter using the assignment operator = or &=, it will be the default value if the parameter is not defined when the function is called. In order to pass an optional parameter when calling the function, it is necessary to specify the variable name and its value separated by a colon. Optional parameters are specified after the obligatory parameters.
If statement
The "if" statements begin with "if", they can have one or more "elif" blocks and end with "else". The statement sequentially evaluates the condition for each branch and if the condition is true, then the corresponding block executes. In this case, the remaining branches are skipped, the statement ends and the control is passed to the next command. If all branch conditions are false, then, if present, the "else" branch is executed.
While statement
The "while" statement is a simple loop. It specifies the repeated execution of a block as long as a boolean condition evaluates to true. If the condition is false the first time through the loop, the statements inside the loop are never executed.
For statement
The "for" statement is used to iterate through all the elements of the specified object. The object must be of a type that supports indexing such as arr, map, str, buf, set, range of integers. For each entry for assigns iteration values to corresponding iteration variables if present and then executes the block. You must specify the name of the variable for iteration values and, optionally, the name of the variable for the index value. If you want to iterate through a range of integer numbers, use the syntax from..to as the object, where from and to are values of the integer type. This will iterate through all the numbers between from and to, including the extreme values. The initial value can be greater than the final value, in this case, the iteration value will be decreased.
Switch statement
The "switch" statement allows you to perform different operations in case an expression has different values. The switch keyword is followed by the initial expression that is calculated and stored as the switch value. The switch value can be of type int, float, char, str. Then you enumerate case statement in curly braces with all possible values and the source code that should be executed. One case can have several possible values separated with a comma in case of which it will be executed. After executing the case block with the matching value, the program goes to the statement coming after switch. The rest of case blocks are not checked.
If you want to execute some operations in case none of the case blocks is executed, insert the default construction at the end of switch. The default statement can appear only once and should come after all case statements.
Local functions
You can define the local functions within the func functions. Local functions can accept parameters and return values. Local functions do not support a variable number of arguments. To return from a local function, you must use the return statement. In local functions, you can access external variables and parameters that have been defined above.
Return statement
A "return" statement terminates the execution of the current function, and optionally provides a result value. If a function doesn't have a result type, a "return" statement must not specify any result value. You can use "return" statement in any nested block.
Break statement
The "break" statement terminates switch/case statement or the execution of the loop (for and while). break is likely to be located within nested blocks. If a program contains several nested loops, break will exit the current loop.
Continue statements
The "continue" statement may occur within loops (for and while) and attempts to transfer control to the loop expression, which is used to increment or decrement the counter (for for or to the conditional expression for while; moreover, the execution of the loop body is not completely executed. The instruction executes only the most tightly enclosing loop, if this loop is nested.
Last updated