 |
|
BaselScript |
Beschreibung
Schleife
Es gibt z.B. eine Aufgabe: Zahl 3 zehn Mal addieren
Einfache Lösung könnte so aussehen:
1. Schritt: #sum = #sum+3 (Ergebnis #sum = 3)
2. Schritt: #sum = #sum+3 (Ergebnis #sum = 6)
...
10. Schritt: #sum = #sum+3 (Ergebnis #sum = 30)
Eine Schleife (auch „Wiederholung“ oder loop) ist eine Anweisungssektion,
die sich wiederholt, bis eine bestimmte Bedingung erreicht wird.
// clear result
#sum = 0
// loop: section "sum" will be executed until #index is less than 10
for #index= 0 step=1 to = 9 section=sum
// this section will be repeated 10 times
section sum
#sum = #sum + 3
// output result
message #sum
end section
Elementen Erklärung:
#index - Schleifenvariable
#index = 1 - Anfangszahl
step = 1 - Schrittweite
to = 10 - Endzahl
section sum - Schleifenkörper (Anweisungssektion)
Beispiel:
Verwendung von Arrays in einer Schleife:
Es gibt zwei Arrays für jede Person - name und sex
#name[0] = “Monica”
#name[1] = “Roland”
#name[2] = “Alex”
#sex[0] = Frau
#sex[1] = Herr
#sex[2] = Herr
Wir wollen nur weibliche Personen auswählen:
for #index=0 step=1 to=2 section=select_female
section select_female
// select only female from array sex
if sex[#index] == Frau
// select from array name with the same #index
message #name[#index]
endif
end section
Ergebnis: Monica
Beispiel für verschachtelte Schleifen:
scene=1 name= "loop in loop"
section init
draw form
#sum1=0
#sum2=0
#sum3=0
call section=aa
for #index=0 step=1 to=3 section=test
message #sum1 + "/" + #sum2
message #t + "/" + #tt
end section
--------------------------------------
section test
#sum1=#sum1+10
for #index2=0 step=1 to=2 section=test2
end section
-----------
section test2
#sum2=#sum2+100
for #index3=0 step=1 to=7 section=test3
message #sum3
end section
section test3
#sum3=#sum3+122
end section
section aa
#t=xxx
call section=bb
end section
section bb
#tt=abcd
end section
section back
message xxx
end section
end scene