What's the standard line to add to the ternary operator in order to do nothing if the condition is not met?
Example:
int a = 0;
a > 10 ? a = 5 : /*do nothing*/;
Using a seems to do the trick, but I am wondering if there is a more generally accepted way.
That will do it:
a = a > 10 ? 5 : a;
or simply:
if (a > 10) a = 5;
Another option:
a ? void(a = 0) : void();
What's good about this one is that it works even if you can't construct an instance of decltype(a = 0) to put into the 'do nothing' expression. (Which doesn't matter for primitive types anyway.)
You can also use a logical expression (though maybe confusing) in case you don't want to use an if statement.
a > 10 && a = 5
You can do:
a > 10 ? a=5 : 0;
But, I would prefer:
if (a > 10)
a = 5;
Just for a sake of variety, but not recommending as it is very ambiguous.
void do_smth()
{}
bool a = true; // not necessarily
a && (do_smth(), 0);
Related
What's the standard line to add to the ternary operator in order to do nothing if the condition is not met?
Example:
int a = 0;
a > 10 ? a = 5 : /*do nothing*/;
Using a seems to do the trick, but I am wondering if there is a more generally accepted way.
That will do it:
a = a > 10 ? 5 : a;
or simply:
if (a > 10) a = 5;
Another option:
a ? void(a = 0) : void();
What's good about this one is that it works even if you can't construct an instance of decltype(a = 0) to put into the 'do nothing' expression. (Which doesn't matter for primitive types anyway.)
You can also use a logical expression (though maybe confusing) in case you don't want to use an if statement.
a > 10 && a = 5
You can do:
a > 10 ? a=5 : 0;
But, I would prefer:
if (a > 10)
a = 5;
Just for a sake of variety, but not recommending as it is very ambiguous.
void do_smth()
{}
bool a = true; // not necessarily
a && (do_smth(), 0);
I want to realize this simple C++ program:
(if y>0) x=2; else x=10;
but without the use of an if statement or any other statement such as for, while, do while, switch or ?.
Is it possible? I am still wondering about this.
You can try this: x = 2 + (y <= 0) * 8; A boolean expression converted to an integral value is either 0 or 1, which you can use to add optional summands.
Here's another option:
x = 10;
y > 0 && (x=2);
Not really recommended, but it works.
f(-1);
int f(int a) {
return (a%2) ? (a%2) : (a*2);
}
Consider the above function f() , a%2 is returned if a%2 is evaluated to non-zero.
QUESTION : Are there any advance operator or expression that can merge the condition and return value and form something like
return (a%2) OR (a*2) // underlying logic is return (a%2) ? (a%2) : (a*2);
I understand that I have provided one possible solution return (a%2) ? (a%2) : (a*2); already but I want to explorer more on C++ so I ask this question. This question is simply a Yes/No question. I am expecting answers other then using if-else statement
My thought :
a%2 in a condition is evaluated and converted to type bool so it is no way to cast back to corresponding int value.
You can use the fact that a Boolean expression results in 0 or 1 and use
int f(int a) {
return (a > -1) * (a + 1);
}
If (a > -1) is true then we have 1 * (a + 1) otherwise we have 0 * (a + 1).
I would not suggest doing this though. Clear code should be one of the big things you try to maintain in you code. Using a well know syntax like condition ? true_result : false_result is better IMHO.
EDIT:
If you want the value to be something other than zero then it gets a little more complicated. You would need to add to the return value the value of the false condition times a Boolean expression that is only true when the true condition is false(a not condition).
int f(int a) {
return ((a % 2) * (a % 2)) + (!(a % 2) * (a * 2));
// ^ true part ^ ^ false part ^
}
With this when the true part is 0 the false part will be a value and when the false part is 0 the true part will be a value.
Just write a function:
template<typename T>
T conditional( T &&v1, T &&v2 )
{
return v1 ? v1 : v2;
}
int f( int a )
{
return conditional( a%2, a*2 );
}
If you're on gcc, there's an extension for that:
int f(int a) {
return (a%2) ? : (a*2);
}
is equivalent to:
int f(int a) {
return (a%2) ? (a%2) : (a*2);
}
Clang seems to support it too. But really the compiler will probably do the right thing for you if you duplicate the expression. At the very least, this:
int f(int a) {
auto&& expr = a%2;
return expr ? expr : (a*2);
}
is more readable.
Actually that function always returns a+1. If a+1 is 0, you say you return 0, which is also a+1.
To answer what you mean though, you can always just do a bit of math if you prefer: !!cond * res1 + !cond * res2 for the general case of cond ? res1 : res2.
So I'm just curious if there is a short hand statement to this:
if(number < 0 )
bigInt.sign = 0;
else
bigInt.sign = 1;
I see all these short hand statements for if a < b and such.
I'm not sure on how to do it properly and would like some input on this.
Thanks!
I actually just figured it out right before you guys had answered.
The shortest solution is bigInt.sign = (number < 0) ? 0 : 1
The basic syntax for using ternary operator is like this:
(condition) ? (if_true) : (if_false)
For you case it is like this:
number < 0 ? bigInt.sign = 0 : bigInt.sign = 1;
try this:
bigInt.sign = number < 0 ? 0 : 1
Yes:
bigInt.sign = !(number < 0);
The ! operator always evaluates to true or false. When converted to int, these become 1 and 0 respectively.
Of course this is equivalent to:
bigInt.sign = (number >= 0);
Here the parentheses are redundant but I add them for clarity. All of the comparison and relational operator evaluate to true or false.
Depending on how often you use this in your code you could consider the following:
macro
#define SIGN(x) ( (x) >= 0 )
Inline function
inline int sign(int x)
{
return x >= 0;
}
Then you would just go:
bigInt.sign = sign(number);
you can also try this :
bigInt.sign = (number<0)*0 + (number>=0)*1;
If we need to assign another value other than 0 and 1 then this code can be used like :
bigInt.sign = (number<0)*(replacement_of_0) + (number>=0)*(replacement_of_1);
I recently encountered with this question: How to reduce this expression: s>73?61:60;.
The hint given was that Instead of using conditional operator we could use a simple comparison which will work fine.
I am not sure but I think it is possible with some GCC extension,although I am unable to figure it out myself.
EDIT:The whole expression is this : s-=s>73?61:60
Just like the other answers:
s -= (s > 73) + 60;
This expression works because the spec defines the results of the relational operators. Section 6.5.8 paragraph 6:
Each of the operators < (less than), > (greater than), <= (less than or equal to), and >= (greater than or equal to) shall yield 1 if the specified relation is true and 0 if it is false. The result has type int.
How to reduce this expression: s-=s>73?61:60;
How about:
typedef int Price;
Price getPriceAfterRebate(const Price priceBeforeRebate)
{
const Price normalRebate = 60;
const Price superRebate = 61;
const Price superRebateThreshold = 73;
Price returnValue = priceBeforeRebate;
if (priceBeforeRebate > superRebateThreshold)
{
returnValue -= superRebate;
}
else
{
returnValue -= normalRebate;
}
return returnValue;
}
Tada! An ugly piece of unmaintainable code is reduced to a readable and maintainable block of code.
This is such an ugly piece of code that I can't beleive I wrote it, but I think it fulfills the requirement:
My answer to the original question which was s>5?6:9:
9 - (((int)(s > 5)) * 3)
Rewritten for the updated question:
61 - (int)(s > 73)
Maybe this?
60 + !!(s > 73)
The double-bang maps non-zero values to 1 and zero to zero.
What is the value of (s>5)? Could you do some arithmetic with that?
Without the hint, I would say this was a bad "gotcha" interview question that requires a particular a-ha insight that's not correlated with ability. With the hint, it's... nice, but dim.
If we assume that True = 1 and False = 0, then doesn't this work:
s-= (60 + (s > 73))
It can be thought of as s -= (s > 73) + 60 as > is a relational operator and it will return 1 or 0 depending on result of expression s > 73 ,As the return value is int so it will work fine