! Program structures

[{TableOfContents }] \\


[Wil Baden | http://home.earthlink.net/ ~ neilbawd /], which you encounter in the English literature often has, in
Escaping his contribution FORTH stated the following: There are four types of control statements:

* The sequence of instructions
* The selection of program parts,
* The repetition of instructions and parts of the program,
* The demolition.

The first three possibilities are absolutely necessary and in the older languages such as PASCAL exclusively available. According to a statement is in the people-FORTH for the selection of program parts are available, the execution is the result of a logical expression subject:

{{{ 
IF flag THEN <Anweisungen> 
IF flag THEN <Anweisungen> ELSE <Anweisungen> 
}}} 

Where, however, the program made a return for instructions wiederho1t run is used for a given number of times this statement, with the current index of I and J is available: 

{{{ 
<Grenzen> DO /? <Anweisungen> DO LOOP 
<Grenzen> DO /? <Anweisungen> <Schrittweite> DO + LOOP 
}}} 

If a repetition of instructions to be executed without the number of runs known 1st, it is an index variable board or otherwise to the result of a logical expression to come. The following construction provides an infinite loop: 

{{{ 
BEGIN REPEAT <Anweisungen> 
}}} 

The repetition of statements are so far balanced that a statement is so long (while running), such an expression is true, or a statement is repeated, right up to (until) one expression. 

{{{ 
BEGIN UNTIL <Anweisungen> flag 
<Anweisungen> Flag BEGIN WHILE REPEAT <Anweisungen> 
}}} 

Both possibilities can in populous FORTH combine, including several (multiple) WHILE may occur in a control statement. 

{{{ 
BEGIN WHILE <Anweisungen> <Anweisungen> flag flag UNTIL 
}}} 

Now occurs in applications often the case that a control instruction to be abandoned, because something has happened. 

Then the fourth situation, the demolition, given the programming language "C" provides for the functions:''break'',''continue'',''return''and''exit''available, state-FORTH offers ''exit'',''leave'',''endloop'',''quit'',''abort'',''abort "abort''and''(''to. In FORTH EXIT to used to leave the definition, in which it appears, however, the smallest enclosing leaves LEAVE DO ... LOOP. 

! Glossary

As of version 3.81.8 has state-FORTH on additional control instruction for the compiler, conditional compilation in the form:

(((
<word> have not. <action1> IF. <action2> ELSE. THEN
)))

DieseWorte be used outside of colon definitions and replace the''\''needs of earlier versions.

* [Have]
* [Exit]
* [? Exit | question-exit]
* [0 = exit |-equals-zero exit]
* [If]
* [. IF]
* [Then]
* [. THEN]
* [Else]
* [. ELSE]
* [Do]
* [? Do | question-do]
* [Loop]
* [+ Loop | plus-loop]
* [I]
* [J]
* [Leave]
* [Endloop]
* [Bounds]
* [Begin]
* [Repeat]
* [Until]
* [While]
* [Execute]
* [Perform]
* [Case? | Case-question]
* [Stop]

! Words for error handling

They work well as control statements, as the definitions of''ARGUMENTS''and''IS''DEPTH::

(((
is-depth (n -)
  depth 1 - - abort "wrong number of parameters!" ;
)))

IS-DEPTH review the stack on a given number of stack elements (depth point).

* [Abort]
* ['Abort | abort-tick]
* [Abort "| abort-quote]
* [Error "| error rate]
* [Error handler]
* [(Error | paren-error]
* [R # | r-sharp]
* [Scr]
* [Quit]
* [? Pairs | question-pairs]

! Case distinction in FORTH 

! With IF ELSE structuring THEN / ENDIF 

It is worth briefly the various possibilities are shown, which can merde made a case distinction in FORTH. Characteristic of such a program situation is that just from a different possibilities of the program flow to be chosen. 

Starting from a clear problem, a game, are described based on the necessary definitions and the development of the above-described control structure. 

An example is a game with simple rules: 

This drinking game, which according to the article "Ultimate CASE Statement" ([Fourth Dimension 2 / 87 | http://forth-ev.de/filemgmt/visit.php?lid=116], page 40 ff) also called CRAPS is, it is about to distribute a supply of jars filled with the players with the help of the cube and leerzutrinken: 

* When ONE was taken a glass out of the stock in the middle of the table and placed before him. 
* For a TWO or THREE got the neighbor / neighbor left zugesehoben a glass of its own stock. 
* If the FOUR or FIVE was the neighbor about the neighbor on the right set before a glass of its own stock. 
* At a SIX, all glasses, the gamer had emptied before him. 

Assignment is: 1 = accept, 2 / 3 = left, 4 / 5 = right, drink 6 = and according to the number of the cube is one of six possible actions are executed. The program should be limited to, read and evaluate the outcome of dice. A message is issued to perform which of the six acts. 

For such a program a number entry is required. This was realized here with the word-F83 NUMBERS: 

((( 
: F83-number? (String - d f) 
   number? ? Dup 
   IF 
     0 <IF THEN extend 
     true exit 
   THEN 
   drop false 0 0; 
    
: Input # (string - s) 
   pad c / l 1 -> expect 
   F83-pad number? 2drop; 
))) 

The definition of the words that are to carry six above-mentioned actions symbolically depends on the rules that dictate exactly one result for each cube action: 

((( 
\ Take drink push left right 

: Take bright. "Take a glass of" normal two spaces; 
: Drink bright. "Drink all the glasses" normal two spaces; 
spaces: left bright. "a glass to the left" normal 2; 
spaces: the right bright. "a glass to the right" normal 2; 

: Slide; 

))) 

PUSHING''''is a dummy procedure, a filler, the necessity arises only very late. For dialogue with countries will deflniert users: 

((( 
: Cr request. "If you take drink or move?" 
             cr. "Please your eyes and <cr> number:"; 
              
: Congratulations cr. "Good luck on the next roll ..." ; 
))) 

The word''RESULTS''is to perform in accordance with a selector just one of 6 possible procedures. So we will examine whether this or this or ... the Mogiichkeiten comes into question. Add to that the test whether the passed parameter was between (between) 1 and 6. 

The Def nit! On is of''BETWEEN''according to folk-FORTH quite short: 

((( 
(Lower limit value ceiling - false or) 
(- True if lower <<= value = upper limit) 
: Between 1 + uwithin; 

: Auswertung.1 (draft results -) 
  dup 1 = IF ELSE take 
          dup IF 2 = move left ELSE 
          dup = 3 ELSE IF one left 
                  dup 4 ELSE IF the right move 
                  dup 5 ELSE IF the right move 
                        dup 6 = IF THEN drink 
                        THEN 
                        THEN 
                  THEN 
                  THEN 
          THEN 
  1 6 between IF not inversely. "Fraud!" THEN normal; 
))) 

(To be continued ...)