Last active
December 11, 2015 08:29
-
-
Save mintyplanet/4573940 to your computer and use it in GitHub Desktop.
Sample program for the 488 project language, demonstrating all loop exit constructs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| % a program using all forms of loop building and exit constructs including multi-level exit. | |
| begin | |
| integer sum(integer start, integer end) | |
| begin | |
| integer i, sum | |
| i := start | |
| sum := 0 | |
| loop | |
| if i > end then exit fi | |
| sum := i | |
| i := i + 1 | |
| pool | |
| result sum | |
| end | |
| integer factorial(integer n) | |
| begin | |
| integer f | |
| f := 1 | |
| loop | |
| f := f * n | |
| n := n - 1 | |
| exit when n <= 1 | |
| pool | |
| result f | |
| end | |
| proc hello_world() | |
| begin | |
| loop | |
| put "h" | |
| loop | |
| put "e" | |
| loop | |
| put "l" | |
| loop | |
| put "l" | |
| loop | |
| put "o" | |
| exit 5 | |
| pool | |
| pool | |
| pool | |
| pool | |
| pool | |
| put " world", skip | |
| return | |
| end | |
| proc count_to_100() | |
| begin | |
| integer tens, ones | |
| tens:= 0 | |
| loop | |
| ones := 0 | |
| loop | |
| ones := ones + 1 | |
| put tens * 10 + ones, skip | |
| exit 2 when tens * 10 + ones = 99 | |
| exit when ones = 9 | |
| pool | |
| tens := tens + 1 | |
| pool | |
| put "one hundred!", skip | |
| return | |
| end | |
| hello_world() | |
| put "sum from 1 to 100 = ", sum(1, 100), skip | |
| put "and 10 factorial = ", factorial(10), skip | |
| put "now counting to 100...", skip | |
| count_to_100() | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment