Difference between revisions of "Tertiary operators"

From ROBOTC API Guide
Jump to: navigation, search
(Created page with "{{DISPLAYTITLE: Tertiary Operators}} <yambe:breadcrumb self="Tertiary Operators">Programming_Tips_Tricks|Programming Tips Tricks</yambe:breadcrumb> <br /> If you have include...")
 
Line 3: Line 3:
 
<br />
 
<br />
  
If you have included your own library of functions, for example:<br />
+
A ternary operation will look something like this:<br />
 
{|
 
{|
 
|-
 
|-
 
|<syntaxhighlight lang="ROBOTC">
 
|<syntaxhighlight lang="ROBOTC">
#include "motorLib.c"
+
value = (boolean statement) ? (value if statement is true) : (value if statement is false);
 
</syntaxhighlight>
 
</syntaxhighlight>
 
|}<br />
 
|}<br />
  
You can suppress warnings about unused functions by including the line:<br />
+
So in a real world example:<br />
 
{|
 
{|
 
|-
 
|-
 
|<syntaxhighlight lang="ROBOTC">
 
|<syntaxhighlight lang="ROBOTC">
#pragma systemFile            // eliminates warning for "unreferenced" functions
+
motor[port2] = abs(vexRT[Ch2]) > 10 ? vexRT[Ch2] : 0;
 
</syntaxhighlight>
 
</syntaxhighlight>
 
|}<br />
 
|}<br />
  
in the top of that file.
+
In contrast to this, the if else statement would look like this:
<br />
+
{|
 +
|-
 +
|<syntaxhighlight lang="ROBOTC">
 +
if(abs(vexRT[Ch2]) > 10)){
 +
  motor[port2] = vexRT[Ch2];
 +
}else{
 +
  motor[port2] = 0;
 +
}
 +
</syntaxhighlight>
 +
|}<br />
  
 +
You can also nest ternary statements. For example, if you wanted to say: move the motor forward at the value of the joystick if it is positive and above the threshold value, but move the motor backward at 1/2 joystick value of it is negative and below the threshold value, then you can nest the ternary statements to make a if else if else statement.<br />
 +
{|
 +
|-
 +
|<syntaxhighlight lang="ROBOTC">
 +
motor[port2] = vexRT[Ch2] > 10 ? vexRT[Ch2] : vexRT[Ch2 < -10 ? (vexRT[Ch2] / 2) : 0;
 +
</syntaxhighlight>
 +
|}<br />
  
{{tip-from-author|name=jpearman|link=http://www.vexforum.com/showpost.php?p=220744&postcount=1}}
+
{{tip-from-author
 +
|name=magicode
 +
|link=http://www.vexforum.com/showpost.php?p=220747&postcount=2
 +
}}

Revision as of 20:15, 15 May 2012

General ProgrammingProgramming Tips Tricks → Tertiary Operators


A ternary operation will look something like this:

value = (boolean statement) ? (value if statement is true) : (value if statement is false);

So in a real world example:

motor[port2] = abs(vexRT[Ch2]) > 10 ? vexRT[Ch2] : 0;

In contrast to this, the if else statement would look like this:

if(abs(vexRT[Ch2]) > 10)){
  motor[port2] = vexRT[Ch2];
}else{
  motor[port2] = 0;
}

You can also nest ternary statements. For example, if you wanted to say: move the motor forward at the value of the joystick if it is positive and above the threshold value, but move the motor backward at 1/2 joystick value of it is negative and below the threshold value, then you can nest the ternary statements to make a if else if else statement.

motor[port2] = vexRT[Ch2] > 10 ? vexRT[Ch2] : vexRT[Ch2 < -10 ? (vexRT[Ch2] / 2) : 0;

This tip was posted by magicode over at http://www.vexforum.com/showpost.php?p=220747&postcount=2.