answersLogoWhite

0


Best Answer

if (i<0) if (j<0) printf ("i<0 and j<0");

else printf ("What's now? Where this else belongs to? It's i>=0 or i<0 and j>=0?");

The answer is:

if (i<0) if (j<0) printf ("i<0 and j<0");

else printf ("i<0 and j>=0");

Of course you can use brackets:

if (i<0) {

if (j<0) printf ("i<0 and j<0");

else printf ("i<0 and j>=0");

}

or

if (i<0) {

if (j<0) printf ("i<0 and j<0");

} else printf ("i>=0");

User Avatar

Wiki User

15y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

9y ago

Nested-if statements are useful when we have two or more expressions that need to be evaluated in various combinations, where a standard if statement would result in the same expression being evaluated two or more times.

For instance, if we have two expressions, a and b, and we want to test all the possible combinations of these two expressions then we find there are four possibilities:

if (a && b)

{/*...*/}

else if (a && !b)

{/*...*/}

else if (!a && b)

{/*...*/}

else // if (!a && !b)

{/*...*/}

Note that the final if is commented out because the previous ifs mean it must be the only logical combination left, so there's no need to evaluate anything. The if is redundant but the comment helps us follow the logic.

Now consider the efficiency behind each individual code block {/*...*/}. Remember that when the first expression in a logical AND (&&) evaluates false, there's no need to evaluate the second expression because false && true is always false.

If both a and b were true, we'd evaluate both a and b exactly once each. That's as good as it gets because if b were false, we'd evaluate a and b twice each! And if a is false and b is either true or false, then we'd evaluate a three times and b once.

Nested ifs allow us to evaluate a and b in isolation:

if (a)

{

if (b)

{/*...*/}

else // if (!b)

{/*...*/}

}

else // if (!a)

{

if (b)

{/*...*/}

else // if (!b)

{/*...*/}

}

Again, I've commented out the redundant ifs. This structure is more efficient than the standard if because we only need to evaluate a once to determine which nested if needs to be evaluated, and we only need to evaluate b once to determine which code block to execute within the nested if. Thus every code block requires just two evaluations. It simply doesn't get more efficient than that!

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Example problem using nested if statement in C programming?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is the if statement that appears inside another if statement?

nested if Statement


What is nesting in C programming?

Nesting can be a very handy tool in C++, but should be avoided if possible.C++ gives us If statements, For loops and many other things. These can be nested. For example:A nested If statement://outer if statementIf( this is true ){//nested if statementif( this is also true ){//do something}else{//do something else}}


What does it mean to evaluate cobol?

The word evaluate in the programming language COBOL can be used to replace a nested if statement. Instead of long statement evaluate allows one to shorten the coding required and write cleaner code.


In which of the following scenarios would you need to use a nested IF statement?

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 &amp;&amp; *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 &quot;ptr != nullptr&quot; is evaluated twice when ptr is valid and refers to a non-zero value. The nested if only evaluates this expression one time.


What does if statements in c plus plus implement?

C++ if() statements implements a comparison and a jump opcode. By way of example, consider the following code:if( x 100 ) generates a compare and jump opcode (cmp and jne).The cmp opcode compares the value of x with 64h (100 decimal) which will either set or clear ZF (the zero flag). If x is 100, then ZF will be unset, otherwise it will be set.The jne (jump if not equal) opcode tests ZF. If it is set, then x was not 100 and the operand (1181934h) is passed to the PC register (the program counter). The IR (instruction register) will fetch the value from PC on the next cycle. This ultimately causes execution to jump to the statement immediately following the else clause of the if statement (the second printf statement).If ZF is not set, then x really was 100 and the PC register remains unaffected. At that point the PC register will contain the value 0118191Bh, which is the next instruction after the if statement, thus execution simply falls through to the first printf statement. After processing the printfinstructions, execution will reach the else clause which simply forces a jump to bypass the second printfstatement.Thus, one of the printf statements is executed and then execution continues from the instruction at 118194Bh, which is not shown but is the next instruction after the second printfstatement.

Related questions

What is the definition of 'nested' in C programming?

In C a structure within a structure is called nested. For example, you can embed a while loop in another while loop or for loop in a for loop or an if statement in another if statement.


What is the if statement that appears inside another if statement?

nested if Statement


What is nesting in C programming?

Nesting can be a very handy tool in C++, but should be avoided if possible.C++ gives us If statements, For loops and many other things. These can be nested. For example:A nested If statement://outer if statementIf( this is true ){//nested if statementif( this is also true ){//do something}else{//do something else}}


How do you write a Program of printing prime numbers between 1 to 100 in 1 statement only?

You really need some nested loops; but some programming languages might allow you to write this as one statement.


What does it mean to evaluate cobol?

The word evaluate in the programming language COBOL can be used to replace a nested if statement. Instead of long statement evaluate allows one to shorten the coding required and write cleaner code.


When do you use the nested if?

we use "nested if" if we have to test a large number of possibilities and trials i an if statement.


Syntax of nested if in c?

If(condition) { if-else statement; } else { if-else statement; }


Can you have nested switch statement inc language?

yes,we can


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.


What does nial mean?

It is a high-level programming language: Nested Interactive Array Language.


What does an IF Statement do?

An if statement is a control statement. It is used to control whether a statement executes or not, depending on whether a control expression evaluates true or false.if (expression) {statement;}In the above example, the expression is evaluated. If true, the statement executes, otherwise it does not.if (expression) {statement1;} else {statement2;}In the above example, the expression is evaluated. If true, statement1 executes otherwise statement2 executes.Note that if statements may be chained together using else if statements. The final else clause (if present) then becomes the default case. Also, any statement within an if statement may itself be an if statement, known as a nested if. If statements may be chained or nested to any depth.


What is an 'embedded if' statement?

nested if statements. Example: If condition is right then if condition is even better then ... else ... end if else if condition is worse then ... else ... end if end if