ternary-operator

C# Conditional Operator Not a Statement?

I have a simple little code fragment that is frustrating me: HashSet<long> groupUIDs = new HashSet<long>(); groupUIDs.Add(uid)? unique++ : dupes++; At compile time, it generates the error: Only assignment, call, increment, decrement, and new object expressions can be used as a statement HashSet.Add is documented to return a bool...

What does the question mark and the colon (?: ternary operator) mean in objective-c?

Hey guys, Google just don't want to give me some useful results. What does this line of code mean? label.frame = (inPseudoEditMode) ? kLabelIndentedRect : kLabelRect; The ? and : confuses me. Thanks a lot! ...

Java conditional operator ?: result type

I'm a bit puzzled about the conditional operator. Consider the following two lines: Float f1 = false? 1.0f: null; Float f2 = false? 1.0f: false? 1.0f: null; Why does f1 become null and the second statement throws a NullPointerException? Langspec-3.0 para 15.25 sais: Otherwise, the second and third operands are of types S1 and S2...

What does "|=" operation mean in C++ ?

I have the following code and I can't understand what does it mean: var1 |= var2>0 ? 1 : 2; Anyone can help me please! ...

unusual ternary operation

Hi, I was asked to perform this operation of ternary operator use: $test='one'; echo $test == 'one' ? 'one' : $test == 'two' ? 'two' : 'three'; Which prints two (checked using php). I am still not sure about the logic for this. Please, can anybody tell me the logic for this. ...

PHP ternary operator

($DAO->get_num_rows() == 1) ? echo("is") : echo("are"); This dose not seem to be working for me,I get an error "Unexpected T_ECHO" I have tried it with out the brackets around the conditional. Am I just not able to use a ternary operator in this way? The $DAO->get_num_rows() returns an integer value. Thanks ...

How to use ternary operator instead of if-else in PHP

hey guys i need to shorten or better to say ., harden my codes this is my original code : if ($type = "recent") { $OrderType = "sid DESC"; }elseif ($type = "pop"){ $OrderType = "counter DESC"; }else { $OrderType = "RAND()"; } now how can i use markers like this : $OrderType = ($type = "recent") ? "sid DESC" : "cou...

Ternary operators in C#

With the ternary operator, it is possible to do something like the following (assuming Func1() and Func2() return an int: int x = (x == y) ? Func1() : Func2(); However, is there any way to do the same thing, without returning a value? For example, something like (assuming Func1() and Func2() return void): (x == y) ? Func1() : Func2(...

Can I customize the indentation of ternary operators in emacs' cperl-mode?

In emacs cperl-mode, ternary operators are not treated specially. If you break them over multiple lines, cperl-mode simply indents each line the same way it indents any continued statement, like this: $result = ($foo == $bar) ? 'result1' : ($foo == $baz) ? 'result2' : ($foo == $qux) ? 'result3' : ($foo == $quu...

question about ? and : in c++

Why this statement : int a = 7, b = 8, c = 0; c = b>a?a>b?a++:b++:a++?b++:a--; cout << c; is not equal to : int a = 7, b = 8, c = 0; c = (b>a?(a>b?a++:b++):a++)?b++:a--; cout << c; and is equal to : int a = 7, b = 8, c = 0; c = b>a?(a>b?a++:b++):(a++?b++:a--); cout << c; Please give me some reason. Why ? ...

Type result with Ternary operator in C#

I am trying to use the ternary operator, but I am getting hung up on the type it thinks the result should be. Below is an example that I have contrived to show the issue I am having: class Program { public static void OutputDateTime(DateTime? datetime) { Console.WriteLine(datetime); } public static bool IsDateT...

conditional operator in C question

I just have a quick question about the conditional operator. Still a budding programmer here. I am given x = 1, y = 2, and z = 3. I want to know, why after this statement: y += x-- ? z++ : --z; That y is 5. The values after the statement are x = 0, y = 5, and z = 4. I know the way the conditional operator works is that it is formatte...

PHP Simplify a ternary operation

In PHP, is there a way to simplify this even more, without using an if()? $foo = $bar!==0 ? $foo : ''; I was wondering if there was a way to not reassign $foo to itself if the condition is satisfied. I understand there is a way to do this in Javascript (using &&, right?), but was wondering if there was a way to do this in PHP. ...

python: iif or (x ? a : b)

Possible Duplicate: Python Ternary Operator If Python would support the (x ? a : b) syntax from C/C++, I would write: print paid ? ("paid: " + str(paid) + " €") : "not paid" I really don't want to have an if-check and two independent prints here (because that is only an example above, in my code, it looks much more complica...

Django Template Ternary Operator

Hi, I was wondering if there was a ternary operator (condition ? true-value : false-value) that could be used in a Django template. I see there is a python one (true-value if condition else false-value) but I'm unsure how to use that inside a Django template to display the html given by one of the values. Any ideas? ...

Why do I have to typecast an int in a ternary expression?

Possible Duplicate: Conditional operator cannot cast implicitly? I have run into a peculiar situation and want to know why I have to do it. I'm using .net 3.5. This works: short foo; if (isValid) foo = -1; else foo = getFoo(); This does not work: short foo; foo = isValid ? -1 : getFoo(); I have to typecast -1: ...

Processing command line argument

Hi, I've been working with OpenCV, and some of the example code I've seen uses the following to read in a filename. I understand that argc is the number of command line arguments that were passes, and argv is a vector of argument strings, but can someone clarify what each part of the following line does? I've tried searching this but ha...

Ternary Operator in JavaScript With Multiple Expressions?

the_styles ? the_styles.appendTo('head'); the_styles=null : the_styles = $('.stylesheet').detach(); Obviously, this isn't valid. Notice the ";" between the appendTo() and the_styles=null. How do I write it on 1 line and still have multiple expressions like that? ...

java ternary operator

Hi, Can someone explain why this code? Collection c = (5 == 5) ? new ArrayList() : new HashSet(); produces the following compiler error: Incompatible conditional operand types ArrayList and HashSet For reasons that I don't understand, the following fixes the problem Collection c = (5 == 5) ? (Collection) new ArrayList() : new ...

C# using LINQ and Nullable Boolean

I have the below linq query which takes a text field which may be Y, N or DBnull and populates a boolean? parameter with either True, False or null depending on the value of the field. var dset = from i in tbdc.Talkbacks where i.talkback_id == id select new Talkback(i.talkback_id, i.acad...