Nested If or single if with And operator, which is better approach?
Single If with And
if (txtPackage.Text != string.Empty && txtPackage.Text == "abc")
{
//
}
Nested If
if (txtPackage.Text != string.Empty)
{
if (txtPackage.Text == "abc")
{
//
}
}
So, there is pretty much nothing you can do to improve things, and nesting or no nesting has absolutely no impact on performance.
Nested IFs are powerful, but they become complicated quickly as you add more levels. One way to avoid more levels is to use IF in combination with the AND and OR functions. These functions return a simple TRUE/FALSE result that works perfectly inside IF, so you can use them to extend the logic of a single IF.
A nested if statement is an if statement placed inside another if statement. Nested if statements are often used when you must test a combination of conditions before deciding on the proper action.
It is possible to nest multiple IF functions within one Excel formula. You can nest up to 7 IF functions to create a complex IF THEN ELSE statement.
Using nested if conditions, we have printed a statement. Here inner if the condition is not true. Hence else part is executed. Nested if condition comes under decision-making statement in Java. It contains several branches with an if condition inside another if condition.
In Excel 2007 – 2016, total 64 conditions can be nested up while working with multiple ifs. You have to maintain a proper order while working with multiple ifs. If your formula contains too many ifs, it’s better to use OR and AND functions with that.
As you can imagine, nested IF statements become harder to read and maintain as the number of condition increases. The CHOOSE function is essentially a more user-friendly alternative for these types of scenarios. The CHOOSE function returns a value from the list of arguments based on the specified position.
The idea here is following the rest of the logic tree, if first condition is not successful. Then look at the second condition, and continue until a satisfactory result is foind. As you can imagine, nested IF statements become harder to read and maintain as the number of condition increases.
I wasn't going to chime in, but seeing that some answers here seem to be about "I like my code to look like this"... I feel that I should say something :)
"Better" means the code will execute faster, or it's more readable / extendable. You would want to nest your if's in the case that you would possibly have multiple checks that all have a common requirement.
Example:
if (myThingy != null)
{
if (myThingy.Text = "Hello") ...
if (myThingy.SomethingElse = 123) ...
}
EDIT: It also needs to be said that nesting your IF's requires more CPU cycles (and is therefore "slower") than a single IF. On top of that, the order of your conditions can greatly increase performance.
Exapmle again:
if (somethingQuick() && somethingThatTakesASecondToCalculate()) ...
is a LOT faster (of course) than
if (somethingThatTakesASecondToCalculate() && somethingQuick()) ...
Because if the first part of the IF fails, the second part won't even be executed, thus saving time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With