Control Structures

Revision as of 21:52, 20 December 2011 by Bfeher (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Main >> NXT >> Functions >> Control Structures


if(expression){ statement1 } else { statement2 }
In the if statement, if the expression in parentheses is nonzero (true), control passes to statement 1. If the else clause is present and the expresssion is zero (false), control will pass to statement 2. The "else" part is optional, and if absent, a false expression will simply result in skipping over the statement 1. An else always matches the nearest previous unmatched if; braces may be used to override this when necessary, or for clarity.

if(SensorValue(touch1) == 1)                       // if the touch sensor 'touch1' reads '1' (pressed):
  nxtDisplayCenteredTextLine(2, "Touch pressed");    // display "Touch pressed" to the VEX LCD
else                                               // else (the touch sensor 'touch1' reads '0' [unpressed]):
  nxtDisplayCenteredTextLine(2, "Touch unpressed");  // display "Touch unpressed" to the LCD


word nNxtButtonTask
The variable serves two purposes. A negative value (default is -1) indicates that the standard firmware processing of NXT buttons should be used. Values in the range of 0 or higher indicates that application program will process buttons. If the value is in the range 0 to 9 (i.e. a valid task number) then this task will be started (or restarted) whenever a button press is detected.

nNxtButtonTask = 2;  // restart task 2 every time a button is pressed


word nNxtExitClicks
Holds the number of 'clicks' of the EXIT button required to abort a program. Default value is one for compatibility with standard firmware. This variable allows end user programs to use the EXIT button. If you set the variable to 'N' (where 'N' is greater than 1) then the first 'N-1' consecutive clicks of the EXIT button can be handled by the application program. On the N-th consecutive click the firmware will automatically EXIT (i.e. end) your program. Having the firmware perform the check ensures that, even if there is an error in your program, you can always EXIT your program. But it also easily allows your program to have access to the EXIT button. NOTE: With the standard firmware, the EXIT button is not accessible to your program!

nNxtExitClicks = 3;  // triple clicking EXIT button will terminate program