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.
I am new to C++ and I have a problem where i have to transform a pseudocode in C++ / C / Pascal language. The answer at the end of the book written in Pascal.
The problem in my C++ code is that at the line 12, I get the error which can be found in the title. Any idea?
Pascal Code:
var n,x:integer;
begin
n:=0;
repeat
write('x=');read(X);
if x<>0 then
if x mod 5 = 0 then
n:=n+1
else
n:=n-1;
until x=0;
if n=0 then
write('yes')
else
write('no')
end;
My C++ Code:
int main()
{
int x,n;
cin>>x;
while(x>0)
{
if(x>0)
{
if(x%5=0){
n=n+1;
} else {
n=n-1;
}
}
if(n=0){
cout<<"Yes"<<;
} else {
cout<<"No"<<;
}
}
}
You have a simple typo: if(x%5=0){ is an attempt to assign 0 to x % 5 (due to operator precedence modulus is computed before assignment). x % 5 cannot be assigned to (it's not an lvalue) and the compiler is telling you that.
The fix, of course, is to write x % 5 == 0.
You're lucky in this case that the error is picked up at compile-time. Something like if (n = 0) (on line 18) might not be, since x = 0 is an expression with value 0.
Two ways to guard against that:
Ensure that your compiler warnings are as aggressive as you can bear. With gcc, I use -Wall -Wextra, and that combination is enough to catch this common problem.
Some developers will write if (0 == x) since an errant if (0 = x) would be picked up at compile time as an attempt to assign to 0. Personally, I find that obfuscating.
Assignment operator requires lvalue means the left side operand need to be a variable/location that can hold a value.
This is what is meant by the error.
What you need in your if statement is == likely not assignment as mentioned by other answers
You need to use == in conditions (while, if, ...) for equality check in C++.
if(x%5 = 0)
should be
if(x%5 == 0)
"x%5" is not an lvalue in that you can not assign a value to it, hence the error.
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