User Tools

Site Tools


k10:k10.3:k10.3.6:start

10.3.6 Do..Loop

Syntax:

DO [ WHILE Condition ] . .. [ BREAK | CONTINUE ] . .. LOOP [ UNTIL Condition ] 

With DO… LOOP a series of statements is repeated until the initial condition remains true or until the last condition becomes true. If neither WHILE nor UNTIL are used in the control structure, this results in an infinite loop that can only be exited using the BREAK statement. If the break statement itself is missing, there is an infinite loop that you can no longer stop.

10.3.6.1.1 Syntax description

The following table shows the individual parts of the control structure Do..Loop:

PartDescription
DoThe first statement in the control structure.
WhileIf While is used, the statements in the loop body are only executed if the subsequent Boolean expression is TRUE.
UntilIf Until is used, the instructions in the loop body are only executed until the subsequent Boolean expression is TRUE.
ConditionBoolean expressions
BreakJump from the control structure Do..Loop. The execution of the program is started with the next line after the control structure Do..Loop continued.
ContinueAll statements after Continue are ignored and a new run starts.
LoopThe last statement in the control structure.

Table 10.3.6.1.1.1: Description of the elements of Do..Loop

10.3.6.2 Examples

Example 1

Public Sub LVW2C(lv As ListView, c As Collection) ' ListView To Collection 
  If lv.Count > 0 Then 
     c.Clear 
     lv.MoveFirst() 
     DO
       c[lv.Item.Key] = lv.Item.Text 
     LOOP UNTIL lv.MoveNext() 
  Else 
    Message.Info("The channel list is empty!") 
    c.Clear 
    Return 
  Endif 
End

Example 2

Private Sub Variante_3(iAnfang As Integer, iEnde As Integer) 
  Dim iPartialsumme As Integer = 0 
  Dim iSummand As Integer = iAnfang 
 
  DO WHILE (iSummand < iEnde + 1) 
     iPartialsumme = iPartialsumme + iSummand 
     If IsIntegerRange(iPartialsumme) = False And b = False Then 
        txtPartialsumme.Text = "Error sum " & String.Chr(8713) & " INTEGER" 
        Return 
     Endif
     Inc iSummand 
  LOOP
 
  txtPartialsumme.Text = Str(iPartialsumme) 
 
' Alternative Variante 3.2: 
' WHILE (iSummand < iEndzahl + 1) 
'   iPartialsumme = iPartialsumme + iSummand 
'   INC iSummand 
' WEND ' While End 
' tboxPartialsumme.Text = Str(iPartialsumme) 
 
End ' Variante_3 - Kontrollstruktur DO..WHILE..LOOP

10.3.6.3 Syntactic Sugar

In the control structure DO… LOOP There are many instructions (While, Until, Break, Continue) that you have already got to know. Based on' http://de.wikipedia.org/wiki/Syntactic_Sugar',' syntactic sugar' means syntax extensions in a programming language. These extensions are alternative spellings that do not extend the functionality of a programming language not. Syntactic sugar can be traced back to elementary instructions of the language by changing the diction, because

Do While bedingung 
  ' sequence of instructions
Loop 

is a long version of While-Wend; just like

Do 
  ' sequence of instructions
Loop Until bedingung 

as a long version of Repeat-Until. Also

Do 
  ' sequence of instructions
Loop 

produces an endless loop like

While True 
  ' Sequence of instructions
Wend.

This means, however, that you can only use the control structure DO..LOOP, which is classified as “syntactic sugar” in Gambas. In example 2, the procedure with alternative 3.2 was demonstrated.

Download

Articles

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.6/start.txt · Last modified: 23.09.2023 by honsek

Page Tools