Programming instructions
Reference
Project Lead The Way
©
and Carnegie Mellon Robotics Academy
©
/ For use with VEX
®
Robotics Systems
Main Title • 1
While Loops with Natural Language
while(condition)
{
// repeated-commands
}
There are three main parts to every while loop.
Part 1. The keyword “while”.
The condition is true as long as
1 is equal to 1, which is always.
task main()
{
while(1 == 1)
{
startMotor(port2, 63);
wait(5.0);
startMotor(port2, -63);
wait(5.0);
}
}
Below is an example of a program using an innite While Loop.
A while loop is a structure within ROBOTC which allows a section of code to be repeated
as long as a certain condition remains true.
While the condition is true, the
port2 motor will turn forward
for 5 seconds, then in reverse
for 5 seconds.
Result: The port2 motor will turn
back and forth, forever.
while
Every while loop begins with the keyword “while”.
while(condition)
{
// repeated-commands
}
Part 2. The 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
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
}
Part 3. The commands to be repeated, or “looped”.
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.
Go to Reference Links