answersLogoWhite

0


Best Answer

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.

User Avatar

Wiki User

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

Wiki User

13y ago

Switch case: Lots of case are there which 1 matched go for it. It dont take condition in case.

If else: take condition as argument. and if condition true then enter into body otherwise left the if block and go to immidiate next statement. If there have any nested if then it checks its condition and this way it works...

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

The primary (subtle) difference is that switches require breaks while if statements do not.

In the following example, if x is one, we add 5 to y, otherwise we add 7.

if(x==1) {

y = y + 5;

} else {

y = y + 7;

}

This is the identical switch statement.

switch(x) {

case 1: y = y + 5; break;

default: y = y + 7;

}

However, it becomes easy to leave out the break, as the compiler won't complain about the "mistake":

switch(x) {

case 1: y = y + 5;

default: y = y + 7;

}

In this case, y will be incremented by 5, then incremented by an additional 7. Comparing this to the original code example, that is clearly not what we intended. If the if statement had been written incorrectly, it likely would have been detected by the compiler.

However, on the downside, nested if statements are difficult to read, such as if statements that run through dozens of scenarios, like a dispatch handler, for example. In this case, switch statements are usually more appropriate, but will require extra consideration to make sure that there are no accidental side effects.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

switch statement is a substitute for the if..else statement. The switch statement tests the value of a variable or expression against the case values used in the switch block. If any of the case value matches with the value of the variable then the statements corresponding to the case value are executed and all the other case values are skipped. suppose a c program as follows:

main()

{char x;

printf("Do you love chocolates (y/n)");

scanf("%c",&x);

switch(x) \\testing the value of x entered by user

{

case 'y': \\if the entered value is y

printf("you should buy cadbury");

break;

case 'n':

printf("sorry no other items"):

break;

default: \\if the value given by the user neither matches with y and n

printf("error");

getch()

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

Nested if statement increases the number of conditions in a block of code. It refers to the use of if or else if statement inside another if or else if statement.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

If statement is the expression statement which only executes true part of logical test & if many logical test are defined within a if statement then it is called nested if statement

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

Both usable in C. And in Java, too.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is an if or nested if statement?
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


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 && *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.


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}}


Can you have nested switch case statement in C?

If that's what you really want to do, the compiler will allow it. It might indicate an inefficient design, though.


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 if statement that appears inside another if statement?

nested if Statement


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 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.


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 && *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.


What is linear nested IF statement?

Its when a field is being tested for various values and different action is to be taken for each value.


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 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.


Can you have nested switch case statement in C?

If that's what you really want to do, the compiler will allow it. It might indicate an inefficient design, though.


What is nested logic?

In Nested Logic a Logic is contained within a Logic. If the Outer Logic is TRUE then the internal Logic is executed. Nested IF, Nested For, Nested While, e.t.c are some examples of Nested Logic in Modern Computer Languages.