User Tools

Site Tools


k10:k10.3:k10.3.4:start

10.3.4 WHILE-WEND control structure

The WHILE-WEND control structure presented in the following sections is another form of loop control structure.

10.3.4.1 Syntax

Syntax for the WHILE-WEND control structure:

WHILE Expression
  <Instruction(s)> 
WEND

10.3.4.2 Notes on the syntax

  • The loop is repeated as long as the expression is true.
  • Since the check of the termination condition is carried out at the beginning, the WHILE-WEND control structure is never * executed if the (start) printout is already incorrect.
  • ' DO WHILE'-LOOP control structure is an equivalent structure to the general DO-LOOP control structure → chapter 10.3.1 Do..Loop.

10.3.4.3 Example 1

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) 

10.3.4.4.4 Example 2

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

10.3.4.5 Example 3

Example 3 demonstrates the use of four different loop control structures for processing the same task:

  • FOR..TO..NEXT control structure
  • REPEAT..UNTIL control structure
  • WHILE..WEND control structure
  • DO..WHILE..LOOP control structure (alternative)

Partialsummen

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

Download

The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.
k10/k10.3/k10.3.4/start.txt · Last modified: 23.09.2023 by honsek

Page Tools