Hi, so I have a bunch of if statements that check to see if a certain name matches what the statement has but I want something to trigger if none of the if statements are triggered.
So basically i had something like (this code doesn’t work):
if (x = something) {}
if (x = something else) {}
if (x = another different thing) {}
else () {}
The intention being that if none of the first 3 if statements triggered, then the else statement would trigger. This obviously doesn’t work since the else statement only triggers based on the third if statement so you could have a situation where both the first if statement AND the else statement triggers.
To fix this i have added a return statement at the end of each if statement and moved the code in the else statement in its own line (outside of the else statement) so that if any of the if statements run, the return statement will ensure that no more code in the function runs.
It looks like:
if (x = something)
{
do something;
return;
}
if ( x = something else)
{
do something else;
return;
}
Code here runs if none of the above if statements run (or more specifically if none of the return statements run)
But i’m not sure if this is the best solution so i thought id just make this post.
I know another potential solution is to, replace the else statement and use an if statement instead that combines all of the conditions of the above if statements but checks that they’re all false, something like:
if (x != something && x != something else) {}
But i don’t want to do that since it makes it a hassle if i ever want to make new if statements in this function (which i know i will).
That’s fine to use return to control the flow. Another option is, if it fits for you, switch statement and default.
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/switch
They can be unwieldy at times also.
Is there a reason you don’t want to else if?
if (A) {} else if(B) {} else if (C) {} else {}
Ummm….have you never heard of an actual else if?
if (condition1){}
else if (condition2) {}
else {}
If you don’t want multiples to run, you can chain if else statements together.
if (x == something) {}
else if (x == something else) {}
else if (x == another different thing) {}
else {}
This will only run the first if statement block or the else at the end if none of the ifs statements are true.
If it’s simple comparisons, you can make it a switch statement that looks like this that’s equivalent, where default works as the final else above.
switch (x) {
case something:
//code
break;
case something else:
//code
break;
case another different thing:
//code
break;
default:
//code
break;
}
no offense but this is genuine r/programminghorror
Cmon you guys, just help this guy instead of talking about r/programminghorror and shit. Hes obviously a beginner and shit like this is why beginners have anxiety about asking beginner questions.
I’m all for helping out beginners but damn we’re people too you know? Watching this hurts in a fundamental level
I think I’ve misunderstood or most others have but what you want is, like you said, the && operator.
If you think it’ll be too long I usually like to break it up like this:
This makes it easier to add and remove lines of you haven’t settled on how your code will work.
But if you find yourself doing too many big multi-part predicates, you probably need to reconsider how you’ve got everything organized
Edit: this looks like garbage on the reddit app, you’ll get a better idea of what I’m saying on desktop/browser I think.
have an upvote for variable names that made my morning!
Use else if or a switch.
Some programming languages have an elseif/elif construct.
But it’s really unneccessary, because if you have an else and an if, you can do the following:
this does the same thing as before, just without a special elseif construct.
and since indentation and new lines are meaningless in C#, you can format it to:
And in fact, any C# IDE will format it like that for you.
Many people don’t realize that C# has no special else-if construct, because “else if” looks like one, but really isn’t, it’s just an else followed by an if.
If you ever look at the Python programming language, you’ll realize that it has a special elif construct, because in Python, indentation is not meaningless.
Another option is the switch statement with the following syntax:
There are multiple quirks and limitations here.
it only works with one variable and with equality. If you want another condition, fall back to the if-construc every case needs a break at the end. Of course that’s not just arbitrary to annoy you, but it has a reason. Without the break, if will fall through to the next case and execute it’s content as well. Of course, that prevents you from using break for loops if you’re in a switch.
The syntax is quite weird for c#, or where else do you use the colon?
And the next option you already mentioned is returning early. Honestly, I like to return erly if it’s a possibility.
I think this:
looks nicer than:
With newer C# versions, there’s now also a switch expression (as opposed to the switch-statement i mentioned before).
it works the following:
And now ignore other peoples opinions and use whatever you think looks best for your purpose.
While the best approach would be chaining if else as most other have suggested, in some cases your approach, or even some mixture of both, is also good.
It mostly depends how you want it to be read:
If someone else is reading your code, do you want them to read it as “if something happens, then do this thing and we’re done here”,
or do you really want them to read “if not this, let’s try that, and if not that, then something else, etc…”
Both are legit approaches – depending on your intent when writing it.
Remember: good code is less for the computer and more for those who will read it afterwards. That also includes yourself – don’t lie to yourself and say you’ll remember a month or more down the road. Even if it’s just an exercise, writing code this way is part of the exercise.
I was going to recommend replacing the if block with just a switch statement if you have a finite number of conditions to test. c# switch has a 1 case:1 condition implementation. However, it sounds like you need branching of execution alongside flexibility of testing multiple conditions per branch. Without more details of the problem you’re trying to solve, as well as constraints, I’d just recommend nesting if blocks with n conditions in each if statement.
Not saying you’re incorrect but this legit sounds like you tried to only use sentences from the C# language spec haha.
You’re being downvoted because if(count == 0 && count ++) is horribly hacky