The WHILE-WEND control structure presented in the following sections is another form of loop control structure.
Syntax for the WHILE-WEND control structure:
WHILE Expression <Instruction(s)> WEND
A file is opened for reading. Then the contents of the file are read out line by line and each line is stored in an array - as long as the end of the file has not yet been reached and lines can still be read:
hFile = Open sRubrikPfad For Input While NOT Eof(hFile) Line Input #hFile, sZeile aSuchDateiMatrix.Add(sZeile) Wend Close #hFile aSuchDateiMatrix.Sort(0)
The task: 'As long as there is still a block of two consecutive blanks in a character string, all blocks of two blanks are to be replaced by a single blank' has a simple solution:
While InStr(sTestString, " ") sTestString = Replace$(sTestString, " ", " ") Wend
Example 3 demonstrates the use of four different loop control structures for processing the same task:
Figure 10.3.4.5.5.1: Calculation of partial sums according to 3 variants
The source text is only specified in the relevant extracts:
… Public Sub btnPartialsummeBerechnen_Click() If KontrolleEingabedaten() = "Data input error-free!" Then If optVariante1.Value = True Then Variante_1(iAnfangszahl, iEndzahl) ' FOR..TO..NEXT Else If optVariante2.Value = True Variante_2(iAnfangszahl, iEndzahl) ' REPEAT..UNTIL Else Variante_3(iAnfangszahl, iEndzahl) ' WHILE..WEND Endif Endif End ' btnPartialsummeBerechnen Private Sub Variante_1(iAnfang As Integer, iEnde As Integer) ' FOR..TO..NEXT Dim iPartialsumme As Integer = 0 Dim iSummand As Integer = iAnfang Dim iCount As Integer = 0 For iCount = 1 To (iEnde - iAnfang + 1) Step 1 iPartialsumme = iPartialsumme + iSummand ' Overflow due to overrange for integer? If (IsIntegerRange(iPartialsumme) = False) And If (bSpezialfall22 = False) Then txtPartialsumme.Text = "ERROR Sum " & String.Chr(8713) & " INTEGER" Return Endif Inc iSummand Next ' iCount txtPartialsumme.Text = Str(iPartialsumme) End Private Sub Variante_2(iAnfang As Integer, iEnde As Integer) ' REPEAT..UNTIL Dim iPartialsumme As Integer = 0 Dim iSummand As Integer = iAnfang Repeat iPartialsumme = iPartialsumme + iSummand If (IsIntegerRange(iPartialsumme) = False) And If (bSpezialfall22 = False) Then txtPartialsumme.Text = "ERROR Sum " & String.Chr(8713) & " INTEGER" Return Endif Inc iSummand Until (iSummand > iEnde) txtPartialsumme.Text = Str(iPartialsumme) End Private Sub Variante_3(iAnfang As Integer, iEnde As Integer) ' WHILE..WEND Dim iPartialsumme As Integer = 0 Dim iSummand As Integer = iAnfang While (iSummand < iEndzahl + 1) iPartialsumme = iPartialsumme + iSummand If (IsIntegerRange(iPartialsumme) = False) And If (bSpezialfall22 = False) Then txtPartialsumme.Text = "ERROR Sum " & String.Chr(8713) & " INTEGER" Return Endif ' Partialsumme-Überlauf ? Inc iSummand Wend ' While End txtPartialsumme.Text = Str(iPartialsumme) ' Alternative Variante 3: ' Do While (iSummand < iEnde + 1) ' iPartialsumme = iPartialsumme + iSummand ' If (IsIntegerRange(iPartialsumme) = False) And If (bSpezialfall22 = False) Then ' txtPartialsumme.Text = "ERROR Sum " & String.Chr(8713) & " INTEGER" ' Return ' Endif ' Inc iSummand ' Loop ' txtPartialsumme.Text = Str(iPartialsumme) End