FAQ Database Discussion Community


Javascript Syntax: AngularJS Filter

javascript,angularjs,syntax,filter,ternary-operator
I have found this excellent code for applying a filter in AngularJS here. It works as expected, although I'm a little unsure on what the shortening using ! ? and : in this context would replace. From experience, it looks like the ? and : are a ternary operators, although...

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

javascript,ternary-operator
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...

Incompatible operand types when using ternary conditional operator

c++,ternary-operator
This code: bool contains = std::find(indexes.begin(), indexes.end(), i) != indexes.end(); CardAbility* cardAbility = contains ? new CardAbilityBurn(i) : new CardAbilityEmpty; gives me the following error: Incompatible operand types CardAbilityBurn and CardAbilityEmpty However if I write the code like this: if (contains) { cardAbility = new CardAbilityBurn(i); } else { cardAbility...

One-line short-circuit evaluation with && || in JavaScript

javascript,ternary-operator,short-circuiting
var prefix = options && options.prefix || ''; In JavaScipt in my case. Can someone explain what kind of statement or condition is this? Or at the end what's the value of prefix variable? I know about(ternary operator): condition ? expr1 : expr2 but this was different....

Ternary operator in C vs C++ [duplicate]

c++,c,ternary-operator,conditional-operator
This question already has an answer here: Errors using ternary operator in c 4 answers There are a lot of differences between C and C++ and came to stuck on one of them The same code gives an error in C while just executes fine in C++ Please explain...

Showing link using Ternary Operator

java,angularjs,ternary-operator
How can this be achieved in partial html file ? {{x.stringData > 50 ? <a ng-click="showData(x)">...</a> | '' }} Obviously the above is not correct so its just for understanding that what I want to achieve....

Difference Between C and Python Ternary Operators [closed]

python,c,ternary-operator
Well I just read a SOF thread where I see many people are talking about Python's ternary operator. I didn't know that Python has a ternary operator (ternary operator or conditional expression whatever you feel comfort with) equivalent though. So the syntax of Python ternary operator is... play_golf if sun_shines...

Is it mandatory to enclose an entire ternary operator statement in parenthesis in this statement?

php,ternary-operator,parentheses,readability,code-readability
For example in the following statement, $class = ( is_array($tagClasses) ? 'class="'.implode(" ",$tagClasses).'"' : '' ); is the outermost parentheses only for readability or does it really have any other purpose? I think it is for readability and code-aesthetics only, unless you have other things happening in the same statement,...

When using Java ternary operator or Math functions, is the returned value always up to date?

java,math,ternary-operator
When using Java ternary operator or Math functions, is the returned value always up to date, or does the statement have to be reevaluated whenever I need an updated value? Considering: a = myBoolean? 1: 2; a = Math.min(x, y); Does the values of "a" would change dynamically like it...

Strange behavior: ternary operator for functions

python,function,ternary-operator
Here is a simplified example of my problem. I thought that these functions would have exactly the same behavior: def f1(l): if type(l[0][0])==list: f=lambda x:x[0][0] else: f=lambda x:x[0] l.sort(key=f,reverse=True) def f2(l): f=lambda x:x[0][0] if type(l[0][0])==list else lambda x:x[0] l.sort(key=f,reverse=True) l=[[1,2],[3,4]] But in reality f1(l) works fine when f2(l) collapses with...

Does RPGLE has a ternary operator?

ternary-operator,rpgle
I did find old posts on forums about this question. But as they were old, I was wondering if it changed. I know that, back maybe in 2004, RPG did not have ternary operator. Having work a bit in C# and Java, I did use them and I like them....

PHP: Using the ternary operator for something else than assignments – valid use case?

php,ternary-operator
Unfortunately I haven't found any official resource on this. Is it allowed to use the ternary operator like this, to shorten and if/else statement: (isset($someVar) ? $this->setMyVar('something') : $this->setMyVar('something else')); In the PHP documentation, the ternary operator is explained with this example: $action = (empty($_POST['action'])) ? 'standard' : $_POST['action']; This...

Why can't I use a “break” statement inside a ternary conditional statement in C++?

c++,data-structures,binary-tree,ternary-operator
Node is a very simple class with a just a constructor and a few variables: a "name" (actually just a char) and two child Node pointers called "left" and "right". I was just starting to write some code that needs to drop to the left-most node, and I was quite...

Ternary operator precedence and assignment

c++,c++11,ternary-operator
I have an unusual bug in my code, where sometimes, my code will return the wrong value. I have a ternary expression in a function which may be the culprit, and it's written like this; corpusBoard[i][j] = (piece == 'o') ? 'o' : 'x'; Which should state, if piece is...

PHP Ternary statement within Associative Array to set both key & value?

php,arrays,ternary-operator
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 defining the array, based on an ternary statement. Array( 'Item 1'...

Usage of ternary operator as placeholder

c,sorting,placeholder,ternary-operator,bubble-sort
I have a function bubble_sort_linkedList declared as, void bubble_sort_linkedList(node *head); The definition of the function (to sort in ascending order) goes like this : void bubble_sort_linkedList(node *head){ node *ptr, *lptr; int temp; lptr = NULL; ptr = head; if(ptr == NULL || ptr->next == NULL) return; while(ptr != lptr){ while(ptr->next...

Java Ternary operator syntax [duplicate]

java,syntax,ternary-operator
This question already has an answer here: Ternary operator, syntax error when using assignment 4 answers I have the following piece of code. This is how I understand it. In the first case, the ternary operator returns the value of y because x=4 and the print statement prints 5,...

Swift ternary syntax error

swift,ternary-operator
I used to program in Objective-C all the time and I am new to Swift. This error Xcode gives me really confuse me. func renderBufferAreaBAUp(yOffset: CGFloat, amount: CGFloat, ifLeft: Bool) { var topViewIndexForIndexAdjust = ifLeft?leftTopIndex:rightTopIndex } On this line I intended to use ternary. leftTopIndex and rightTopIndex are both Int...

How to convert a Ternary expression to a Binary tree structure?

binary-tree,ternary-operator,data-conversion
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 / \ c d My approach using a Binary tree implemented using a array :-...

How does the ternary operator evaluate the resulting datatype?

java,data-type-conversion,ternary-operator
Given this piece of code public class Main { public static void main(String[] args) { foo(1); foo("1"); foo(true?1:"1"); foo(false?1:"1"); } static void foo(int i){System.out.println("int");} static void foo(String s){System.out.println("String");} static void foo(Object o){System.out.println("Object");} } This is the output I get: int String Object Object I can't understand why in the last...

Why can't I replace if statement with conditional operator (?:)?

java,if-statement,conditional,ternary-operator
I've been struggling to turn private PlaneClass preferredClass; if (preferredClass == PlaneClass.FIRST_CLASS) preferredClass = PlaneClass.ECONOMY_CLASS; else preferredClass = PlaneClass.FIRST_CLASS; into preferredClass == PlaneClass.FIRST_CLASS ? preferredClass = PlaneClass.ECONOMY_CLASS : preferredClass = PlaneClass.FIRST_CLASS; The if-statement compiles. The conditional operator doesn't. (Error messages: 1. Type mismatch: cannot convert from PlaneClass to boolean 2....

Angular ternary operators

angularjs,ternary-operator
I'm attempting to use a ternary operator to set up a portion of my template: account.accountType === "" ? "" : "Type: " + account.accountType This code snippet should leave out the Type label if it is null. However, the type label is always present: Evaluates to true: Type: Evaluates...

Type of ternary expression

c++,ternary-operator,cout
Can anyone explain the output of the following program: #include <iostream> using namespace std; int main() { int test = 0; cout << "First character " << '1' << endl; cout << "Second character " << (test ? 3 : '1') << endl; return 0; } Output: First character 1...

The ternary operator in a one-line function

julia-lang,ternary-operator
In Julia, I might want to write a function that returns 0 if the input is less than 1, or returns 2 if the input is greater than or equal to 1. This is a pretty simple function, and the verbosity of a five-line if else construct is probably excessive....

Java ternary operator logic explanation

java,ternary-operator
Can someone please explain what this line of java code is doing? public String getTitleNavContainer(boolean isThisLandingPage) { return isThisLandingPage ? StringUtils.EMPTY : "title-nav-container"; } I can see what it is doing but I am wondering how I would modify this so that if it is landing page it does one...

Ternary Operator in For Loop causing infinite iterations

c,for-loop,infinite-loop,ternary-operator,operator-precedence
I was working on a function to transpose an NxN matrix which is stored in an array of floats. My first implementation seemed to cause the function to loop infinitely and I can't seem to figure out why. Here is the original code: for(int i = 0; i < numRows...

Understanding method's return statement syntax [duplicate]

java,ternary-operator,tic-tac-toe
This question already has an answer here: Java: boolean in println (Boolean? “print true”:“print false”) [duplicate] 11 answers There is a Tic Tac Toe game in Java that I am trying to understand the syntax of a method, but I don't get what the return statement is doing. Here...

Unexpected output printf statement [duplicate]

c,macros,printf,ternary-operator
This question already has an answer here: Why is “i” variable getting incremented twice in my program? 8 answers Why outputs of i and j in the following two printf()s are different? #include <cstdio> #define MAX(x,y) (x)>(y)?(x):(y) int main() { int i=10,j=5,k=0; k==MAX(i++,++j); printf("%d %d %d\n",i,j,k); i=10,j=5,k=0; k=MAX(i++,++j); printf("%d...

Android ternary operator

android,ternary-operator
I am trying to do a simple ternary operator but I have an error I can´t see. This is my code: public void setVisibility(boolean isVisible) { View adView = ((Activity) context).findViewById(R.id.adView); isVisible? adView.setVisibility(View.VISIBLE) : adView.setVisibility(View.GONE); } I get the error : Not a statement. Does someone know where is my...

Run 2 functions in Javascript inline with ternary operator

javascript,ternary-operator
I want to run a function and also check if element1 is enabled and if so to disabled it and element2 and the other way around. <input type="" onclick = "function(); element1.disabled==true ? (element1.disabled=false, element2.disabled=false ) : (element1.disabled=true, element2.disabled=true)" /> How do i make this code work ? Since you...

C comma in ternary statement

c,operator-keyword,ternary-operator,comma
int m = 5, d = 12, y = 1975, val; // May 12, 1975 Can someone please explain the function/purpose of the comma operator in the line of code below: val = (d+=m<3?y--:y-2,23*m/9+d+4+y/4-y/100+y/400)%7; The above line was written by Mike Keith to calculate the day of the week given...

understanding Complex inArray ternary operator

javascript,ternary-operator
I was just going through the inArray method code and came across the following :: inArray: function (elem, arr, i) { var len; if (arr) { if (indexOf) { return indexOf.call(arr, elem, i); } len = arr.length; i = i ? i < 0 ? Math.max(0, len + i) :...

How to check a single checkbox using ternary operator when multiple checkbox exist?

c#,linq,asp.net-mvc-4,ternary-operator
I do have the following HTML <td> <input type="checkbox" id="PrevPay" checked="@(item.BSBreakTPayType.Value==322 ? "checked":"")" /> </td> <td> <input type="checkbox" id="Fullpay" checked="@(item.BSBreakTPayType.Value==323 ? "checked" : "") "/> </td> <td> <input type="checkbox" id="Halfpay" checked="@(item.BSBreakTPayType.Value==324 ? "checked" : "")" /> </td> <td> <input type="checkbox" id="Nopay" checked="@(item.BSBreakTPayType.Value==325 ? "checked" : "")"/> </td> Now...

Why does using “0” with the ternary operator return the first value?

javascript,types,ternary-operator
I was playing around with JSconsole and found something strange. The value of "0" is false "0" == false => true The value of false when used in ternary returns the second value false ? 71 : 16 => 16 However the value "0" which equals false when used in...

assign and execute if/else conditions in single line

java,if-statement,ternary-operator
How can I do that for example, if block checks if the condition is true or false and then execute action depends on that condition? like, if (check true or false) if false do action if true do action Something like that do it all in the same line without...

i % 2 == 0 ? arr[i] = 0 : arr[i] = 1; Ternary operator error

c,arrays,ternary-operator,lvalue
About ternary operator. I was rewriting an if-else statement in C, using the more clean ternary operator. Here´s the code: #include <stdio.h> #include <stdlib.h> int main() { int arr[10]; int i; // for ( i = 0; i < 10; i++ ) // { // if ( i % 2...

Throw new exception in Ternary condition [duplicate]

java,ternary-operator
This question already has an answer here: Ternary Operators Java 5 answers I have this lines of code: List<Long> list = new ArrayList<>(); if (n < 0) throw new RuntimeException(); if (n == 0) return list; I want to use Ternary condition : return (n < 0) ? (throw...

Can logical operator be used with in ternary operators in PHP

php,ternary-operator,logical
Is this statement correct in PHP $abc = $x === "" || $y !== $z ? "true" : "false"; ...

c# Ternary operator

c#,if-statement,error-handling,ternary-operator
I am working on a an application that has no documentation (although the code is quite clear and well-written) and I am trying to write some useful technical documentation for the next guy who comes along after I am in a mental hospital. In the web-service method, if the call...

Having a strange behavior with Ternary Logic [duplicate]

php,ternary-operator
This question already has an answer here: The Ternary Operator in PHP [duplicate] 3 answers When I do - echo "Hello ". (true) ? 'World' : 'XYZ'; The output - World instead of Hello World. But when - $person = (true) ? 'World' : 'XYZ'; echo "Hello ".$person; The...

What is the type of “auto var = {condition} ? 1 : 1.0” in C++11? Is it double or int?

c++,c++11,types,auto,ternary-operator
In C++11 what are the types of x and y when I write this? int main() { auto x = true ? 1 : 1.0; auto y = false ? 1 : 1.0; std::cout << x << endl; std::cout << y << endl; return 0; } ...

Ternary operator when casting a variable

php,types,casting,ternary-operator,shorthand-if
While writing is_numeric($var) ? (Int)$var : (String)$var;, I was wondering if it could be possible to move the ternary operator to the part where I cast the variable: echo (is_numeric($var) ? Int : String)$var; Not to my surprise, it didn't work: PHP Parse error: syntax error, unexpected '$var' (T_VARIABLE) Is...

Ternary operators in Meteor

meteor,ternary-operator,meteorite,spacebars
I am using Meteoris for working on my application. I have this scenario in forms. This is an edit view and I am just showing two of the 17 fields I have here: <label for="name" class="control-label">Name *</label> <input type="text" id="name" value="{{profile.name}}" placeholder="Name" class="form-control"> <label for="acctType" class="control-label">Account Type</label> <select id="acctType" placeholder="Name"...

How to use ternary operator to change value of ng-model in AngularJS?

javascript,angularjs,ternary-operator,ng-class
I have one text field and one checkbox. If checkbox is true I want value from address field to become after the checkbox, if false the field should be empty. I tried ternary operator with ng-class and many more, but no luck. What would be the best and working solution...

Groovy: Ternary operator says that “” is null

groovy,null,ternary-operator
def nullString = "String is NOT null" def check = (nullString != null) ? nullString : "String is null" def check2 = nullString ? nullString : "String is null" def check3 = nullString ?: "String is null" println check println check2 println check3 Tell me if I'm wrong, but these...

NullPointerException throws when I use ternary operator [duplicate]

java,boolean,ternary-operator,boxing,unboxing
This question already has an answer here: NullPointerException through auto-boxing-behavior of Java ternary operator 3 answers I have the following return statement: public Boolean foo(String booleanString){ return ("true".equals(booleanString) ? true : ("false".equals(booleanString) ? false : null)); } when booleanString equal not true and not false I get the NullPointerException....

What's wrong with this strpos and ternary operator?

php,ternary-operator,strpos
strpos($mydata,'Εntity') === false) ? 'normal' : 'other' It returns always 'normal' even if $mydata="Entity: This data must work." ...

C# ternary operator List operator

c#,list,operator-keyword,ternary-operator
Any knows a quickest Ternary operation for this. label1.Text = Cclass.TestMe() .Where(t => t.GFName == (textBox1.Text == "" ? "GName" : textBox1.Text)) .First() .GFName == null ? "Nothing" : "Super"; I was trying to check if the List is empty return something. So the compiler will not throw a Exception...

Is this an acceptable use case of the ternary operator?

php,ternary-operator
I was using the ternary operator to perform a very simple, conditional action. It worked well, but my IDE (PHPStorm) reported it as an error. The error I got was: 'void' method 'performAction' result used The code I was using is this: ($this->doThis ? $class->performAction() : $class->performDifferentAction()); The error in...

Is it possible to use the ternary operator “?” to fill an array list in C/C++?

c++,c,ternary-operator
This is probably a dumb questions. I'm modifying a code developed by someone else. I need to particularize the value of some chars array based on a logic variable ThreeDim. I'm trying to do this without success. int VarNumber = ThreeDim==1 ? 3 : 2; const char* VarList [] =...

Use ternary operator to check for successful INSERT in database?

php,pdo,ternary-operator
Is it acceptable practice to use PHP's ternary operator to check for successful INSERT in database (or more generally, the return value of any function)? The PHP manual seems to be cautious on stacked ternary operators, and I have some embedded in my writeData function. #pseudocode variables to give context...

PHP - urlencoded() ampersand in url perceived as actual ampersand

php,parameter-passing,urlencode,ternary-operator
There is an dummy example PHP application which shows what causes me problems. <?PHP echo ((isset($_GET['P'])) ? print_r($_GET) : "<a href='http://example.com/a.php?P=" . urlencode('One & Two') . "'>One & Two</a>"); ?> If we visit page without P parameter page will output: http://example.com/One+%26+Two And that's fine, but if we visit the link,...

Weird behaviour of ternary conditional with boxed types in Java [duplicate]

java,ternary-operator
This question already has an answer here: Why does the ternary operator unexpectedly cast integers? 1 answer I had this piece of code in my application (simplified version): Object result; if (check) result = new Integer(1); else result = new Double(1.0); System.out.println(result); return result; Then I decided to refactor...

Ternary operator error - “Expected expression”

objective-c,ternary-operator
I haven't used the ternary operator much and I'm getting an error on this: (isInitializing) ? (return YES) : (isInitializing = 1); Error is: "Expected Expression" and it's pointing to return YES....

Ternary operator on Radio Button

php,html,if-statement,ternary-operator
I am new to PHP/Programming. I am unable to understand this ternary operator condition used for radio buttons. Can anyone tell me how do I convert this to simple if else type condition.? <?php if($_POST['midnightdelivery']) { echo ($_POST['midnightdelivery'] == 0)?'checked="checked"':''; } else { echo 'checked="checked"'; } ?> I checked this...