ternary-operator

ternary operator in matlab

自作多情 提交于 2019-11-28 08:08:27
is there a way of typing for if like: var = (cond) ? true : false; or do we have to use this format? if (cond) true else false end MatLab doesn't have a ternary operator, or any other syntactic sugar for one-line if-statements. But if your if-statement is really simple, you could just write it in one line anyway: if (cond); casetrue(); else; casefalse(); end It's not as simple as ternary operator, but still better than writing it in 5 lines of code. If you only need true or false, you can do what MatlabSorter suggests. In case you want a real tertiary operator (i.e. a = b ? c : d ), there is

If without else ternary operator

寵の児 提交于 2019-11-28 07:56:19
So far from I have been searching through the net, the statement always have if and else condition such as a ? b : c . I would like to know whether the if ternary statement can be used without else . Assuming i have the following code, i wish to close the PreparedStatement if it is not null (I am using Java programming language.) PreparedStatement pstmt; //.... (pstmt!=null) ? pstmt.close : <do nothing>; frankie liuzzi No, you cannot do that. Instead try this: if(bool1 && bool2) voidFunc1(); Just write it out? if(pstmt != null) pstmt.close(); It's the exact same length. Why using ternary

Compilation error with generics and ternary operator in JDK 7

馋奶兔 提交于 2019-11-28 07:34:37
问题 I ran into a compilation failure while writing some Java code, which I distilled down to the following test case: import java.util.Collections; import java.util.List; public class TernaryFailure { public static List<String> thisWorks() { return Collections.emptyList(); } public static List<String> thisFailsToCompile() { return true ? Collections.emptyList() : Collections.emptyList(); } } The code above fails to compile with javac with JDK 1.7.0_45: $ javac TernaryFailure.java TernaryFailure

Does the VB.NET “If” operator cause boxing?

僤鯓⒐⒋嵵緔 提交于 2019-11-28 07:00:07
问题 Those of us who've worked in VB/VB.NET have seen code similar to this abomination: Dim name As String = IIf(obj Is Nothing, "", obj.Name) I say "abomination" for three simple reasons: IIf is a function , all of whose parameters are evaluated; hence if obj is nothing in the above call then a NullReferenceException will be thrown. This is unexpected behavior for someone who's accustomed to short-circuited ternary operators in languages like C#. Because IIf is a function, it thus incurs the

Multiple Ternary Operators

蹲街弑〆低调 提交于 2019-11-28 05:49:12
I need a bit of syntax help with a ternary operator which will help me to put the correct marker icons on to my good map.I have three areas 0,1 and 2 which have unique icons 0, 1 and 2. I used to have just two areas so this ternary operator worked fine; icon: (area == 1) ? icon1: icon0, Now I need to add an additional third icon (icon2) for area2. I've tried various methods but just can't seem to get it right. The syntax would be: icon: (area == 1) ? icon1 : (area == 2) ? icon2 : icon0, But this is starting to get complicated. You may well be better off just creating a function to do this work

Ternary operator

泄露秘密 提交于 2019-11-28 05:20:44
问题 Is there any logical reason that will explain why in ternary optor both branches must have the same base type or be convertible to one? What is the problem in not having this rule? Why on earth I can't do thing like this (it is not the best example, but clarifies what I mean): int var = 0; void left(); int right(); var ? left() : right(); 回答1: Expressions must have a type known at compile time. You can't have expressions of type "either X or Y", it has to be one or the other. Consider this

Understanding C# compilation error with ternary operator

人走茶凉 提交于 2019-11-28 05:06:03
问题 I have copied the following code from Wrox Professional ASP.NET 4.0 MVC 4 book, page 179 (Chapter "Understanding the Security Vectors in a Web Application") with the little modification of making it protected and storing as utility method in my abstract application-wide Controller protected ActionResult RedirectToLocal(string returnUrl) { if (Url.IsLocalUrl(returnUrl)) { return Redirect(returnUrl); } else { return RedirectToAction("Index", "Home"); } } The code above is aimed at securing the

Array initialization with a ternary operator?

ⅰ亾dé卋堺 提交于 2019-11-28 03:23:41
问题 I don't have access to the C11 specification, therefore I can't investigate this bug. The following declaration rises an error during compilation: int why[2] = 1 == 1 ? {1,2} : {3,4}; The error is: expected expression before { and: expected expression before : 回答1: This is not valid C11. You can only initialize an array with an initializer-list not with an expression. int why[2] = { ... }; // initializer-list {} Moreover, 1 == 1 ? {1,2} : {3,4} is not a valid C expression because {1, 2} is

How to use ternary operator in C#

只愿长相守 提交于 2019-11-28 03:00:36
问题 int five = 5; when the variable five is equal to 5, write true when the variable five is not equal to 5, write false How do I write a statement for this in ASP.NET using C#? 回答1: int five = 5; string answer = five == 5 ? "true" : "false"; I see that you want to use this to write the values out in ASP.NET, the answer string will hold your desired value, use that as you please. 回答2: The ternary operator in just about every language works as an inline if statement: Console.WriteLine((five == 5)

Short form for Java if statement

自闭症网瘾萝莉.ら 提交于 2019-11-28 02:51:54
I know there is a way for writing a Java if statement in short form. if (city.getName() != null) { name = city.getName(); } else { name="N/A"; } Does anyone know how to write the short form for the above 5 lines into one line? Use the ternary operator: name = ((city.getName() == null) ? "N/A" : city.getName()); I think you have the conditions backwards - if it's null, you want the value to be "N/A". What if city is null? Your code *hits the bed in that case. I'd add another check: name = ((city == null) || (city.getName() == null) ? "N/A" : city.getName()); To avoid calling .getName() twice I