Tertiary operators
(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 /> | ||
| − | + | A ternary operation will look something like this:<br /> | |
{| | {| | ||
|- | |- | ||
|<syntaxhighlight lang="ROBOTC"> | |<syntaxhighlight lang="ROBOTC"> | ||
| − | + | value = (boolean statement) ? (value if statement is true) : (value if statement is false); | |
</syntaxhighlight> | </syntaxhighlight> | ||
|}<br /> | |}<br /> | ||
| − | + | So in a real world example:<br /> | |
{| | {| | ||
|- | |- | ||
|<syntaxhighlight lang="ROBOTC"> | |<syntaxhighlight lang="ROBOTC"> | ||
| − | + | motor[port2] = abs(vexRT[Ch2]) > 10 ? vexRT[Ch2] : 0; | |
</syntaxhighlight> | </syntaxhighlight> | ||
|}<br /> | |}<br /> | ||
| − | + | 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= | + | {{tip-from-author |
| + | |name=magicode | ||
| + | |link=http://www.vexforum.com/showpost.php?p=220747&postcount=2 | ||
| + | }} | ||
Revision as of 13:15, 15 May 2012
General Programming → Programming 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.