ternary-operator

Ternary operator to return value- Java/Android

有些话、适合烂在心里 提交于 2020-01-22 13:22:27
问题 Just switched to Java from php I encountered following issue I want to rewrite if(usrname.equals(username) && (passwd.equals(password))){ return true; } else{ return false; } as (usrname.equals(username) && passwd.equals(password) )? return true : return false; it is not working(syntax error) however, int a=1; int b=2; int minVal = a < b ? a : b; is working Why ternary operator are not behaving correctly while returning value depending on some condition EDIT return (usrname.equals(username) &

Ternary operator to return value- Java/Android

帅比萌擦擦* 提交于 2020-01-22 13:20:07
问题 Just switched to Java from php I encountered following issue I want to rewrite if(usrname.equals(username) && (passwd.equals(password))){ return true; } else{ return false; } as (usrname.equals(username) && passwd.equals(password) )? return true : return false; it is not working(syntax error) however, int a=1; int b=2; int minVal = a < b ? a : b; is working Why ternary operator are not behaving correctly while returning value depending on some condition EDIT return (usrname.equals(username) &

Does the ternary “?: operator” have a bug in C#?

雨燕双飞 提交于 2020-01-15 04:26:06
问题 Why can not I run the following code ? static int num = 0; static void Main(string[] args) { (num == 0) ? inc() : dec(); } public static void inc() { num++; } public static void dec() { num--; } Why doesn't C# allow me to use the ternary "?:" operator to check a condition and then run a method accordingly without the need to return any value? Equivalently to this: if (num == 0) inc(); else dec(); I am not sure if the same rule is applied in other languages, e.g., Java, C++, etc... 回答1: Jon

Python - Call a Function on a Condition

蓝咒 提交于 2020-01-14 14:47:08
问题 I was wondering if there is a concise way to call a function on a condition. I have this: if list_1 != []: some_dataframe_df = myfunction() I'm wondering if it is possible to this in a ternary operator or something similiar. If I do (some_dataframe_df = myfunction()) if list_1 != [] else pass It doesn't work. 回答1: Your code is fine. The only change I suggest is to use the inherent Truthness of non-empty lists: if list_1: some_dataframe_df = myfunction() If you wish to use a ternary statement

How to I execute multiple functions on the result of a ternary operation?

南笙酒味 提交于 2020-01-13 14:54:20
问题 I have an if/else statement that results in two functions being called if it evaluates as true. if (isTrue) { functionOne(); functionTwo(); } else { functionThree(); } I would like to be able to put that in a ternary statement like this: isTrue ? (functionOne(), functionTwo()) : functionThree(); Is this possible? 回答1: Your example is indeed valid javascript. You can use a comma to separate expressions, and wrap that in a single statement with parentheses for the ternary. var functionOne =

Is there a PHP like short version of the ternary operator in Java?

我的梦境 提交于 2020-01-13 10:43:48
问题 In PHP the ternary operator has a short version. expr1 ? expr2 : expr3; changes into expr1 ? : expr3; The short version returns the result of expr1 on true and expr3 on false. This allows cool code that can populate variables based on their own current state. For example: $employee = $employee ? : new Employee(); If $employee == null or evaluates to false for any other reason, the code above will create a new Employee(); Otherwise the value in $employee will be reassigned to itself. I was

Is there a PHP like short version of the ternary operator in Java?

有些话、适合烂在心里 提交于 2020-01-13 10:43:35
问题 In PHP the ternary operator has a short version. expr1 ? expr2 : expr3; changes into expr1 ? : expr3; The short version returns the result of expr1 on true and expr3 on false. This allows cool code that can populate variables based on their own current state. For example: $employee = $employee ? : new Employee(); If $employee == null or evaluates to false for any other reason, the code above will create a new Employee(); Otherwise the value in $employee will be reassigned to itself. I was

Why ternary operation gives nullpointer while its ifelse counterpart doesn't? [duplicate]

匆匆过客 提交于 2020-01-09 10:03:11
问题 This question already has answers here : Booleans, conditional operators and autoboxing (4 answers) Closed 5 years ago . I am getting NullPointerException in one instance below while its counterpart runs smooth. public static void main(String[] args){ System.out.println(withTernary(null, null)); //Null Pointer System.out.println(withIfElse(null, null)); //No Exception } private static Boolean withTernary(String val, Boolean defVal){ return val == null ? defVal : "true".equalsIgnoreCase(val);

Why ternary operation gives nullpointer while its ifelse counterpart doesn't? [duplicate]

こ雲淡風輕ζ 提交于 2020-01-09 10:02:43
问题 This question already has answers here : Booleans, conditional operators and autoboxing (4 answers) Closed 5 years ago . I am getting NullPointerException in one instance below while its counterpart runs smooth. public static void main(String[] args){ System.out.println(withTernary(null, null)); //Null Pointer System.out.println(withIfElse(null, null)); //No Exception } private static Boolean withTernary(String val, Boolean defVal){ return val == null ? defVal : "true".equalsIgnoreCase(val);

How ternary operator works?

本小妞迷上赌 提交于 2020-01-06 03:55:46
问题 In codewars I have completed a kata using for loop with 15 lines of code, some other person has completed it with just 7 lines. Could anybody explain the code? public class CamelCase { public static String cAmEl(final String yourName) { final int length = yourName.length(); final StringBuilder cAmEl = new StringBuilder(length); boolean upper = true; for (int i = 0; i < length; i++, upper ^= true) { final char c = yourName.charAt(i); cAmEl.append(upper ? toUpperCase(c) : toLowerCase(c)); }