Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which is better apply two conditions in nested If or using single with And? [closed]

Tags:

c#

vb.net

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")
  {
     //
  }
}
like image 909
Nakul Chaudhary Avatar asked Nov 26 '08 12:11

Nakul Chaudhary


People also ask

Are nested if statements faster?

So, there is pretty much nothing you can do to improve things, and nesting or no nesting has absolutely no impact on performance.

How do you avoid multiple nested if statements?

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.

Why are nested if statements Good?

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.

How many nested if statements is too many?

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.

How do you use nested if conditions in Java?

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.

How many conditions can be nested in Excel with multiple IFs?

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.

What is the difference between choose and nested IF statements?

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.

Why are nested IF statements so hard to read?

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.


1 Answers

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.

like image 160
Timothy Khouri Avatar answered Nov 16 '22 03:11

Timothy Khouri