EasyManua.ls Logo

LEGO MINDSTORMS Robots

LEGO MINDSTORMS Robots
226 pages
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
while (SENSOR_3 < 35) {
PlaySound(0);
Wait(50);
}
Notice how curly braces are used to bracket the statements that belong to the while loop. If you have only one command in the body of the while, you can omit the braces like this:
while (SENSOR_3 < 35)
Wait(50);
Conditionals
To test a condition, use the if command.
if (boolean condition) [statements]
This command executes the given statements only if condition is true.
if (boolean condition) [statements] else [statements]
This is a simple variation on the basic if command. If the condition is false, the statements after the else are executed.
The following example turns different directions depending on the value of input 2:
SetPower(OUT_A + OUT_C, OUT_FULL);
if (SENSOR_2 < 50) {
Fwd(OUT_A);
Rev(OUT_C);
}
else {
Rev(OUT_A);
Fwd(OUT_C);
}
On(OUT_A + OUT_C);
Page 67
Variables
To use a variable, you simply need to declare its name. Only integer variables are supported. Once a variable is declared, you can assign the variable values and test it in the body of the program.
Here's a simple example:
int i;
task main() {
i = 0;
while (i < 10) {
PlaySound (0);
Wait(5 i);
i += 1;
}
}
This example beeps at successively longer intervals. The variable, i, is declared in the very first line:
int i;
Values are assigned to the variable using the = operator:

Table of Contents

Related product manuals