📖⚡DO

Do..Loop Construct allows us to repeat a section of code


Option 1: Repeat Forever

Syntax

DO

   COMMANDS

LOOP

Example

 

' DO..LOOP Example
' Do.txt
' Kic8 

  ' A never ending loop example.
  ' This is often the basis for the
  ' main part of a program.
DO 

  ' Toggle an output
  TOGGLE 0 

  ' Pause a short time
  PAUSE 100	

LOOP 

Option 2: WHILE

Syntax

DO WHILE Condition

  COMMANDS

LOOP

Example

 

' WHILE Example
' DoWhile.txt
' Kic8 

 ' Execute a loop while B0 is less than 10

 LET B0 = 0

DO WHILE B0 < 10 ' Test here to see if we start the loop

  ' Increase the value of B0 by 1
  LET B0 = B0 + 1

LOOP

' We have now left the loop

Option 3: UNTIL

Syntax

DO

  COMMANDS

LOOP UNTIL Condition

Example

 

' LOOP UNTIL Example
' LoopUntil.txt
' Kic8 

  ' Execute a loop UNTIL B0 equals 10

LET B0 = 0 ' Try changing this initial value 

' This code gets executed at least once..
DO 

  ' Increase the value of B0 by 1
  LET B0 = B0 + 1

LOOP UNTIL B0 >= 10 ' Do test here, if not true then repeat the code..

' We have now left the loop

EXIT

We can use EXIT  to break out of the section.

Example

 

' DO EXIT Example
' DoExit.txt
' Kic8 

  ' This loop will repeat forever, but..
  ' It will EXIT early if pin0 is pressed

DO 

  ' Do something visible
  TOGGLE 0 
  PAUSE 100	

  ' Exit early if pin0 is pressed
  IF PIN0 = 1 THEN EXIT
  
LOOP 
' We have left the loop

Related Commands:

todo:

Tutorials: Simple Logic Do..Loop Tutorial

Command Reference: AND OR For..Next