Questions tagged [conditional-expressions]
23 questions
61
votes
7 answers
C# if-null-then-null expression
Just for curiosity/convenience: C# provides two cool conditional expression features I know of:
string trimmed = (input == null) ? null : input.Trim();
and
string trimmed = (input ?? "").Trim();
I miss another such expression for a situation I…

chiccodoro
- 14,407
- 19
- 87
- 130
54
votes
3 answers
Why isn't this a syntax error in python?
Noticed a line in our codebase today which I thought surely would have failed the build with syntax error, but tests were passing so apparently it was actually valid python (in both 2.x and 3).
Whitespace is sometimes not required in the…

wim
- 338,267
- 99
- 616
- 750
21
votes
2 answers
Conditional expression in Makefile
I know you can use if statements like the following in makefiles:
foo: $(objects)
ifeq ($(CC),gcc)
$(CC) -o foo $(objects) $(libs_for_gcc)
else
$(CC) -o foo $(objects) $(normal_libs)
endif
Is there a way to do a conditional…

aaronman
- 18,343
- 7
- 63
- 78
7
votes
3 answers
Are conditional expressions broken within packages?
Consider the following snippet:
requires
designide,
rtl,
vcl,
{$IF RTLVersion < 19.0} // E2026 Constant expression expected
//{$IF CompilerVersion = 22.0} // same as above
vcljpg;
{$ELSE}
vclimg;
{$IFEND}
It seems…

OnTheFly
- 2,059
- 5
- 26
- 61
5
votes
1 answer
Python inline conditional in string concatenation
I had this:
msg = time + b' - ' + Logger.LEVELS_WORD[msg_loglevel] + b': ' + msg.encode('utf-8') + b'\n'
Since sometimes msg was already bytes, I wanted to concat msg.encode('utf-8') if it was string or else just msg, so I did this:
msg = time + b'…

JorgeeFG
- 5,651
- 12
- 59
- 92
4
votes
2 answers
Numeric Type Promotion with Conditional Expression
I was toying around with java, and I noticed something. It can be best shown here:
boolean boo = true;
Object object1 = boo ? new Integer(1) : new Double(2.0);
Object object2;
if (boo)
object2 = new Integer(1);
else
object2 = new…

John S.
- 626
- 1
- 4
- 16
4
votes
9 answers
What does 'Conditional expressions can be only boolean, not integral.' mean?
What does 'Conditional expressions can be only boolean, not integral.' mean? I do not know Java and I know C++ deffenetly not enought to understend what it means.. Please help (found in…

Rella
- 65,003
- 109
- 363
- 636
3
votes
6 answers
In line Conditional Expression or Function - Pythonic?
I have a situation where I would like to conditionally slice a string from the
reported position of an '@' symbol; the condition being: slice the string if
the '@' is there, else leave it untouched. I thought up two ways, one using a
function, the…

Raz
- 595
- 4
- 16
3
votes
1 answer
One line if assignment in python
following this topic One line if-condition-assignment
Is there a way to shorten the suggested statement there:
num1 = (20 if intvalue else 10)
in case that the assigned value is the same one in the condition?
this is how it looks now:
num1 =…

John
- 1,724
- 6
- 25
- 48
3
votes
5 answers
Is it possible to have an if inside a tuple?
I'd like to build something like:
A = (
'parlament',
'queen/king' if not country in ('england', 'sweden', …),
'press',
'judges'
)
Is there any way to build a tuple like that?
I tried
'queen/king' if not country in ('england', 'sweden', …)…

Hoffmann
- 1,050
- 9
- 26
3
votes
3 answers
In Java, is a conditional expression a thread safe operation?
I'm wondering if a conditional expression is a thread safe operation in Java.
E.g.:
return (mObject != null ? mObject.toString() : "null");
So, my question is: If two threads can change mObject, is this code thread safe, or does the developer…

Plinio.Santos
- 1,709
- 24
- 31
2
votes
1 answer
Incomplete FLWOR expression: expecting 'return'
I am having some trouble with the if-then-else command of the XQuery.
Currently I am using BaseX to edit XQuery (if that matters!)
if ($item/pf3:Current/pf3:Name) then (
let $Name := "None"
) else (
let $Name :=…

hell_storm2004
- 1,401
- 2
- 33
- 66
2
votes
1 answer
Conditional check with str.endswith()
I have the following string
mystr = "foo.tsv"
or
mystr = "foo.csv"
Given this condition, I expect the two strings above to always print "OK".
But why it fails?
if not mystr.endswith('.tsv') or not mystr.endswith(".csv"):
print "ERROR"
else:
…

neversaint
- 60,904
- 137
- 310
- 477
2
votes
5 answers
Python one line if-else with different operators
I was fiddling with one line if and for statements in python and ran across the following problem:
I can make something like the following work:
state = 1 if state == 4 else 2
But I want to use = and += in the same context, something like…

ovunctuzel
- 135
- 1
- 10
2
votes
3 answers
pointer/integer type mismatch in a conditional expression
So I have already had a look at other posts with similar headings but none of the suggested answers are working for me.
I have a function that calculates the frequency of a char in a string:
int frequency(char *s, char c) {
int i;
for (i=0;…

cxzp
- 652
- 2
- 14
- 28