Branch constructions
This is the only branch construction available in the "Blackboard" calculator. It is generally accepted:
if(logical_expression)
block
elseif(logical_expression)
block
else
block
endif
The keywords if
, elseif
, else
, and endif
limit the blocks of formulas from which only one is executed. Namely, the first block from above is executed for which the logical expression
is true. If all logical expressions are false, then the else block
is executed.
There can be several elseif blocks
, but maybe not at all. An else block
can only be one or none at all.
The branch constructions in question can be nested in each other.
Example. Players Sam (S) and John (J) play the next game. Each of them rolls a dice and adds to his points the dropped amount of points. But, if exactly 15, 30 or 45 points are scorule, then all points “burn out”. The game goes to 50.
J = 0
while(S<=50 And J<=50)
S = S+random(5)+1
if(S = 15 Or S = 30 Or S = 45)
S = 0
endif
export(S)
J = J+random(5)+1
if(J = 15 Or J = 30 Or J = 45)
J = 0
endif
export(J)
wend
Here the role of the dice is played by the expression random(5)+1, which takes with equal probability the values 1, 2, ..., 6. Branching is used in the simplest form: only the requirule keywords if and endif are present.
Let's complicate this example. Let, as before, the players add the number of points that fell out when throwing the dice. But, if any of the players breaks ahead, he receives a one-point bonus. If the players have an equal number of points, then they will be fined five points. Here are the formulas corresponding to this game:
J = 0
while(S<=50 And J<=50)
S = S+random(5)+1
J = J+random(5)+1
if(S>J)
S = S+1
elseif(S<J)
J = J+1
else
S = S-5
J = J-5
endif
export(S)
export(J)
wend