NQC Quick Reference
Statements
Statment |
Description |
while (cond) body |
Execute body zero or more times while condition is true |
do body while (cond) |
Execute body one or more times while condition is true |
until (cond) body |
Execute body zero or more times until condition is true |
break |
Break out from while/do/until body |
continue |
Skip to next iteration of while/do/until body |
repeat (expression) body |
Repeat body a specified number of times |
switch (expression) body |
Execute one alternative within body depending on the value of expression. |
if (cond) stmt1 if (cond) stmt1 else stmt2 |
Execute stmt1 if condition is true. Execute stmt2 (if present) if condition is false. |
start task_name |
Start the specified task |
stop task_name |
Stop the specified task |
function (args) |
Call a function using the supplied arguments |
var = expression |
Evaluate expression and assign to variable |
var += expression |
Evaluate expression and add to variable |
var -= expression |
Evaluate expression and subtract from variable |
var *= expression |
Evaluate expression and multiply into variable |
var /= expression |
Evaluate expression and divide into variable |
var |= expression |
Evaluate expression and perform bitwise OR into variable |
var &= expression |
Evaluate expression and perform bitwise AND into variable |
return |
Return from function to the caller |
expression |
Evaluate expression |
Conditions
Conditions are used within control statements to make decisions. In most cases, the condition will involve a comparison between expressions.
Condition |
Meaning |
true |
always true |
false |
always false |
expr1 == expr2 |
true if expressions are equal |
expr1 != expr2 |
true if expressions are not equal |
expr1 < expr2 |
true if expr1 is less than expr2 |
expr1 <= expr2 |
true if expr1 is less than or equal to expr2 |
expr1 > expr2 |
true if expr1 is greater than expr2 |
expr1 >= expr2 |
true if expr1 is greater than or equal to expr2 |
! condition |
logical negation of a condition |
cond1 && cond2 |
logical AND of two conditions (true if and only if both conditions are true) |
cond1 || cond2 |
logical OR of two conditions (true if and only if at least one of the conditions is true) |
Expressions
There are a number of different values that can be used within expressions including constants, variables, and sensor values. Note that SENSOR_1, SENSOR_2, and SENSOR_3 are macros that expand to SensorValue(0), SensorValue(1), and SensorValue(2) respectively.
Value |
Description |
number |
A constant value (e.g. 123) |
variable |
A named variable (e.g x) |
Timer(n) |
Value of timer n, where n is between 0 and 3 |
Random(n) |
Random number between 0 and n |
SensorValue(n) |
Current value of sensor n, where n is between 0 and 2 |
Watch() |
Value of system watch |
Message() |
Value of last received IR message |
Values may be combined by using operators. Several of the operators may only be used in evaluating constant expressions, which means that their operands must be either constants or expressions involving nothing but constants. The operators are listed here in order of precedence (highest to lowest).
Operator |
Description |
Associativity |
Restriction |
Example |
abs() sign() |
Absolute value Sign of operand |
n/a n/a |
none none |
abs(x) sign(x) |
++ -- |
Increment Decrement |
left left |
variables only variables only |
x++ or ++x x-- or --x |
- ~ |
Unary minus Bitwise negation (unary) |
right right |
none constant only |
-x ~123 |
* / % |
Multiplication Division Modulo |
left left left |
none none constant only |
x * y x / y 123 % 4 |
+ - |
Addition Subtraction |
left left |
none none |
x + y x - y |
<< >> |
Left shift Right shift |
left left |
constant only constant only |
123 << 4 123 >> 4 |
& |
Bitwise AND |
left |
none |
x & y |
^ |
Bitwise XOR |
left |
constant only |
123 ^ 4 |
| |
Bitwise OR |
left |
none |
x | y |
&& |
Logical AND |
left |
constant only |
123 && 4 |
|| |
Logical OR |
left |
constant only |
123 || 4 |
RCX Functions
Most of the functions require all arguments to be constant expressions (numbers or operations involving other constant expressions). The exceptions are functions that use a sensor as an argument and those that can use any expression. In the case of sensors, the argument should be a sensor name: SENSOR_1, SENSOR_2, or SENSOR_3. In some cases there are predefined names (e.g. SENSOR_TOUCH) for appropriate constants.
Function |
Description |
Example |
SetSensor(sensor, config) |
Configure a sensor. |
SetSensor(SENSOR_1, SENSOR_TOUCH) |
SetSensorMode(sensor, mode) |
Set sensor's mode |
SetSensor(SENSOR_2, SENSOR_MODE_PERCENT) |
SetSensorType(sensor, type) |
Set sensor's type |
SetSensor(SENSOR_2, SENSOR_TYPE_LIGHT) |
ClearSensor(sensor) |
Clear a sensor's value |
ClearSensor(SENSOR_3) |
On(outputs) |
Turn on one or more outputs |
On(OUT_A + OUT_B) |
Off(outputs) |
Turn off one or more outputs |
Off(OUT_C) |
Float(outputs) |
Let the outputs float |
Float(OUT_B) |
Fwd(outputs) |
Set outputs to forward direction |
Fwd(OUT_A) |
Rev(outputs) |
Set outputs to reverse direction |
Rev(OUT_B) |
Toggle(outputs) |
Flip the direction of outputs |
Toggle(OUT_C) |
OnFwd(outputs) |
Turn on in forward direction |
OnFwd(OUT_A) |
OnRev(outputs) |
Turn on in reverse direction |
OnRev(OUT_B) |
OnFor(outputs, time) |
Turn on for specified number of 100ths of a second. Time may be an expression. |
OnFor(OUT_A, x) |
SetOutput(outputs, mode) |
Set output mode |
SetOutput(OUT_A, OUT_ON) |
SetDirection(outputs, dir) |
Set output direction |
SetDirection(OUT_A, OUT_FWD) |
SetPower(outputs, power) |
Set output power level (0-7). Power may be an expression. |
SetPower(OUT_A, x) |
Wait(time) |
Wait for the specified amount of time in 100ths of a second. Time may be an expression. |
Wait(x) |
PlaySound(sound) |
Play the specified sound (0-5). |
PlaySound(SOUND_CLICK) |
PlayTone(freq, duration) |
Play a tone of the specified frequency for the specified amount of time (in 10ths of a second) |
PlayTone(440, 5) |
ClearTimer(timer) |
Reset timer (0-3) to value 0 |
ClearTimer(0) |
StopAllTasks() |
Stop all currently running tasks |
StopAllTasks() |
SelectDisplay(mode) |
Select one of 7 display modes: 0: system watch, 1-3: sensor value, 4-6: output setting. Mode may be an expression. |
SelectDisplay(1) |
SendMessage(message) |
Send an IR message (1-255). Message may be an expression. |
SendMessage(x) |
ClearMessage() |
Clear the IR message buffer |
ClearMessage() |
CreateDatalog(size) |
Create a new datalog of the given size |
CreateDatalog(100) |
AddToDatalog(value) |
Add a value to the datalog. The value may be an expression. |
AddToDatalog(Timer(0)) |
SetWatch(hours, minutes) |
Set the system watch value |
SetWatch(1,30) |
SetTxPower(hi_lo) |
Set the infrared transmitter power level to low or high power |
SetTxPower(TX_POWER_LO) |
RCX Constants
Many of the values for RCX functions have named constants that can help make code more readable. Where possible, use a named constant rather than a raw value.
Sensor configurations for SetSensor() |
SENSOR_TOUCH, SENSOR_LIGHT, SENSOR_ROTATION, SENSOR_CELSIUS, SENSOR_FAHRENHEIT, SENSOR_PULSE, SENSOR_EDGE |
Modes for SetSensorMode() |
SENSOR_MODE_RAW, SENSOR_MODE_BOOL, SENSOR_MODE_EDGE, SENSOR_MODE_PULSE, SENSOR_MODE_PERCENT, SENSOR_MODE_CELSIUS, SENSOR_MODE_FAHRENHEIT, SENSOR_MODE_ROTATION |
Types for SetSensorType() |
SENSOR_TYPE_TOUCH, SENSOR_TYPE_TEMPERATURE, SENSOR_TYPE_LIGHT, SENSOR_TYPE_ROTATION |
Outputs for On(), Off(), etc. |
OUT_A, OUT_B, OUT_C |
Modes for SetOutput() |
OUT_ON, OUT_OFF, OUT_FLOAT |
Directions for SetDirection() |
OUT_FWD, OUT_REV, OUT_TOGGLE |
Output power for SetPower() |
OUT_LOW, OUT_HALF, OUT_FULL |
Sounds for PlaySound() |
SOUND_CLICK, SOUND_DOUBLE_BEEP, SOUND_DOWN, SOUND_UP, SOUND_LOW_BEEP, SOUND_FAST_UP |
Modes for SelectDisplay() |
DISPLAY_WATCH, DISPLAY_SENSOR_1, DISPLAY_SENSOR_2, DISPLAY_SENSOR_3, DISPLAY_OUT_A, DISPLAY_OUT_B, DISPLAY_OUT_C |
Tx power level for SetTxPower() |
TX_POWER_LO, TX_POWER_HI |
Keywords
Keywords are those words reserved by the NQC compiler for the language itself. It is an error to use any of these as the names of functions, tasks, or variables.
__sensor
__type
abs
asm
break
case
const
continue
default
do
else
false
if
inline
int
repeat
return
sign
start
stop
sub
switch
task
true
void
while