Replacing if statement C++ - c++

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.

Related

if then Ternary Operator in C++ [duplicate]

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);

Issue with the ternary statement

<cfscript>
x = 100;
y = 1;
var i = len(x) ? ((y == 1) ? 'PD': 'PP') : 'PU'
dump(i);
</cfscript>
problem is it is coming as PD while it should come as PP
If i am running in this gist, it works, only in my actual code it is behaving odd,
the values are not coming from anywhere else i double checked it
possibly if i write it in another way, that might fix it, any way i can do it
Ternary logic is essentially "if my condition is true, then do this, else do that.
<cfscript>
x = 100;
y = 1;
var i = len(x) ? ((y == 1) ? 'PD': 'PP') : 'PU'
dump(i);
</cfscript>
As written, what you have is:
"If there is a length of x, then ( if y is 1 then 'PD', else 'PP' ), else 'PU'.
Step 1: len(x) >>> x is the number 100, but is converted to the string "100", whose length is 3. But since ternary is a conditional, len(x) is true. So 'PU' can't be the answer.
Step 2: y == 1 >>> another conditional. And since you've set y to 1, then that is also true. So the answer will be the first part, or 'PD'.
Step 3: i is set to 'PD', as per the above ternary operations.
The answer you are getting is correct.

"Do nothing" in the else-part of the ternary operator?

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);

C++ Error "lvalue required as left operand of assignment"

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.

Combining static assert and assert?

I have a function that looks like this:
int div_round_up(int x, int y) {
/**
* This function only works for positive divisor and non-negative dividend!!
*/
assert(y > 0 && x >= 0);
if (x == 0)
return 0;
return (x - 1) / y + 1;
}
It won't work with y <= 0 or x < 0. That's ok with me, I can even dynamically check for right values, but I would like to check statically, when someone feeds it wrong values. If I defined x and y as unsigned, they would get silently converted from negative values to huge positive values which would produce erroneous result, so I don't want that. I would like to make compilation fail when someone attempts to feed it negative values like in div_round_up(variable, -7). What should I do?
To verify a number at compile time (which is what static_assert does), it has to be known at compile time. To see why this is needed, consider that something like div_round_up(read_integer_from_file(), read_keyboard_character()). The obvious drawback of doing that is that you have to know the numbers at compile time.
The easiest way is to make them template parameters, which allows you to leave the implementation of the function (almost) the same:
template<int x, int y>
int div_round_up() {
static_assert(y > 0 && x >= 0, "This function only works for positive divisor and non-negative dividend");
if (x == 0)
return 0;
return (x - 1) / y + 1;
}
It can be called as div_round_up<3, 4>() and will fail the compilation when the static_assert fires.
If you're using gcc or clang you can include a macro
#define div_round_up(a, b) (__builtin_constant_p(b) ? drus(a, b) : drud(a, b))
and two different function where drus includes a static assertion for b while drud does not.
Yeap you can do it with some magic(one nonamed russian code guru told me this trick)
#define check2(x) typedef char checkVal[(x)?1:-1];
int main() {
check2(3<4);
check2(5<4);
return 0;
}
but also in this case there is one limit. Compiler should know result of this value. In any another case it`s imossible(IMHO).