Basic constructs (AmigaE)

From LiteratePrograms

Jump to: navigation, search
Other implementations: AmigaE | C | C++ | Java | OCaml | Unlambda

This program is under development.
Please help to debug it. When debugging
is complete, remove the {{develop}} tag.


This article describes some basic constructs in the AmigaE programming language].

Loops

There are 4 standard ways to create a loop in AmigaE.

This is a FOR loop.

<<forloop>>=
	FOR n:=0 TO 9
          WriteF('For loop alternative 1: \d\n",n)
	ENDFOR
        FOR n:=0 TO 9 STEP 1 DO WriteF('For loop alternative 2: \d\n",n)

Here is an example of a WHILE loop. The output is exactly the same as in the FOR loop example. In the alternative way of writing this the BUT construct is used to allow for a second expression (sequencing).

<<whileloop>>=
	n:=0
	WHILE n<10
          WriteF('While loop alternative 1: \d\n",n)
          n:=n+1
	ENDWHILE
        n:=0
        WHILE n<10 DO n:=n+1 BUT WriteF('While loop alternative 2: \d\n",n)

The REPEAT ... UNTIL works like the WHILE loop, except the test expression is evaluated after the code block.

<<repeatuntilloop>>=
	n:=0;
        REPEAT
          n:=n+1
          WriteF("'REPEAT .. UNTIL loop \d\n",n)
        UNTIL n=9

This is a never ending DO LOOP - exit is done by using the Amiga E's "goto" statement JUMP

<<doloop>>=
	n:=0;
        DO
          n:=n+1
          WtiteF("'REPEAT .. UNTIL loop \d\n",n)
          IF n=9 JUMP exit
        LOOP
exit:
<<loops>>=
PROC loops()
  forloop
  whileloop
  repeatuntilloop
  doloop
ENDFOR
<<basic_constructs.e>>=
PROC main()
  loops()
ENDPROC
Views