The REPEAT UNTIL control structure is also a form of loop control structure.
Syntax for the REPEAT UNTIL control structure:
REPEAT <Instruction(s)> UNTIL Expression
The example refers to a project in chapter? 18.12 ListContainer. With the statement in the fourth line of the procedure below, the HTTP client is sent into the track to load data from the specified server. Afterwards, the system waits in the waiting loop until the download is complete and the HTTP client is (again) inactive:
Public Sub cmbFeedSource_Click() ListContainer.Clear myHttpClient.URL = Feeds[cmbFeedSource.Index] myHttpClient.Get Repeat Wait 0.01 Until myHttpClient.Status = Net.Inactive ' Automatically select the first feed - if available If ListContainer.Count Then ListContainer.Index = 0 End ' cmbFeedSource_Click()
The REPEAT-UNTIL control structure uses the approximate determination of a zero point of a function f (x) according to the secant approximation method. Incorrect start values for the two arguments xa and xe are recognized and displayed. It should be ensured that the function has exactly one zero point in the completed interval[xa | xe].
Figure 10.3.3.4.1: Proximity method with adjustable accuracy
The source text is completely specified:
' Gambas class file Public Sub Form_Open() FMain.Center FMain.Resizable = False End ' Form_Open() Public Function f(x_wert As Float) As Float Return (x_wert * x_wert) - 4 End ' Function f(x_wert As Float) Public Sub btnNullstelleBerechnenUndAusgeben_Click() Dim iCount As Integer Dim x1 As Float ' Left interval limit Dim x2 As Float ' Right interval limit Dim fEpsilon As Float ' Stop criterion Dim y1, y2, fNullstelle, fDifferenzenquotient As Float x1 = CFloat(txb_x1.Text) x2 = CFloat(txb_x2.Text) fEpsilon = CFloat(txb_epsilon.Text) If f(x1) * f(x2) > 0 Then Message.Error("The start values x1 and x2 are not allowed!") txb_x2.SetFocus Return Endif ' f(x1) * f(x2) > 0 txaTabelle.Clear Repeat y1 = f(x1) y2 = f(x2) fDifferenzenquotient = (x2 - x1) / (y2 - y1) fNullstelle = x1 - (y1 * fDifferenzenquotient) txaTabelle.Text = txaTabelle.Text & " " & Str(iCount) & Chr$(9) & Format$(fNullstelle, "0.0000000") & Chr$(9) & " " & Str(f(fNullstelle)) & Chr$(10) If f(x1) * f(fNullstelle) > 0 Then x1 = fNullstelle Else x2 = fNullstelle Endif Inc (iCount) Until Abs(f(fNullstelle)) < fEpsilon End ' btnNullstelleBerechnenUndAusgeben_Click() Public Sub xGroup_Change() txaTabelle.Clear End ' xGroup_Change()
Auch die REPEAT-UNTIL-Kontroll-Struktur ist eine Form der Loop-Kontroll-Strukturen.
Syntax für die REPEAT-UNTIL-Kontroll-Struktur:
REPEAT <Anweisung(en)> UNTIL Expression
Das Beispiel bezieht sich auf ein Projekt im Kapitel → 18.12 ListContainer. Mit der Anweisung in der vierten Zeile der u.a. Prozedur wird der HTTP-Client in die Spur geschickt, um Daten vom angegebenen Server zu laden. Danach wird in der Warte-Schleife solange gewartet, bis der Download abgeschlossen ist und der HTTP-Client (wieder) inaktiv ist:
Public Sub cmbFeedSource_Click() ListContainer.Clear myHttpClient.URL = Feeds[cmbFeedSource.Index] myHttpClient.Get Repeat Wait 0.01 Until myHttpClient.Status = Net.Inactive ' Automatisch den ersten Feed auswählen - wenn vorhanden If ListContainer.Count Then ListContainer.Index = 0 End ' cmbFeedSource_Click()
Die näherungsweise Bestimmung einer Nullstelle einer Funktion f(x)nach dem Sekanten-Näherungsverfahren verwendet die REPEAT-UNTIL-Kontroll-Struktur. Fehlerhafte Startwerte für die beiden Argumente xa und xe werden erkannt und angezeigt. Es sollte gesichert werden, dass die Funktion im abgeschlossenen Intervall [xa | xe] genau eine Nullstelle besitzt.
Abbildung 10.3.3.4.1: Näherungsverfahren mit einstellbarer Genauigkeit
Der Quelltext wird vollständig angegeben:
' Gambas class file Public Sub Form_Open() FMain.Center FMain.Resizable = False End ' Form_Open() Public Function f(x_wert As Float) As Float Return (x_wert * x_wert) - 4 End ' Function f(x_wert As Float) Public Sub btnNullstelleBerechnenUndAusgeben_Click() Dim iCount As Integer Dim x1 As Float ' Linke Intervallgrenze Dim x2 As Float ' Rechte Intervallgrenze Dim fEpsilon As Float ' Abbruchkriterium Dim y1, y2, fNullstelle, fDifferenzenquotient As Float x1 = CFloat(txb_x1.Text) x2 = CFloat(txb_x2.Text) fEpsilon = CFloat(txb_epsilon.Text) If f(x1) * f(x2) > 0 Then Message.Error("Die Startwerte x1 und x2 sind nicht zulässig!") txb_x2.SetFocus Return Endif ' f(x1) * f(x2) > 0 txaTabelle.Clear Repeat y1 = f(x1) y2 = f(x2) fDifferenzenquotient = (x2 - x1) / (y2 - y1) fNullstelle = x1 - (y1 * fDifferenzenquotient) txaTabelle.Text = txaTabelle.Text & " " & Str(iCount) & Chr$(9) & Format$(fNullstelle, "0.0000000") & Chr$(9) & " " & Str(f(fNullstelle)) & Chr$(10) If f(x1) * f(fNullstelle) > 0 Then x1 = fNullstelle Else x2 = fNullstelle Endif Inc (iCount) Until Abs(f(fNullstelle)) < fEpsilon End ' btnNullstelleBerechnenUndAusgeben_Click() Public Sub xGroup_Change() txaTabelle.Clear End ' xGroup_Change()