use Windows, Math, Strings, Editor
group Statements
design Demonstrator is
constructor is
cell Window = create MenuWindow("Statement test")
Window <- print("\n\n*** Test1: assignment\n")
float a = 4, b = 7, c = 6
float x = 5
var y = a*x^2 + b*x + c
Window <- print("\ny = a*x^2 + b*x + c when x = [x=3] then y = [a*x^2 + b*x + c]")
c = (a = 3.14)*( b = 73.8)
Window <-print("\n a = [a], b = [b] , c = a * b = [c]")
int r,s,t,u,v,w
r = s = t = u = v = w = 144
Window <- print("\nThe variables u,v,w and c have the same value: [r], [s], [t], [u], [v] and [w]")
Window <- print("\na and b before swapping: a = [a], b = [b]")
(1) a,b = b,a
Window <- print("\na and b after swapping: a = [a], b = [b]")
a += 25.1
b /= a + b^3
int SmallPrimes[] = [ 2, 3, 5, 7, 11, 13, 15, 17, 19]
int i=0
Window <- print("\nThe second prime number is [SmallPrimes[ i += 1 ]]")
Window <- print("\n\n*** Test2: Delayed assignment\n")
i = 7
i ~ 8
i = 19
var j = i ~ 13
i = 0
(2) Window <- print("\nThe first prime number is [SmallPrimes[ i +~ 1 ]],
and the second is [SmallPrimes[ i +~ 1 ]]")
Window <- print("\n\n*** Test3: if - then - else\n")
int Players
utf8 Game = "tennis"
(3) if Game is "tennis" then
Players = 2
else
Players = 0
end
if Game is "handball" then
Players = 2*7
end
Game = "soccer"
if Game is "tennis" then
Players = 2
elif Game is "basket" then
Players = 2*5
elif Game is "soccer" then
Players = 2*11
elif Game is "handball" then
Players = 2*7
end
Window <- print("\nIn [Game] there are [Players] players on the field")
Window <- print("\n\n*** Test4: expr ? true : false\n")
Game is "tennis" ? Players = 2 : Players = 3
Window <- print("\nThe result of 'Game is \"tennis\" ? Players = 2 : Players = 3' with Game = [Game] is Players = [Players]")
Players = Game is "tennis" ? 4 : 5
Window <- print("\nThe result of 'Players = Game is \"tennis\" ? 4 : 5' with Game = [Game] is Players = [Players]")
(4)
Window <- print("\n\n*** Test5: for loops\n")
Window <- print("\nThe months of the year: ")
ansi months[] = ["january","february","march", "april", "may", "june", "july", "august", "september", "october", "november", "december"]
for i = 0 to 11 do
Window <- print("[months[i]] ")
end
for each month in months do
Window <- print("[month] ")
end
(5)
Window <- print("\nThe elements of the array have changed: ")
for each month in months do
Window <- print("[month.upper()] ")
end
Window <- print("\nThe square of some small primes: ")
for each Prime in [2,3,5,7,11] do
Window <- print(" [Prime]^2 = [Prime^2]")
end
Window <- print("\nThe numbered months ")
for each [i] in months do
Window <- print("[i+1]:[months[i]],")
end
int A[4, 4] = [ 1,2,3,4,
5,6,7,8,
9,10,11,12,
13,14,15,16 ]
Window <- print("\n\nTranspose a square matrix - before:
[A[0,0]] [A[0,1]] [A[0,2]] [A[0,3]]
[A[1,0]] [A[1,1]] [A[1,2]] [A[1,3]]
[A[2,0]] [A[2,1]] [A[2,2]] [A[2,3]]
[A[3,0]] [A[3,1]] [A[3,2]] [A[3,3]] ")
for each [i,j] in A do
if j > i then
A[i,j],A[j,i] = A[j,i],A[i,j]
end
end
for each [i,j] in A => j > i ? A[i,j],A[j,i] = A[j,i],A[i,j]
Window <- print("\n\nTranspose a square matrix - after:
[A[0,0]] [A[0,1]] [A[0,2]] [A[0,3]]
[A[1,0]] [A[1,1]] [A[1,2]] [A[1,3]]
[A[2,0]] [A[2,1]] [A[2,2]] [A[2,3]]
[A[3,0]] [A[3,1]] [A[3,2]] [A[3,3]] ")
var Sum = 0, Count = 0
for i = 0 to 3, j = 0 to 5, k = 0 to 7 do
Sum += i + j + k
Count += 1
end
Window <- print("\nThe sum after [Count] iterations is [Sum]")
Window <- print("\n\nGeneral for-loop. Distances are: ")
float Distance
for ( Distance = 0.25 , Distance < 12.50, Distance *= 1.5 ) do
Window <- print(" [Distance]")
end
Window <- print("\n\n*** Test6: while-do and repeat-until \n")
float RandomSum = 0
var Iterations = 0
while RandomSum < 2.0 do
RandomSum += randf()
RandomSum -= randf()
Iterations += 1
end
Window <- print("\nWhile-do loop. Random sum = [RandomSum] after [Iterations] Iterations")
RandomSum = Iterations = 0
repeat
RandomSum += randf()
RandomSum -= randf()
Iterations += 1
until RandomSum >= 2.0
Window <- print("\nrepeat-until loop. Random sum = [RandomSum] after [Iterations] Iterations")
Window <- print("\n\n*** Test7: switch statement \n")
i=405
ansi Selection
(6) switch i
case 17: Selection = "seventeen"
case 21: Selection = "twenty one"
case 13: Selection = "thirteen"
case 405: Selection = "four hundred and five"
case 55: Selection = "fifty five"
default : Selection = "the default selection"
end
Window <- print("\nThe selected element [i] is [Selection]")
ansi month = "august"
var Days = 0
ansi Quotation = null
switch month
case "january": Days = 31
case "february": Days = 28
Quotation = "
Why, what's the matter,
That you have such a February face,
So full of frost, of storm and cloudiness?
-- William Shakespeare 'Much Ado About Nothing'"
case "march": Days = 31
Quotation = "
Our life is March weather, savage and serene in one hour.
-- Ralph Waldo Emerson"
case "april": Days = 30
case "may": Days = 31
case "june": Days = 30
case "july": Days = 31
case "august": Days = 31
Quotation = "
August rain: the best of the summer gone,
and the new fall not yet born. The odd uneven time.
-- Sylvia Plath, 'The Unabridged Journals of Sylvia Plath'"
case "september": Days = 30
case "october": Days = 31
case "november": Days = 30
case "december": Days = 31
default : Days = 0
end
Window <- print("\nThe month of [month] has [Days] days")
Quotation ? Window <- print("[Quotation]")
Window <- print("\n\n*** Test8: Leave and continue \n")
const n = 100
int Primes[n]
int NrOfPrimes = 1
int SquareRoot
Primes[0] = 2
task FindPrimes
for (i=3,true,i+=1) do
SquareRoot = sqrt( i )
task TestPrimes
for j=0 to NrOfPrimes-1 do
i % Primes[j] is 0 ? continue FindPrimes
Primes[j] > SquareRoot ? leave TestPrimes
end
Primes[ NrOfPrimes +~ 1 ] = i
NrOfPrimes is n ? leave FindPrimes
end
Window <- print("\nFound [NrOfPrimes] primes:")
for each [i] in Primes do
i % 20 is 0 ? Window <- print("\n")
Window <- print("\t[Primes[i]]")
end
end
end