Remembering the Ternary Operator Syntax - ternary-operator

Anyone have a good trick to remember the standard ternary syntax?
Specifically whether the '?' or ':' comes first. I have consistently gotten this backwards over the years.

The condition you are checking is kind of like a question, so the question mark comes first.
x > 0 ? 1 : 0
Think of this statement as three English sentences: "Is x greater than 0? Then 1. Else, 0." You have one sentence for each clause in the statement.
The predicate:
x > 0 ? /* Is x greater than 0? */
The "true" branch:
1 /* Then 1. */
The "false" branch:
: 0 /* Else, 0. */

As far as remembering which symbol comes first, I just think of the fact that the first part is a question, "Is it true or not?", so the question mark goes first.
I think of the syntax in this manner
Question ? Yes : No

in python I read it as a normal English sentence:
a equals b if condition is true else c

Think of it this way: a ternary statement consists of three parts: the question, the code to execute if the answer to the question is "yes" and the code if the answer is "no". The "?" comes after the question like it does in English sentences.

"?" is a question mark so it means "if".
A colon means, "now it comes", "then do".
The good thing about the ternary operator is that you are not forced to use it, especially if you are having problems remembering the syntax. Just use an if-statement which is more readable most times.
And no - the ternary has no better performace then an if-statement.

It goes like this:
myVariable = this.testMethod() ? 'value for true case' : 'value for false case'

If you're unit tests still pass when you get it wrong, then either it doesn't matter or your tests aren't covering enough of the paths through the code. If there's too long a gap between typing it and getting a pass/fail from the tests, that's another issue. Very few little syntax nits matter in the presence of good, fast tests.

Related

Concatenation Not Working

I'm sure I'm missing something simple and would like a little help. My delay codes are always 3 digits. Two letters a dash (-) and a number. I am trying to use a single line of code to detect either MT or DA, the actual classification number is irrelevant, so I want the message box to fire on the two letters only. The code looks right, but it doesn't fire as it should. If I take out the wild card it works. I think I have a problem with the concatenation, but I'm not sure. I tried putting () brackets around it but that was not help. Additionally I tried using an or statement to capture the MT code on the other side but got nothing but an error code for type mismatch. Any ideas?
If Range("L24").Value = "DA" & "*" Then
MsgBox "The flight had a Maintenance delay"
Else
End If
A simple solution to this kind of problem would be to ignore the wildcard altogether and check the first two digits:
If Left(Range("L24").Value, 2) = "DA" Then

Syntax Error on TI-84 Plus

I get a syntax error when I run the following code:
Prompt A,B,C
B^2-4*A*C→Δ
If Δ IS<(0)
Disp "No Real Solutions"
If Δ=0
Disp "One Solution",-B/(2*A)
If Δ IS>(0)
Then
(-B-√(Δ))/(2*A)→E
(-B+√(Δ))/(2*A)→F
End
Any problems With this code?
Your issue is with using
If Δ IS<(0)
The command IS< does not test for less than. Instead, it takes a variable and a value as parameters, increments the variable, and skips the next line of code if the variable is less than the value. Instead, you want to do If Δ < 0 as said by JFed-9.
Also, the delta may be an issue, but try the above first.
I've never seen the 'Δ' symbol on the TI-84 Plus, maybe that could be the problem, but if not, I'm willing to bet that the third line is the issue.
If Δ IS<(0)
is not correct. You should replace it with
If Δ < 0
That should work for you. Other than that, you should be good! Nice starter program by the way!
Try this:
Prompt A,B,C
B²-4AC
If Ans<0
Disp "No Real Solutions
If not(Ans
Disp "One Solution",-B/2/A
If D>0
Then
(-√(D)-B)/2/A→E
(√(D)-B)/2/A→F
End

J string manipulation using only builtins

You are given a string like ))()(())(, and you wish to remove all instances of () from the string, which in this case means these two instances:
))()(())(
^^ ^^
leaving only ))()(.
I know you can use the library function stringreplace, or you could load up a regex library, but I want to know is if there is a concise way of accomplishing this the the J builtin operators exclusively?
I should clarify that my own solution was:
#~(-.#+._1&|.)#('()'&E.)
which I consider verbose -- so any similar solutions would not qualify as "concise" in my book. I'm really asking if there is a way to use a builtin (or maybe a simple combination of 2) to solve this directly. I expect this answer is no.
I think you are right that there is no ultra-concise way of expressing the operation you want to perform using just J primitives. The version I came up was very much like the one Dan, suggested above.
However given that a built in library verb rplc (based on stringreplace) performs exactly the operation you are after, I'm not sure why it would be better to replace it with a primitive.
'))()(())(' rplc '()';''
))()(
Having said that, if you can come up with a compelling case, then there is probably no reason it couldn't be added.
Not sure how concise it is, but I think that this will work:
deparen=. (-.#:(+/)#:(_1&|. ,: ])#:E. # ])
'()' deparen '))()(())('
))()(
Essentially the work is done by -. #: (+/) #: (_1&|. ,: ] )#:E. to create a bit string that removes the '()' instances using # (Copy) on the right argument.
E. identifies the positions of '()' using a bit string. Shift and laminate to get positions of '(' and ')', add them together to have 1 1 in the string where ever there is a '()' and then negate so these positions become 0 0 and are removed using Copy

One line IF condition in PL/SQL

It's possible to write one line if's in pl/sql? I'm just curious.
I want to write this snippet:
IF ( i.DECISAO_AT = 'S')
THEN 'PA'
ELSE 'I'
END IF;
And I want to know if it's possible to write it in one line, just like java. Like this:
IF ( i.DECISAO_AT = 'S') ? 'PA' : 'I' ;
Thanks!
You can write an IF ... ELSE ... END IF; on one line as others have shown; but no, you cannot do what you suggested and write it "just like Java":
IF ( i.DECISAO_AT = 'S') ? 'PA' : 'I' ;
PL/SQL does not understand the Java ? and : syntax, and does not have its own ternary operators as such. You can only use what is described in the documentation. The closest thing to what I think you're asking, for this particular statement anyway, is probably a case:
CASE WHEN i.DECISAO_AT = 'S' THEN 'PA' ELSE 'I' END
Or maybe a decode, as xQbert already suggested in a comment. Neither is an "IF" any more though.
There is no problem in PL/SQL executing the code if it was in one line or several lines . What it matters the syntax is correct.
You could use the DECODE statement that you can use inline:
The syntax for the DECODE function in Oracle/PLSQL is:
DECODE( expression , search , result [, search , result]... [, default] )
Sure you can, but down the road when you need to debug that if with several tests it will be a pain. Keep it on separate lines, thus easier to read, debug, comment and easier (and thus cheaper) for maintenance down the road. The next guy to work on it will thank you.
I read in a book (I think it was "C elements of style") once: "beware of clever, clever kills"). Keep it simple and easy. Code for the guy after you that has to maintain your code.

Math formula using a ternary operator causes error

I am using a ternary operator as part of a mathematical formula. My intention is to reduce the cost of an item by 1.5% if its condition is "poor." Below is the formula:
<c:out value="${usedEquip.cost * ( 1 - ("poor" == usedEquipItem.condition ? 0.015 : 0) ) }" />
The file is a JSP running in Tomcat. This line of code produces an error which says "equal symbol expected." I suspect that the problem is something other than a missing equal sign. Can anyone see what my problem is here? Is there a better way to do this? Thank you very much!
Replace your "poor" with 'poor', or escape the character by doing \"poor\".
You already have quotation mark in your value (value=""), including another quotation mark will not work in jstl.
Also you can also replace == with eq

Resources