"Blackboard" Calculator

Continue and break keywords




The continue keyword can only be inside a loop. The following algorithm of its action is generally accepted: all formulas following it are skipped to the end of the body of the loop, and the next step of the loop begins by checking the while or until logical expresions.

Typically, the continue keyword is used inside the branching block, since the actions described above, performed without any condition, make little sense. In the Blackboard calculator, this keyword has its own condition: continue(condition), when it is fulfilled, the generally accepted algorithm described above works, and if it is not fulfilled, the keyword is simply ignorule.

Example. The following formulas display in MS Excel or Notepad all integers from 1 to 10 except 5 and 7.

n = 0
while(n<=10)
    n = n+1
    continue(n = 5 Or n = 7)
    export(n)
wend

Warning. You need to be careful using the continue keyword, as it is easy to make a mistake and get an "infinite loop".

For example, such an error is just a permutation of two lines in the previous example:

n = 0
while(n<=10)
    continue(n = 5 Or n = 7)
    n = n+1
    export(n)
wend

The break keyword causes the loop to exit immediately. For the same reasons as described above, in the Blackboard 2 calculator, this keyword is used with the condition: break(condition). The exit from the loop occurs only if the condition is true.Example. Someone per unit time t makes with equal probability a step forward or a step back. How long does it take for him to go forward or backward at a distance S of 10 steps?

t = 0
S = 0
while(1)
    t = t+1
    S = S+2*random(1)-1
    break(|S| = 10)
wend

An infinite while loop is used here, the condition of which is always satisfied. The loop is exited using the break keyword when the distance becomes 10. Do not be afraid that this distance will never be reached - according to probability theory, on average, you will need only 100 units of time.

Tip. Many programmers believe that the break and continue keywords violate the structure of the program code and prefer to do without them. So, if possible, it is better to do so.