In this article, we will look at conditional operators and cycles, as well as conduct several small experiments on their use.
')
The above operators in Perl 6 must get blocks for execution: even if only one expression is to be executed, it must be passed inside the block: {expression; }
Conditional statements One of the conditional statements is if, its general form looks like:
if () {} elsif (1) {1} elsif (N) {N} else {}
A block stored in a scalar variable cannot be transferred, but you can send instructions on how to execute a saved block: {$block();} Parentheses around the terms are optional. The following conditional statement is unless: it executes Block1 if condition1 is false, otherwise DrugBlock is executed
unless (1) {1} else {}
However, there is a construction in which it is possible to use not a block, but a single operator:
if ; unless ;
If you need to perform several simple actions for the current task and you want to write them in a similar form, you can specify them with the help of the construction
(1; 2;) if ;
If you write the following construction
my $var = ( if ($i==0 { 1+1; } )
then the last calculated expression will be entered into $ var, in this case if $ i is equal to zero, then the calculated value will be 2, if $ i is not equal to zero, then the calculated value will be Nil (of the same type as Nil) Another type of conditional statement is the select statement:
After the first successful comparison, the given operator stops its work: this means that Block3 will never be executed. The comparison goes in the order in which it is indicated: if you put after the default any or when then the comparison will never reach it, since after default, the statement execution will stop.
Cycles One of the statements in this group is while: it executes the specified block until the condition is met
$i = 0; while ($i<10) { $i++; }
This loop will iterate through all values from 0 to 10. Parentheses around the condition are optional. The following statement is until: it executes the specified block until the condition is true:
$i = 0; until ($i>=10) { $i++; }
This cycle will do the same as the previous one. As with the condition, the design is possible
say $a++ while $a<10; ($a++; say $a) until $a>9;
The following loop statement is
loop (; ; ) {}
In the Initial Conditions block, the initial values of the variables are specified, the Termination Condition specifies an expression that will determine whether to start the next iteration of the loop or not, the Cycle Variable Variation contains expressions that will be executed at the end of each iteration.
The for loop operator is used to iterate through all the elements of an array or the list passed to it.
for @mas, @mas2, 'string'20, True -> $elemList {}
At each iteration, the list item can be accessed via the $ elemOfMas variable, which is an alias of this item, but you cannot change the values of the list items through this variable. In order to be able to change the list items you need instead of '->' use '<->' For all loop operators, you can use the operators next (start the next iteration), redo (Return to the next iteration), last (Stop executing the loop). It is possible to start a loop with a postcondition:
repeat {} while ();
and
repeat {} until ($x >= 10);
Also in the synopses it is stated that it will be possible to write a construction
repeatwhile () {};
however, in the latest version (12.09), this construct produced an error.