conditional-operator

Bash if [ false ] ; returns true

亡梦爱人 提交于 2019-11-28 15:32:11
问题 Been learning bash this week and ran into a snag. #!/bin/sh if [ false ]; then echo "True" else echo "False" fi This will always output True even though the condition would seem to indicate otherwise. If I remove the brackets [] then it works, but I do not understand why. 回答1: You are running the [ (aka test ) command with the argument "false", not running the command false . Since "false" is a non-empty string, the test command always succeeds. To actually run the command, drop the [ command

Ruby ternary operator without else

倾然丶 夕夏残阳落幕 提交于 2019-11-28 15:01:00
问题 Is there a ruby idiom for "If do-this," and "do-this" just as a simple command? for example, I'm currently doing object.method ? a.action : nil to leave the else clause empty, but I feel like there's probably a more idiomatic way of doing this that doesn't involve having to specify a nil at the end. (and alternatively, I feel like taking up multiple lines of code would be wasteful in this case. 回答1: a.action if object.method? 回答2: As a general rule: you pretty much never need the ternary

Using conditional operator in h:inputText value and h:commandButton actionListener

与世无争的帅哥 提交于 2019-11-28 12:23:50
I want to load two difference panel in .xhtml file. <h:inputText value="#{param['from']=='TERMINAL' ? terminalsList.globalFilter : merchantsList.globalFilter}" size="50" /> <h:commandButton value="Filter" actionListener="#{param['from']=='TERMINAL' ? terminalsList.filterTerminals : merchantsList.filterMerchants}" /> <h:commandButton value="Reset" actionListener="#{param['from']=='TERMINAL' ? terminalsList.resetTerminalsFilter : merchantsList.resetMerchantsFilter}" /> when http get request params equals 'TERMINAL' i want to load 'terminalsList' managed bean, else 'merchantsList' managed bean.

Coalesce operator and Conditional operator in VB.NET [duplicate]

元气小坏坏 提交于 2019-11-28 11:57:21
Possible Duplicate: Is there a conditional ternary operator in VB.NET? Hi guys, Can we use Coalesce operator(??) and conditional ternary operator(:) in VB.NET as in C#? I think you can get close with using an inline if statement: //C# int x = a ? b : c; 'VB.Net Dim x as Integer = If(a, b, c) Sub Main() Dim x, z As Object Dim y As Nullable(Of Integer) z = "1243" Dim c As Object = Coalesce(x, y, z) End Sub Private Function Coalesce(ByVal ParamArray x As Object()) Return x.First(Function(y) Not IsNothing(y)) End Function just for reference, Coalesce operator for String Private Function Coalesce

Short circuiting statement evaluation — is this guaranteed? [C#]

╄→гoц情女王★ 提交于 2019-11-28 10:50:28
Quick question here about short-circuiting statements in C#. With an if statement like this: if (MyObject.MyArray.Count == 0 || MyObject.MyArray[0].SomeValue == 0) { //.... } Is it guaranteed that evaluation will stop after the "MyArray.Count" portion, provided that portion is true? Otherwise I'll get a null exception in the second part. Yes, this is guaranteed. C# Language Specification - 7.11 Conditional logical operators : The && and || operators are called the conditional logical operators. They are also called the "short-circuiting" logical operators. Therefore they will support logical

Check if int is between two numbers

寵の児 提交于 2019-11-28 09:37:46
Why can't do you this if you try to find out whether an int is between to numbers: if(10 < x < 20) Instead of it, you'll have to do if(10<x && x<20) which seems like a bit of overhead. Stephen C One problem is that a ternary relational construct would introduce serious parser problems: <expr> ::= <expr> <rel-op> <expr> | ... | <expr> <rel-op> <expr> <rel-op> <expr> When you try to express a grammar with those productions using a typical PGS, you'll find that there is a shift-reduce conflict at the point of the first <rel-op> . The parse needs to lookahead an arbitrary number of symbols to see

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

C# conditional operator error Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement

烂漫一生 提交于 2019-11-28 07:40:27
问题 Hi I was writing a basic program to find if the input number is prime or not. I have a checkPrime(num) function that checks for prime number and returns true if num is prime else returns false . Now in my main() function I used conditional operator to shorten the code but I am getting an error which I am not sure about. Below is my code : static void Main(String[] args) { int n = Int32.Parse(Console.ReadLine()); while (n-- > 0) { int num = Int32.Parse(Console.ReadLine()); (checkPrime(num) ==

if/else vs ternary operator

白昼怎懂夜的黑 提交于 2019-11-28 01:59:58
Considering the evaluation time, are following two equivalent? if(condition1) { //code1 } else { //code2 } condition1 ? code1 : code2 Or they are just syntactically different? The difference is that the latter station can be used to return a value based on a condition. For example, if you have a following statement: if (SomeCondition()) { text = "Yes"; } else { text = "No"; } Using a ternary operator, you will write: text = SomeCondition() ? "Yes" : "No"; Note how the first example executes a statement based on a condition, while the second one returns a value based on a condition. Well ... In

Conditional operator used in cout statement

*爱你&永不变心* 提交于 2019-11-28 01:51:58
By trying, I came to know that it is necessary to put parentheses around a conditional operator in a cout statement. Here a small example: #include <iostream> int main() { int a = 5; float b = (a!=0) ? 42.0f : -42.0f; // works fine std::cout << b << std::endl; // works also fine std::cout << ( (a != 0) ? 42.0f : -42.0f ) << std::endl; // does not work fine std::cout << (a != 0) ? 42.0f : -42.0f; return 0; } The output is: 42 42 1 Why are these parentheses necessary? The resulting type of the conditional operator is known in both cases, isn't it? The ?: operator has lower precedence than the <<