Sie sind auf Seite 1von 5

NQC: Control Structures: Loops

Last updated 10/5/05


References:
NQC Tutorial
Baum Chapter 3
Baum Appendix D pg 368.

While Control Structure




Sample forms
1) while(Timer(0) <= 10) x+=5;

2) while(z != 5)
{
if(SENSOR_1==0)
{
STATEMENTS EXECTUED IF TRUE
}
else
{
STATEMENTS EXECTUED IF FALSE
}
}
-----------------------------------------------

General Form
while(condition) statement
Creating an Infinite Loop

while(true)
{
until(Timer(0) > TIMER_LIMIT);
PlayTone(440, 10);
ClearTimer(0);
bumps = 0;
}


A Closely Related Cousin to the while

do
{
Toggle(OUT_A);
Off(Out_B);
} while (SENSOR_2 <= 10);

Notice the do while is always executed at least one
time since the test of the condition is at the bottom of
the loop.
General Form
do statement while(condition)
The until control structure
Example
until(SENSOR_2>=5) x+=1;

This is often used to trigger a segment of code when an event
occurs

until(SENSOR_1==1)

Note in the last example the statement is the null statement. The
program acts as though it holds at this point until the event
SENSOR_1==1 occurs and then it is released to the next
statement in the program.
General Form
until(condition) statement