ternary-operator Questions

4

Solved

I've used ternary operators for a while and was wondering if there was a method to let say call a function without the else clause. Example: if (isset($foo)) { callFunction(); } else { } Now o...
Disseise asked 25/1, 2013 at 11:31

10

I have been using a ternary operator in JavaScript to modify the value of an object based on user input. I have the following code, which runs as it should: var inputOneAns = inputOne == "Yes" ? "...

5

Solved

I see a lot of: var something = (is_something_true()) ? 3 : 4; in javascript. Is this faster than var something; if (is_something_true()) { something = 3; } else { something = 4; } Or is it...
Lette asked 6/7, 2012 at 20:4

6

Solved

Is this: int val; // ... val = (val != 0) ? otherVal : 0; less efficient than this: int val; //... if (val != 0) val = otherVal; ? Are compiler able to optimize the ternary operator? The i...
Mulberry asked 22/5, 2013 at 16:6

14

Solved

Is it possible to do something like this in JavaScript? max = (max < b) ? b; In other words, assign value only if the condition is true. If the condition is false, do nothing (no assignment)....
Linearity asked 21/2, 2013 at 18:2

13

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
Mackenie asked 8/4, 2011 at 12:18

18

Solved

I have been working with Java a couple of years, but up until recently I haven't run across this construct: int count = isHere ? getHereCount(index) : getAwayCount(index); This is probably a ver...
Villanelle asked 28/4, 2009 at 15:28

13

Solved

Please demonstrate how the ternary operator works with a regular if/else block. Example: Boolean isValueBig = value > 100 ? true : false; Exact Duplicate: How do I use the ternary operator?...
Dicho asked 20/1, 2009 at 21:18

14

From what I know, PowerShell doesn't seem to have a built-in expression for the so-called ternary operator. For example, in the C language, which supports the ternary operator, I could write somet...
Danby asked 10/7, 2015 at 13:26

17

Solved

How can I use an inline if statement in JavaScript? Is there an inline else statement too? Something like this: var a = 2; var b = 3; if(a < b) { // do something }

8

Apologies if this has been asked before, but I couldn't see it anywhere. Essentially I've come across a scenario where i need to make use of an if statement inside a lambda function. What ma...

7

Solved

Checkstyle complains about the following: return (null == a ? a : new A()); and says the parens are unnecessary. While the statement certainly works fine without them, it seems far more readab...
Piton asked 1/9, 2010 at 17:48

4

Solved

Firstly is there a name for this expression ? Javascript var value = false || 0 || '' || !1 || 'string' || 'wont get this far'; value equals string (string) aka the fifth option PHP $value =...
Isochroous asked 6/4, 2016 at 12:1

11

Solved

In VBA I can do the following: A = B + IIF(C>0, C, 0) so that if C>0 I get A=B+C and C<=0 I get A=B Is there an operator or function that will let me do these conditionals inline in MATLA...
Certain asked 30/1, 2013 at 19:36

9

Solved

I came across this problem that has Ternary expression (a?b:c) and needs the ternary expression to be converted into a Binary tree structure. a?b:c a / \ b c a?b?c:d:e a / \ b e / \ ...
Metropolitan asked 12/2, 2015 at 21:10

7

I'm a fan if the short if-version, example: ($thisVar == $thatVar ? doThis() : doThat()); I'd like to cut out the else-statement though, example: ($thisVar == $thatVar ? doThis()); However, i...
Turbellarian asked 14/2, 2018 at 0:10

3

Solved

I've been searching and testing for some time and just can't find if what I am trying to accomplish is possible the way I'm going about it. I would like to add a key/value pair to an array, when d...
Dishman asked 7/2, 2015 at 4:30

4

Solved

In shell scripting, I am using ternary operator like this: (( numVar == numVal ? (resVar=1) : (resVar=0) )) I watched shell scripting tutorial by Derek Banas and got the above syntax at 41:00 of...
Grijalva asked 31/12, 2017 at 10:50

2

Solved

For context, please read this question about Ternary Operators first. I am building my own programming language that allows you to define custom operators. Because I want it to have as few compile...
Jotham asked 31/3, 2016 at 11:24

3

Solved

I have a function that checks whether a value is found in array. I want to return a true or false. Current code works but throws and js-standerd/es-lint error "Unnecessary use of boolean literals i...
Melanimelania asked 22/4, 2018 at 22:25

14

Solved

In C/C++ (and many languages of that family), a common idiom to declare and initialize a variable depending on a condition uses the ternary conditional operator : int index = val > 0 ? val : -v...
Printmaking asked 14/11, 2013 at 13:39

7

Looking for a ternary operator for blade templates @if(Auth::check()) ? yes : no @endif Can't seem to get it to work this works @if(Auth::check()) yes @else no @endif suppose there is not muc...
Decapolis asked 13/8, 2014 at 10:57

5

Solved

With the Python (2.7) ternary expression x if cond else y what is the logical order when evaluating multiple expressions of these nested in order: e.g. 1 if A else 2 if B else 3 Drawing out the ...
Violone asked 19/6, 2017 at 17:24

10

how to use ternary if else with two or more condition using "OR" and "AND" like if(foo == 1 || foo == 2) { do something } { else do something } i want to use it like foo == 1 || foo =...
Preordain asked 7/2, 2019 at 6:8

9

Why the ternary operator does not have blocks? In other words, why the following code does not work and reports error for {} braces? int main() { int i = 1; (i==1)?{printf("Hello\n")}:{printf("W...
Dyewood asked 8/7, 2015 at 18:26

© 2022 - 2024 — McMap. All rights reserved.