answersLogoWhite

0


Best Answer

You use a nested if when the condition is dependent upon another condition. For example:

if (ptr != nullptr)

{

// ptr is non-null -- test the value it refers to

if (ptr* == 0)

{

// the value pointed to by ptr is zero

}

else

{

// the value pointed to by ptr is non-zero

}

}

In this case, the alternative to a nested if creates an inefficiency:

if (ptr != nullptr && *ptr == 0 )

{

// ptr is valid and refers to the value zero

}

else if (ptr != nullptr)

{

// ptr is valid and refers to a non-zero value

}

In this example, the expression "ptr != nullptr" is evaluated twice when ptr is valid and refers to a non-zero value. The nested if only evaluates this expression one time.

User Avatar

General Schiller

Lvl 10
2y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

7y ago

to calculate a person's tax rate based on earnings

This answer is:
User Avatar

User Avatar

Wiki User

8y ago

A nested if is any if statement where the body is another if statement.

if (expr1) { // if statement

if (expr2) { // nested if

// ...

}

}

Note that expr2 is only evaluated when expr1 is true. As such, this same statement could also be written without a nested if using a logical AND:

if (expr1 && expr2) { // ...

}

We typically use a nested if when we need to separate the conditional expressions and execute additional statements either before or after the nested if:

if (expr1) { // if statement

// ... statements to execute when expr1 is true

if (expr2) { // nested if

// ... statements to execute when both expr1 and expr2 are true

}

// ... more statements to execute when expr1 is true

}

We can also nest if statements within the else clause of an if statement, thus producing an else if statement:

if (expr1) { // if statement

// ... statements to execute when expr1 is true

} else if (expr2) { // nested if

// ... statements to execute when expr1 is false and expr2 is true

}

If we require additional statements between the else and if statements, we can use braces to separate them:

if (expr1) { // if statement

// ... statements to execute when expr1 is true

} else {

// ... statements to execute when expr1 is false

if (expr2) { // nested if

// ... statements to execute when expr1 is false and expr2 is true

}

// ... more statements to execute when expr1 is false

}

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: In which of the following scenarios would you need to use a nested IF statement?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

When is it best to use a case statement instead of nested if statements and vice versa?

You would want to do this when avoiding negativity or redundent statements. For instance, when taking any kind of survey, you would not to ask any type of question-- you would want to make a statement and using a grading scale to answer that statement (strongly agree, agree, etc).


What is an if or nested if statement?

Convenience here is in terms of the human reader of the program; a good optimizing compiler may treat both the switch and the nested if-else as pretty much the same thing. For me, using a switch is easier because it is easier to add additional cases to the switch. It can be (in my opinion) harder and more dangerous logically to fit additional test cases in a nested if. Finally, in some compilers the switch statement may be implemented in the hardware, so you end up with better performance.


Can a conditional operator replace an if statement always?

No. An if statement does not require an elseclause and the expression(s) do not return anything to the caller, whereas the conditional operator always executes one of two expressions and always returns the value of the executed expression back to the caller. The executed expression may be yet another conditional operator, thus allowing simulation of nested ifs and if...else if... else statements.Consider the following example:int x = rand();if( x > 100 ) x = 100;We can achieve the same result with a conditional operator:int y = rand();y = y>100 ? 100 : y;However, if we were to expand this statement to an if statement we would not get the original if statement shown above:int z = rand();if( z > 100 ) z = 100;else z = z;The else clause is clearly unnecessary in this case, so the original if statement would be the statement of choice here.As a general rule, if you can make use of the return value in a conditional operator, and must return one of at least two values, then use the conditional operator. Otherwise use an if statement.


What is nested class and what is its use in c plus plus?

A nested structure is simply one structure inside another. The inner structure is local to the enclosing structure. struct A { struct B {}; }; Here, we can instantiate an instance of A as normal. A a; But to instantiate B we must qualify the type because it is local to A: A::B b; If B were only required by A, then we can prevent users from instantiating instances of B simply by declaring it private: struct A { private: struct B {}; };


What are the similarities between if else and switch case in c program?

A switch-case statement is used to select between multiple values for a single variable. Like having a case for 1 2 and 3 for an integer. An If-else statement is used for evaluating an expression to either true or false.

Related questions

Would you be correct in saying that in an nested if statement it would read ----if and it is and it is then do this--- else do this?

What you are asking would be not be a nested if then else statement, in pseudocode what you are asking would be:if condition thendo thiselsedo that[this is pseudo code because the 'and' would be rendered differently in other languages and there potentially would be statement terminators, etc]A nested if statement would be:if condition1 thenif condition2 thendo thiselsedo thiselsedo thatThe second if statement is nested within the first one, clearly the nesting can go on quite deeply.


Which of the following scenarios would have been possible for the first time during the song dynasty?

Telling a friend that betrayed you it.


When is it best to use a case statement instead of nested if statements and vice versa?

You would want to do this when avoiding negativity or redundent statements. For instance, when taking any kind of survey, you would not to ask any type of question-- you would want to make a statement and using a grading scale to answer that statement (strongly agree, agree, etc).


What would the tokens be in the following statement if a 5 then b a?

5


David needs to create a new spreadsheet that will help him determine the payroll deductions for each employee. Which function would he use when he wants the spreadsheet to decide a deduction based on?

nested IF statement


What is a nested list?

A list within a list (or a list within another nested list).A list contains entries, which can be anything.Suppose those entries were lists themselves. Then they would be nested lists.


Would view the following statement with the least emotional?

A lawyer not involved in the case


What is the nested formula of MS Excel?

A nested formula is where one or more functions are placed inside another function to make a formula. For example you can write a formula where you put an IF function within an IF function and this would be a nested formula.


What is the definition of nested IF statement?

I believe it would be an if statement INSIDE of another if statement. So, ex. if one condition is TRUE, then the 2nd if statement is executed. could look like: if (colour ==red) if (leaf == maple) cout<< "OMG CANADA!"; else cout<< "Fail."; get it? of course, it can be beneficial sometimes beyond situations where you can simply use the && or operators as well.. but i have PDENG. hope that helps


What does write an expression for the following statement mean?

It is the start of a question. A statement would then follow as part of the question. It is asking you to read that statement and carry out the instructions in the first part of the question. Without the statement it is not possible to answer the question.


Following transitions would be most effective to show contrast to a preceding statement?

on the other hand


What is an if or nested if statement?

Convenience here is in terms of the human reader of the program; a good optimizing compiler may treat both the switch and the nested if-else as pretty much the same thing. For me, using a switch is easier because it is easier to add additional cases to the switch. It can be (in my opinion) harder and more dangerous logically to fit additional test cases in a nested if. Finally, in some compilers the switch statement may be implemented in the hardware, so you end up with better performance.