HP - While
HP - While
Reference
while(condition) (condition)
{ The condition controls how long or how many times a while loop
repeats. While the condition is true, the while loop repeats; when
// repeated-commands the condition is false, the while loop ends and the robot moves
} on in the program. The condition is checked every time the loop
repeats, before the commands between the curly braces are run.
while(condition)
{
// repeated-commands Repeated commands
Commands placed between the curly braces will repeat
} while the (condition) is true when the program checks at
the beginning of each pass through the loop.
task main()
{
while(1 == 1) The condition is true as long as
1 is equal to 1, which is always.
{
startMotor(port2, 63);
wait(5.0);
While the condition is true, the
startMotor(port2, -63); port2 motor will turn forward
wait(5.0); for 5 seconds, then in reverse
for 5 seconds.
}
} Result: The port2 motor will turn
back and forth, forever.
© Carnegie Mellon Robotics Academy / For use with VEX® Robotics Systems While Loop
ROBOTC
Reference
task main()
{
int count = 0; Creates an integer variable named
“count” and gives it an initial value of 0.
while(count < 4) Checks if count is “less than” 4.
{
startMotor(port2, 63);
wait(5.0);
startMotor(port2, -63);
wait(5.0);
task main()
{
while(SensorValue[Estop] == 0) Checks if the “Estop” touch sensor
is equal to 0 (unpressed).
{
if(SensorValue[controlBtn] == 1)
If the “controlBtn” is pressed, turn the LED
{ on; if it’s not, turn the LED off.
turnLEDOn(LED);
Result: The loop repeats continuously,
} allowing the LED to be turned on while
else the “controlBtn” is pressed, and off while
“controlBtn” is released. The loop will
{ stop as soon as the “Estop” touch sensor
turnLEDOff(LED); is pressed.
}
}
}
© Carnegie Mellon Robotics Academy / For use with VEX® Robotics Systems While Loop