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);
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);
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'm trying to get fizzbuzz working in D but I have no idea for the life of me what the problem is. I have tried reversing the logic and it does write both words when it is not appropriate, but when it is it just writes nothing.
Here's a screenshot of what the output looks like: http://puu.sh/p67Hd/2a5a598b1b.png
import std.stdio;
void main() {
for (uint i = 0; i < 100; i++) {
if (i%5 && i%3) write(i);
if (!i%3) write("Fizz");
if (!i%5) write("Buzz");
writeln();
}
}
The ! operator has precedence over % so your if statements look like
if ((!i) % 3) write("Fizz");
if ((!i) % 5) write("Buzz");
and because all of i is non-zero (besides the first time), !i is always 0 and 0 % 5 and 0 % 3 is always 0 (false).
To fix, all you need to do is add parenthesis around the % operations
if (!(i % 3)) write("Fizz");
if (!(i % 5)) write("Buzz");
The reason is the operator precedence in D.
The if(!i%3) will actually be interpreted as if((!i)%3), which results in either 0%3 (which is false), or 1%3 (which is true). !n will result in 1 if n is 0, otherwise it will always be 0. Because it's going from 0 to 100, (!i) % 3 will only be true once at the beginning. This is the reason why there is a FizzBuzz at the start of the output.
So instead your code should look like this:
import std.stdio;
void main() {
for (uint i = 0; i < 100; i++) {
if (i%5 && i%3) write(i);
if (!(i%3)) write("Fizz");
if (!(i%5)) write("Buzz");
writeln();
}
}
I am trying to form an if statement like so:
int myVariable = -1;
if (0 <= myVariable <= 99)
{
// Do something
}
However, if I assigned -1 to myVariable, which is an int, the if-statement still evaluations to true.
What am I doing wrong ?
That is not the correct way to say what you want in C++ syntax. You want:
int myVariable = -1;
if (0 <= myVariable && myVariable <= 99)
{
// Do something
}
What you wrote does the following:
1) Evaluate 0 <= myVariable, which in your example will be false (converted to 0 in C++ in this context)
2) Then this result (0) is compared against <= 99, so C++ reads "0 <= 99", which is true, so the if statement is true.
What would be the output of this program ?
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int x=20,y=30,z=10;
int i=x<y<z;
printf("%d",i);
getch();
}
Actually i=20<30<10, so the condition is false and the value of i should be 0 but i equals 1. Why?
This int i=x<y<z; doesn't work the way you intended.
The effect is int i=(x<y)<z;, where x<yis evaluated first, and the value true is then compared to z.
Pascal points out below that in C the result of the comparison is 1 instead of true. However, the C++ true is implicitly converted to 1 in the next comparison, so the result is the same.
The comparison operators don't work like that. Your program is equivalent to:
i = (x < y) < z;
which is equivalent to:
i = (x < y);
i = i < z;
After the first operation, i == 1. So the second operation is equivalent to:
i = 1 < 10;
You need to rewrite your statement as:
i = (x < y) && (y < z);
The < operator has left-to-right associativity. Therefore x<y<z will do (x<y)<z. The result of the first parenthesis is 1, 1 is smaller than 10, so you'll get 1.
That's not how it works. It's better to see with parenthesis:
int i = (x<y)<z;
Now, first x<y is evaluated. It's true, 20<30, and true is 1 as an integer. 1<z is then true again.
Its precedence is from left to right. Thats is why it is like
20<30 = true
1<10 TRUE
SO FINALLY TRUE
Actually < is left-associative, so first, 20<30 is evaluated (giving 1 usually), then 1 is less than 10.
The output of "1" is correct. This is evaluated as (20<30) < 10, which is 1 < 10, which is 1.
The problem is that you are comparing a boolean value to an integer value which in most cases doesn't make sense.
< is evaulated from left to right, so 20<30 is true, or one, which is less than 10.
The operator < associates from left to right.
So x < y < z is same as ( x < y ) < z
Your expression evaluates as:
( x < y ) < z
= ( 20 < 30 ) < 10
= ( 1 ) < 10
= 1