ternary

Is this a reasonable use of the ternary operator?

Are there any understanding / maintainability issues that result from code like inVar1 == 0 ? NULL : v.push_back(inVar1); inVar2 == 0 ? NULL : v.push_back(inVar2); and so forth. The possibly confusing idea is using the ternary operator for program flow rather than variable assignment, which is the usual explanation. I haven't seen c...

What does it mean? expr1 = expr2 Mod expr3 = 0

So I am porting a VBA application to PHP and ran into this wonderful little nugget of code: expr1 = expr2 Mod expr3 = 0 I thought it was behaving like a ternary operator but when I broke it down to simple if then statements the outcome was not as expected. So I ask the brilliant stack**overflow** community to help me out and put it in...

PHP ternary operator not working

The code below takes an array value, if it's key exist it should echo out it's value, the ternary if/else part works but the value is not showing up, can anyone figure out why it won't? $signup_errors['captcha'] = 'error-class'; echo(array_key_exists('captcha', $signup_errors)) ? $signup_errors['catcha'] : 'false'; Also where I have ...

Ternary operator evaluation order

class Foo { public: explicit Foo(double item) : x(item) {} operator double() {return x*2.0;} private: double x; } double TernaryTest(Foo& item) { return some_condition ? item : 0; } Foo abc(3.05); double test = TernaryTest(abc); In the above example, why is test equal to 6 (instead of 6.1) if some_condition is true? Ch...

Which ternary operator in C# is most popular and mostly used?

Which ternary operator in C# is most popular and mostly used? ...

What is a concise and robust way to write these statements in JavaScript?

I have an <iframe> that uses some variables that are set in an object literal in the parent document. These variables are optional; if they're set, I want to use their values, assigning them to a more succinct global variable; if not, I want to call the function error(). First of all, is this ternary operator statement valid and effecti...

Question mark in JavaScript

I came across the following line in a JS function (it was an RGB to HSB color converter, if you must know) hsb.s = max != 0 ? 255 * delta / max : 0; I'm wondering if someone can explain what the "?" and the ":" mean in this context. ...

Javascript Ternary operator

I cant seem to wrap my head around the first part of this code ( += ) in combination with the ternary operator. h.className += h.className ? ' error' : 'error' The way i think this code works is as following. h.className = h.className + h.className ? ' error' : 'error' But that isn't correct because that gives a error in my consol...

How to use C#'s ternary operator with two byte values?

There doesn't seem to be a way to use C#'s ternary operator on two bytes like so: byte someByte = someBoolean ? 0 : 1; That code currently fails to compile with "Cannot convert source type 'int' to target type 'byte'", because the compiler treats the numbers as integers. Apparently there is no designated suffix to indicate that 0 and ...

java ternary hack

So I'm not going for maintainability or elegance here.. looking for a way to cut down on the total tokens in a method just for fun. The method is comprised of a long nested if-else construct and I've found that (I think) the way to do it with the fewest tokens is the ternary operator. Essentially, I translate this: String method(param) ...

Entity Relationship Model: Ternary Relationships

Hi, I am trying to understand why this statement in the book is wrong: "given a C entity, there is at most one related A entity and at most one related B entity". Is it that it doesn't apply to a specific kind of relationship?? So, if I have an example of a student who is in attendance to a course with a type of subject. The entities ...

Other ternary operators besides ternary conditional (?:)

The "ternary operator" expression is now almost equivalent to the ternary conditional operator: condition ? trueExpression : falseExpression; However, "ternary operator" only means that it takes three arguments. I'm just curious, are there any languages with any other built-in ternary operators besides conditional operator and which o...

?: ternary conditional operator behaviour when leaving one expression empty

Hi, I was writing a console application that would try to "guess" a number by trial and error, it worked fine and all but it left me wondering about a certain part that I wrote absentmindedly, The code is: #include <stdio.h> #include <stdlib.h> int main() { int x,i,a,cc; for(;;){ scanf("%d",&x); a=50; i=100/a; for(...

Hibernate mapping for ternary association fails to delete table rows

Hi there, I'm pretty new to hibernate and this is my first posting in here. I want to create a mapping for a somehow complex scenario and I just don't get it. I have a 'group' entity where I want to store information about 'orgUnits' and 'divisions', a group member has access to. One group can contain zero to many orgUnits and the orgU...

return statement in ternary operator c++

I wrote the absolute function using ternary operator as follows int abs(int a) { a >=0 ? return a : return -a; } I get the following error messages ../src/templates.cpp: In function ‘int abs(int)’: ../src/templates.cpp:4: error: expected primary-expression before ‘return’ ../src/templates.cpp:4: error: expected ‘:’ before ‘return’ ....