'''DO...LOOP''' statements are used in programs to repeat code or return to the start of a procedure.


{{PageSyntax}}
''Syntax 1:''
:'''[[DO]]''' [{{{KW|WHILE}}|{{KW|UNTIL}}} condition]
:: ''{code}''
:: ⋮
:'''[[LOOP]]''' 


''Syntax 2:''
:'''[[DO]]'''
:: ''{code}''
:: ⋮
:'''[[LOOP]]''' [{{{KW|WHILE}}|{{KW|UNTIL}}} condition]


{{PageDescription}}
* '''DO UNTIL or DO WHILE used with LOOP''': The condition is evaluated before running the loop code.
::[[UNTIL]] checks if the condition is false each time before running code.
::[[WHILE]] checks if the condition is true each time before running code.
* '''DO used with LOOP UNTIL or LOOP WHILE''': The code block will run at least once:
::[[UNTIL]] checks if the condition is false before running loop code again.
::[[WHILE]] checks if the condition is true before running loop code again.
* NOTE: You cannot use a condition after both the DO and LOOP statements at the same time. 
* Use '''[[EXIT]] DO''' to exit a loop block even before the condition is met.
* Use [[_CONTINUE]] to skip the remaining lines in the iteration without leaving the loop.
** If you don't specify a condition, you must exit the loop block manually using '''[[EXIT]] DO'''.
* If a loop never meets an exit condition requirement, it will never stop.


{{Template:RelationalTable}}


{{PageExamples}}
''Example 1:'' Using WHILE to clear the keyboard buffer.
{{CodeStart}}

{{Cl|DO}} {{Cl|WHILE}} {{Cl|INKEY$}} &lt;> "": {{Cl|LOOP}} ' checks evaluation before running loop code

{{Cl|DO}}: {{Cl|LOOP}} {{Cl|WHILE}} {{Cl|INKEY$}} &lt;> "" ' checks evaluation after one run of loop code

{{CodeEnd}}


''Example 2:'' Using UNTIL to clear the keyboard buffer.
{{CodeStart}}

{{Cl|DO}} {{Cl|UNTIL}} {{Cl|INKEY$}} = "": {{Cl|LOOP}} ' checks evaluation before running loop code

{{Cl|DO}}: {{Cl|LOOP}} {{Cl|UNTIL}} {{Cl|INKEY$}} = "" ' checks evaluation after one run of loop code

{{CodeEnd}}


''Example 3:'' Using a one time DO loop to exit ANY of several FOR LOOPs, without using [[GOTO]]. 
:SUB reads header contents of a [[BSAVE]] file that may include embedded RGB color settings before the image.
{{CodeStart}} '' ''
{{Cl|DEFINT}} A-Z
{{Cl|INPUT}} "Enter a BSAVE file name to read the file for screen mode:"', filenm$
CheckScreen filenm$

{{Cl|END}}

{{Cl|DEFINT}} A-Z
{{Cl|SUB}} CheckScreen (Filename$)        'find Screen mode (12 or 13) and image dimensions
   {{Cl|DIM}} Bsv {{Cl|AS}} {{Cl|STRING}} * 1
   {{Cl|DIM}} Header {{Cl|AS}} {{Cl|STRING}} * 6

   Scr = 0: MaxColors = 0
   {{Cl|OPEN}} Filename$ {{Cl|FOR (file statement)|FOR}} {{Cl|BINARY}} {{Cl|AS}} #1

   {{Cl|GET}} #1, , Bsv           '1 check for small 2 character
   {{Cl|GET}} #1, , Header        '2 - 7 rest of file header

   {{Cl|IF}} Bsv &lt;> {{Cl|CHR$}}(253) {{Cl|THEN}}   ' small 2 character denotes a {{Cl|BSAVE}} file
      {{Cl|COLOR}} 12: {{Cl|LOCATE}} 15, 33: {{Cl|PRINT}} "Not a BSAVE file!": {{Cl|SLEEP}} 3: {{Cl|EXIT SUB}}
   {{Cl|END IF}}

   {{Cl|GET}} #1, , widN           '8 no color info bmp sizes
   {{Cl|GET}} #1, , depN           '9   "        "      "

{{Cl|DO}}
  {{Cl|IF}} widN > 63 {{Cl|OR}} depN > 63 {{Cl|THEN}} {{Cl|EXIT DO}}  ' width and depth already found

  {{Cl|FOR}} i = 10 {{Cl|TO}} 55       'check for Screen 12 embedded colors
    {{Cl|GET}} #1, , RGB
    tot12&amp; = tot12&amp; + RGB
    'PRINT i; RGB; : SOUND 300, 1         'test sound slows loop in QB
    {{Cl|IF}} RGB > 63 {{Cl|OR}} RGB &lt; 0 {{Cl|THEN}} {{Cl|EXIT DO}}
    {{Cl|IF}} i = 55 {{Cl|AND}} tot12&amp; = 0 {{Cl|THEN}} {{Cl|EXIT DO}}
  {{Cl|NEXT}}

  {{Cl|GET}} #1, , wid12          '56
  {{Cl|GET}} #1, , dep12          '57
  {{Cl|IF}} wid12 > 63 {{Cl|OR}} dep12 > 63 {{Cl|THEN}} {{Cl|EXIT DO}}

  {{Cl|FOR}} i = 58 TO 775      'check for Screen 13 embedded colors
    {{Cl|GET}} #1, , RGB
    tot13&amp; = tot13&amp; + RGB
    'PRINT i; RGB; : SOUND 300, 1          'test
    {{Cl|IF}} RGB > 63 {{Cl|OR}} RGB &lt; 0 {{Cl|THEN}} {{Cl|EXIT DO}}
    {{Cl|IF}} i = 775 {{Cl|AND}} tot13&amp; = 0 {{Cl|THEN}} {{Cl|EXIT DO}}
  {{Cl|NEXT}}
  {{Cl|GET}} #1, , wid13          '776
  {{Cl|GET}} #1, , dep13          '777
{{Cl|LOOP}} {{Cl|UNTIL}} 1 = 1    'TRUE statement exits one-time LOOP
{{Cl|CLOSE}} #1

{{Cl|COLOR}} 14: {{Cl|LOCATE}} 10, 25
{{Cl|SELECT CASE}} i
  {{Cl|CASE IS}} &lt; 56:
   {{Cl|IF}} widN > 640 {{Cl|THEN}}
       Scr = 13: MaxColors = 0
       {{Cl|PRINT}} "Default Screen 13:"; widN \ 8; "X"; depN
   {{Cl|ELSE}}
    {{Cl|LOCATE}} 10, 15
    {{Cl|PRINT}} "Screen 12 ("; widN; "X"; depN; ") OR 13 ("; widN \ 8; "X"; depN; ")" 
    {{Cl|DO}}: {{Cl|SOUND}} 600, 4
       {{Cl|COLOR}} 13: {{Cl|LOCATE}} 12, 23  'ask if no data found. Prevents ERROR opening in wrong mode
       {{Cl|INPUT}} "Enter a Screen mode 12 or 13 : ", Scrn$  
       Scr = {{Cl|VAL}}(Scrn$)
    {{Cl|LOOP}} {{Cl|UNTIL}} Scr = 12 {{Cl|OR}} Scr = 13
   {{Cl|END IF}}
   {{Cl|IF}} Scr = 12 {{Cl|THEN}} MaxColors = 0: PWidth = widN: PDepth = depN
   {{Cl|IF}} Scr = 13 {{Cl|THEN}} MaxColors = 0: PWidth = widN \ 8: PDepth = depN
  {{Cl|CASE}} 56 TO 775
     {{Cl|PRINT}} "Custom Screen 12:"; wid12; "X"; dep12
     Scr = 12: MaxColors = 16: PWidth = wid12: PDepth = dep12
  {{Cl|CASE}} 776: {{Cl|PRINT}} "Custom Screen 13:"; wid13 \ 8; "X"; dep13
     Scr = 13: MaxColors = 256: PWidth = wid13 \ 8: PDepth = dep13
{{Cl|END SELECT}}

{{Cl|END SUB}} '' ''
{{CodeEnd}}
{{small|Code by Ted Weissgerber}}
:''Explanation:'' The SUB procedure reads a file that was [[BSAVE]]d previously. If the RGB colors are stored before the image, the values can only be between 0 and 63. Higher values indicate that the image width and height are located there and that there are no stored color values to be read. SUB later displays the dimensions of the file image that [[GET (graphics statement)|GET]] placed in the file array. The loop is set to only run once by creating '''a TRUE [[UNTIL]] statement''' such as 1 = 1. When a screen mode cannot be determined, the user must select one.

:Dimensions and location of width and height information indicates the screen mode as [[SCREEN (statement)|SCREEN]] 13 if it has 768 RGB values and dimensions of 320 X 200 max. If the file only holds 64 settings and/or is larger than 320 X 200, it uses SCREEN 12 or 9. The procedure [[EXIT]]s the DO LOOP early when the image size is found with or without custom color settings. 

: Divide SCREEN 13 [[GET (graphics statement)|GET]] widths by 8.


{{PageSeeAlso}}
* [[EXIT DO]]
* [[WHILE...WEND]]
* [[FOR...NEXT]]


{{PageNavigation}}
