Loops and Arrays
A natural extension of the if-statement, which conditionally executes a block of instruction,is the loop, which repeats a block of instruction until condition is met. There are two kinds
of loops available in RobotC, the while-loop and the for-loop. As we will see, they have the
same functionality, but for the sake of readability, use dierent syntaxes.
6.1 While-loops
A while-loop, like an if-statement, has a predicate followed by a block of instruction. Unlike
the if-statement, however, at the end of the block, the program execution returns to the top
of the loop, the predicate is checked again and if it is true, the block is executed again. The
block is executed over and over until, for some reason, the predicate becomes false. When
the predicate if false, the block is skipped and the program resumes at the line after the
closing brace, }, of the block.
A while-loop can run once, 10 times, an innite number of times or not at all depending
on the behavior of the predicate. The syntax is given in Listing 6.1.
while ( [ predicate ] ) {
// block of instruction
}
Listing 6.1: The syntax of a while-loop. The block of instruction is executed until the
predicate becomes false.
Listing 6.2 shows a program that shows the light sensor reading. The program exits when
the touch sensor is depressed. Notice that the touch sensor value, a Boolean-type, can be
used as the predicate of the while-loop (in this case negated). Notice also the use of the
wait10Msec() function to delay the loop execution each time. Without this function call,
the display is updated so quickly that the light meter number
ickers and is hard to read.
In Listing 6.3, a while-loop checks the touch sensor over and over. When the touch sensor
is pressed, the loop exits and the \balloon" is popped.
The most useful aspect of a while-loop is that its duration is indenite. On the
ip
side, the most aggravating aspect of a while-loop is that its duration is indenite (possibly
# pragma config ( Sensor , S1 , trigger , sensorTouch )
# pragma config ( Sensor , S2 , light , sensorLightInactive )
task main () {
while (!( SensorValue [ trigger ])) {
nxtDisplayCenteredBigTextLine (2,"%d",SensorValue [ light ]);
// slows the loop down so that number does not flicker
wait10Msec (5);
}
}
Listing 6.2: This program displays the light sensor reading. The while-loop repeatedly polls
the light sensor value and displays it. The loop exits when the touch sensor is depressed.
# pragma config ( Sensor , S1 , trigger , sensorTouch )
task main () {
nxtDrawEllipse (18 ,63 ,78 ,0);
// loops until trigger is pressed
while (!( SensorValue [ trigger ])) {}
eraseDisplay ();
nxtDisplayBigStringAt (28 ,40 ,"Pop!");
wait10Msec (300);
}
Listing 6.3: This program displays a \balloon" that pops when the touch sensor is depressed.
The while-loop halts the program until the touch sensor is pressed. The block of the while-
loop is trivial.
innite). A common programming error is to build a while-loop that has no logical end. In
other words, the programmer's exit strategy is
awed.
Another purpose of while-loops in the context of robotics is the detection of status changes
in sensors. Consider the program in Listing 6.4 which counts the number of times the touch
sensor has been pressed. The rst inner while-loop, with an empty block, simply puts the
program into a \holding pattern" until the trigger is pressed. When the trigger is pressed, a
counter is incremented and a second while-loop with an empty block puts the program into
a holding pattern until the trigger is released. The outer while-loop has a trivial predicate
with literal value true. This loop will run forever, or until the battery runs down, or until
we press the orange stop button on the NXT brick. This program is nearly always in one
holding pattern or the other. Between holding patterns, the counter is incremented (notice
too that it has been initialized to zero) and the display is updated.
# pragma config ( Sensor , S1 , trigger , sensorTouch )
task main () {
int count =0;
nxtDisplayCenteredBigTextLine (3,"%d",count );
while (1) {
// hold as long as trigger is not depressed
while (!( SensorValue [ trigger ])) {}
// increment the trigger count by 1
count ++;
// hold as long as trigger is depressed
while (( SensorValue [ trigger ])) {}
// reset display and display the current
eraseDisplay ();
nxtDisplayCenteredBigTextLine (3,"%d",count );
}
}
Listing 6.4: This program will count the number of times the touch sensor has been depressed
and display the count on the screen.
No comments:
Post a Comment