User Manual
MCP Series
Brushed DC Motor Controllers
MCP Series User Manual
182
WHILE - WEND
Syntax
while condition
program statements
wend
• Condition - can be a variable or expression
• Statements - any group of commands to be run inside the loop.
Description
The WHILE - WEND loop executes commands nested inside of it while some condition is true.
The condition is tested before the WHILE - WEND loop is run. This is opposite of DO - WHILE
which will test the condition for true after running once. The condition can be any variable or
expression and is tested every loop until false. A simple example would be using the WHILE -
WEND loop to test the state of an input pin. If the pin is low “go do something” until pin is high.
Notes
The loop will continue until its value equals 0.
WHILE - WEND checks the condition rst. If the condition is false the WHILE - WEND statements
and all program code nested within them will not run.
You can nest multiple WHILE - WEND commands within each other. However, you can not nest
DO - WHILE with a WHILE - WEND together or the compiler will get the WHILE statements
confused.
Example
Connect to the following program with the terminal window set to 9600 baud. The program will
start counting up from 0 to 100. Once index reaches a value of 100 the condition is no longer
true. The less than symbol (<) was used for the condition and 100 is no longer less than 100
making the condition false. Since WHILE - WEND loops if a statement is true the program exits.
;ALL - all_while_wend2.bas
Index var word
Main
Index = 0
While Index < 100 ;run until no longer less than 100
index = index + 1
;print the value of index
puts 0,[0, “Couting: “, dec index]
pause 75
wend
puts 0, [13,13, “Index = “, dec index]
puts 0, [13, “My condition is no longer true.”]
puts 0, [13, “Index is no longer less than 100”]
End