How can if() evaluate incorrectly in C# [closed] - c#

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 11 years ago.
I have a simple bit of logic.
int i = 0;
if (i < 0) {
//whatever;
}
When I debug with VS I see i set to 0 BUT the if evaluates as false! How can this be?
GUYS. Perhaps I could have worded it better!!! The above expression SHOULD evaluate as false when i is 0 which I see when I hover over it in VS BUT it goes into the brackets and does "whatever"... WHICH is not right.
EDIT: Please see my similarly named but more recent question for a solution.

0 is not less than 0. Its equal to. Do if (i <= 0)

That's because 0 < 0 is false.

Because i is not less than zero. So, the expression evaluates as false, which is correct.

0 is NOT less than 0. it is less than OR EQUAL to 0

It evaluates to false because 0 is not less than 0.

The answer is because i isn't less than 0.
In order for the if statement to evaluate to true, i would need to be a negative integer.

If i equals 0, then it is false, because it is not less than 0.
What you are thinking of is if(i == 0) or if (i <= 0) (or for that matter if(i >= 0)). Each of these are true if i equals 0.

0 < 0 will always be false. Under what condition do you expect it to evaluate to true?

0 < 0 is false. use <= if you want it to evaluate to true if i is 0

Maybe your simple bit of logic needs to be:
int i = 0;
if(i <= 0) {Whatever }

Related

if greater than in C# [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
Edit: Problem solved, the greater than was the wrong ascii character. Even tho I didn't copy paste it from somewhere. I'll have to check my keyboard language.
Edit: Just to make it clear, effect.Parry is an int? not a decimal. See edit below
Edit: I changed the decimalToString. That was an error on my part but I'm still getting the error
I am trying to do a greater than if statement in my code but I am getting an error and I just can't figure what the error is...
Here is the part of the code.
if (effect.Parry != null)
{
txtEffect += " Parry the next " + effect.Parry.ToString() + " attack";
if (effect.Parry ­> 1)
txtEffect += "s";
if (effect.NumberOfTurn != 0)
{
txtEffect += " over " + effect.NumberOfTurn.ToString();
}
}
And here is the error I get:
Erreur 1 Caractère '-­' inattendu
In english it would be: Unexpected character '-'
Can someone explain me what I did wrong? I tried ­>= and it gives me the same error. If I try without > I don't get an error.
Edit: txtEffect is a string, effect.Parry is an int? (To allow nulls) I tried parsing it into a normal int and I get the same error as above.
Your problem came from defining effect.Parry as an int? and then passing it into a function requiring a decimal? as a parameter. Change one or the other so they match, and you should take care of your problem.

Collision through for loop [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
for (int x = bulletBounds.Count - 1; x > -1; x-- )
{
for (int y = alienPosition.Count - 1; y > -1; y--)
{
if (bulletBounds[x].Intersects(alienBounds[y]))
{
alienPosition.RemoveAt(y);
bulletBounds.RemoveAt(x);
hit++;
}
}
}
What I'm trying to do with this code is go through both lists of objects and see if they collide. I saw in another question that the only way to do this is iterate backwards through the list so I did that. The issue now is that when a bullet collides with an alien, every alien before it in the list is also getting deleted. So if I have 6 aliens on the screen and I hit the one on the far left, every alien to the right of it gets deleted! How do I fix this?
After removing the alien and bullet you need to break out of the inner loop.
if (bulletBounds[x].Intersects(alienBounds[y]))
{
alienPosition.RemoveAt(y);
alienBounds.RemoveAt(y);
bulletBounds.RemoveAt(x);
hit++;
break;
}
Add break; to your if loop. By the way, what's the difference between alienBounds and alienPosition? You're looping through alienPosition, however, accessing data in alienBounds. In the example below, I'm just looping through alienBounds. If I misunderstood anything, please let me know, and I will update my answer accordingly.
foreach (var bullet in bulletBounds)
{
foreach (var alien in alienBounds)
{
if (bullet.Intersects(alien))
{
bulletBounds.Remove(bullet);
alienBounds.Remove(alien);
break;
}
}
}
Hope this helps!

OR statement in lambda expression [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
What is the correct way to do something like this.
db.Tasks.Where(t => {t.CategoryId == 1 || t.CategoryId == 2) || t.CategoryId == 3)}).ToList();
Your brackets are just wrong at the moment, both in terms of curly braces and plain parentheses. You don't actually need any brackets within the expression - this is fine (reformatted for clarity):
var list = db.Tasks
.Where(t => t.CategoryId == 1 ||
t.CategoryId == 2 ||
t.CategoryId == 3)
.ToList();
A lambda expression is exactly that – it consists of a single ordinary expression. (such as a || b || c)
Braces are for statements, not expressions.
Just remove the {} and that will work fine.
You also have some stray ).

C# taking large negative no. as a positive value [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
To cut to the chase, when performing some calculations within my code i have a result that is something like. 7.6742332E-30, i have this value stored in a double variable for example, double result = 7.6742332E-30;
When i later check whether this value is greater than 0 the result is true, that it is greater than 0, i assume due to the 7.6742332.
So my question is this, why is the E-30 not being considered and how do i resolve this?
Any advice would be great and thanks so much i advance!
7.6742332E-30 is 0.0000000000000000000000000000076742332, which is a positive number.
7.6742332E-30 mathematcally equals 7.6742332 x 10^-30 which is a positive number

Short circuiting not working [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
It was my understanding that C# conditional operators perform short circuiting. However, my program throws an exception while running this code. Index is equal to -1, so the second condition should never execute, but I get an index out of bounds exception.
if (index != -1 || pieces[index].Type != PieceType.Rook)
{
allowed = false;
}
You have used || so when the first condition fails (index IS -1) the runtime needs to check the second condition before excluding the code from execution and that will trigger the exception,
Instead, you use && (AND) if you want to enter the if only when the two conditions are true.
In this way, as before, the runtime checks the first condition and now getting the false result is enough to decide that there is no need to check the second condition..
if (index != -1 && pieces[index].Type != PieceType.Rook)
{
allowed = false;
}
Replace || with &&
if (index != -1 && pieces[index].Type != PieceType.Rook)
Otherwise the second codition is evaluated if the first is false what you don't want. You want both conditions being true and especially the first one.
That's because you use OR operator. First condition is false, so second starts evaluating.
You should have index != 1 && ....
Also, is index < -1 or >= pieces.Length?
|| will stop evaluating when it finds that something is true. Since index != -1 is false it will evaluate both sides of the expression. If you && it would stop once it finds a false. I would recommend reading up on lazy evulation.

Categories

Resources