[Help] [FIXED] if/or/and statement
Hi,
I currently have a simple and or in my if statement and it shouldn't go in my if statement, it should go in my else. But for some reason it doesn't, i have no idea why.
Here's my code:
Code:
string TempWebsite = "/Login";
String CleanAdress = "example.com";
if (TempWebsite.Contains("http://" + CleanAdres) == false || TempWebsite.Contains("https://" + CleanAdres) == false && TempWebsite.Contains("http") == true || TempWebsite.Contains("https") == true)
{
OutgoingLinks.Add(TempWebsite);
}
else
{
IncomingLinks.Add(TempWebsite);
}
Now it's going in the if statement, when it should go in the else statement.
Why is that?
It should only go in the if, if it includes http or https as well.
(TempWebsite.Contains("http") == true || TempWebsite.Contains("https") == true))
/login doesn't include http or https.
Re: [Help] if/or/and statement
I don't get the whole gist of what you're trying to do, but && has higher precedence than || hence you have 3 separate conditions a || (b && c) || d where by what it looks like you probably meant to do (a || b) && (c || d).
Isn't the latter redundant though? If the address contains either "http://something" or "https://something" then it by necessity also contains the shorter string "http" or "https". Also note that if the address contains "https" then it also automatically contains "http".
edit: Read your code wrong, didn't pay attention to how the first 2 conditions test against false. Note what I said about operator precedence though and "http" is still always contained in "https" like I said originally.
Is this what you're trying to do?
Code:
linkUrl = ...
string myDomain = "example.com";
if (linkUrl.Contains(myDomain)) { incomingLinks.Add(linkUrl); } else { outgoingLinks.Add(linkUrl; }
Re: [Help] if/or/and statement
Code:
string TempWebsite = "/Login";
String CleanAdress = "example.com";
some initialisation
Code:
&& TempWebsite.Contains("http") == true || TempWebsite.Contains("https") == true
part of the if statement
TempWebSite contain : /Login
so you will not find http or https in this one
Re: [Help] if/or/and statement
Quote:
Originally Posted by
decarvk
Code:
string TempWebsite = "/Login";
String CleanAdress = "example.com";
some initialisation
Code:
&& TempWebsite.Contains("http") == true || TempWebsite.Contains("https") == true
part of the if statement
TempWebSite contain : /Login
so you will not find http or https in this one
Umm yeah sure. I assumed the input url could be anything and not only this fixed string and your answer is probably more helpful if he's struggling even with this particular case.
Quote:
Originally Posted by
Eronisch
Code:
string TempWebsite = "/Login";
String CleanAdress = "example.com";
if (TempWebsite.Contains("http://" + CleanAdres) == false
Now it's going in the if statement, when it should go in the else statement.
Why is that?
Let me try again with the shortest possible exact answer: The TempWebSite string doesn't contain "http://example.com" so the first part in the if evaluates to true which makes the whole condition evaluate to true hence going to the first branch instead of else.
Like I hinted in my first post it's because && precedes ||, you probably meant to have braces like (a || b) && (c || d).
Re: [Help] if/or/and statement
Quote:
Originally Posted by
Negata
Umm yeah sure. I assumed the input url could be anything and not only this fixed string and your answer is probably more helpful if he's struggling even with this particular case.
Let me try again with the shortest possible exact answer: The TempWebSite string doesn't contain "http://example.com" so the first part in the if evaluates to true which makes the whole condition evaluate to true hence going to the first branch instead of else.
Like I hinted in my first post it's because && precedes ||, you probably meant to have braces like (a || b) && (c || d).
Thanks, i never knew that i needed braces. You solved my bug, thanks!