ternary-operator

Why can't I use break in a C# ternary expression?

允我心安 提交于 2019-12-02 13:38:52
I am trying to convert the if else clause to a ternary within a while loop, however it's not allowing me to have a break after the question mark, pointing an error at the break as an invalid expression. How would I go about turning this simple if else into a ternary like so. while (true) { Console.WriteLine("Enter 3 words seperated by spaces: "); var input = Console.ReadLine(); //input == "" ? break : ConvertToPascal(input); if (input == "") break; else ConvertToPascal(input); } } It isn't possible using the ternary operator, but you can simplify your code structure as follows: string input;

javascript ternary operator opposed to if/else

北战南征 提交于 2019-12-02 10:28:24
I am trying to write this if/else statement using javascript's ternary operator syntax. Is it possible to write this as a ternary operator? function changePlayer() { if (currentPlayer === playerOne) { currentPlayer = playerTwo } else { currentPlayer = playerOne } }; My current attempt is: function changePlayer(){ currentPlayer === playerOne ? playerTwo : playerOne; } You just miss the assignment statement. So the final example will go like this: function changePlayer(){ currentPlayer = (currentPlayer === playerOne) ? playerTwo : playerOne; } The first argument of the ternary operator is the

Ternary operator with multiple operations

北城余情 提交于 2019-12-02 10:06:35
问题 Can I use a ternary operator when I have more than one operation to perform per case? For example can I use it here?: if (dwelling) { dwelling = dwelling[0].nodeValue; //first operation letterDwelling = dwelling[0].toUpperCase(); //second operation } else { dwelling = ""; letterDwelling = ""; } I've only used this syntax which allows one subsequent operation: dwelling = dwelling ? dwelling[0].nodeValue : ""; 回答1: Although i highly advice against it for the sake of readability and

Ternary operator in java vs c [duplicate]

夙愿已清 提交于 2019-12-02 09:54:34
问题 This question already has answers here : Why doesn't this method work? Java ternary operator (6 answers) Closed 5 years ago . Why does this ternary operator doesn't works over here but where as in c it works perfectly? import java.util.Scanner; class Pack { public static void main(String[] args) { System.out.println("enter a number"); Scanner s=new Scanner(System.in); int i=s.nextInt(); i%2==0?System.out.println("even"):System.out.println("odd"); } } 回答1: Because you can't assign a statement

ternary operator for clang's extended vectors

会有一股神秘感。 提交于 2019-12-02 07:26:59
I've tried playing with clang's extended vectors . The ternary operator is supposed to work, but it is not working for me. Example: int main() { using int4 = int __attribute__((ext_vector_type(4))); int4 a{0, 1, 3, 4}; int4 b{2, 1, 4, 5}; auto const r(a - b ? a : b); return 0; } Please provide examples on how I might make it work, like it works under OpenCL . I am using clang-3.4.2 . Error: t.cpp:8:16: error: value of type 'int __attribute__((ext_vector_type(4)))' is not contextually convertible to 'bool' auto const r(a - b ? a : b); ^~~~~ 1 error generated. You can loop over the elements

Ternary operator in java vs c [duplicate]

被刻印的时光 ゝ 提交于 2019-12-02 07:07:43
This question already has an answer here: Why doesn't this method work? Java ternary operator 6 answers Why does this ternary operator doesn't works over here but where as in c it works perfectly? import java.util.Scanner; class Pack { public static void main(String[] args) { System.out.println("enter a number"); Scanner s=new Scanner(System.in); int i=s.nextInt(); i%2==0?System.out.println("even"):System.out.println("odd"); } } Because you can't assign a statement like that in Java. Your ternary would work if you used it like, System.out.println(i%2==0 ? "even" : "odd"); Fundamentally, Java

Reference initialization in C++

二次信任 提交于 2019-12-02 06:48:44
问题 Greetings, everyone! Examining my own code, I came up to this interesting line: const CString &refStr = ( CheckCondition() ) ? _T("foo") : _T("bar"); Now I am completely at a loss, and cannot understand why it is legal. As far as I understand, const reference must be initialized, either with r-value or l-value. Uninitialized references cannot exist. But ()? operator executes a CheckCondition() function before it assigns value to the reference. I can see now, that while CheckCondition() is

Ternary operator with multiple operations

拥有回忆 提交于 2019-12-02 05:47:32
Can I use a ternary operator when I have more than one operation to perform per case? For example can I use it here?: if (dwelling) { dwelling = dwelling[0].nodeValue; //first operation letterDwelling = dwelling[0].toUpperCase(); //second operation } else { dwelling = ""; letterDwelling = ""; } I've only used this syntax which allows one subsequent operation: dwelling = dwelling ? dwelling[0].nodeValue : ""; Although i highly advice against it for the sake of readability and extensibility you could: dwelling ? (dwelling = dwelling[0].nodeValue, letterDwelling=dwelling[0].toUpperCase()) :

Syntax error with ternary operator

[亡魂溺海] 提交于 2019-12-02 05:47:06
问题 I'm new to Python and I'm trying to use ternary opertor which has this format (I think so) value_true if <test> else value_false Here's a snippet of code: expanded = set() while not someExpression: continue if currentState in expanded else expanded.push(currentState) # some code here But Python doesn't like it and says: SyntaxError: invalid syntax (pointed to if) How to fix it? 回答1: Ternary operation in python using for expression , not statements . Expression is something that has value.

Full if/else statement vs. Conditional Operator [duplicate]

被刻印的时光 ゝ 提交于 2019-12-02 05:34:35
问题 This question already has answers here : Closed 9 years ago . Possible Duplicates: Benefits of using the conditional ?: (ternary) operator Is the conditional operator slow? Hi all, I've got a pretty simple question regarding the different if/else statements. Apart from writing less code, are there any other benefits for using the conditional operator as opposed to the full if/else statement? Is there a performance increase, less compiled code, or anything else that would benefit me when using