answersLogoWhite

0


Best Answer

The switch statement evaluates an expression, which must have an integral result. Based on that result, a branch is taken to one of the case statements, or to the default statement if present and none of the case statements match.

Note that the case statement represents a "goto" type of operation. If it is intended that each case execute without executing the following case statement(s), then each case must include a break statement.

The if-else statement evaluates an expression, which must have a logical result. If the result is true, the first target statement executes - if false, the second target statement executes.

User Avatar

Wiki User

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

Wiki User

13y ago

if statement is used when we have to check two condition for example

if the condition associated to if is satiafied than the statement associated with that is executed.

void main()

{

int i ;

printf ("enter the value of i");

scanf("%d",&i);

if (i==1)

{

printf("the no is one ");

}

printf("no is not one ");

}

switch:-switch is a multiconditional control statement .in this we check different conditions

in this we uses different cases if case is satisfied the statement associated to that case is executed.

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

Functionally, or logically, they amount to the same thing. You are looking to do some logic based on a match with some data or variable.

My own personal opinion is that a 'switch' statement is easier to read and add logic to then a nested 'if' statement. Also, in some implementations, the 'switch' statement is a hardware instruction and will generate better machine code.

This answer is:
User Avatar

User Avatar

Wiki User

7y ago

An if statement evaluates a boolean expression and executes a statement if the boolean expression evaluates is true.

A switch statement evaluates an integral expression and jumps to the appropriate case label. Execution continues through subsequent cases unless or until a break statement is encountered.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

Syntax and semantics.

--- added on ---

switch operates on 1 thing with boolean operator 2)

...

else

...

But it will be very difficult to convert the if-statement below as a switch statement:

if (n > 2 and k < 3)

{...}

else if (n < 3 and k >= 4)

{...}

else

{...}

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

Both can be used as condition based statements but their syntax is different.

Switch is used when a variable is to be compared with different constants.Switch statement has the syntax as :

switch(choice)

{

case 1: statements

break;

case 2': statements

break;

.

.

case 'n': statements

break;

default: statements

break;

}

where choice is an integer or character variable.

if else statements take the form:

if(cond1)

{

statements;

}

else if(cond2)

{

statements;

}

.

.

else {

statements;

}

where cond1 and cond 2 areany valid arithmetic, logical or relational expression.

This answer is:
User Avatar

User Avatar

Wiki User

8y ago

An if statement is used to evaluate a boolean expression and make a conditional jump according to the result of that evaluation, which can only be true or false. If true, the body of the if statement executes, otherwise it does not. An if else if statement is an extension of this, such that when the first boolean expression is false, control passes to the next if statement which evaluates another boolean expression.

A switch is used to evaluate just one expression (which need not be a boolean expression) and passes control to the label that matches the result of that expression. Execution then continues from that point on, falling through any subsequent case labels until a break, a return or a goto statement is encountered. A default label can be used to catch any results that are not explicitly defined by a case label.

Although it is possible for an if else if statement to mimic a switch statement, this may result in code that is less than optimal:

void f (int x) {

if (x==0) {/* ... */ }

else if (x==1) {/* ... */ } else if (x==2) {/* ... */ }

else if (x==3) {/* ... */ }

else {/* ... */ }

}

This function would be better implemented using a switch:

void g (int x) {

switch (x) {

case 0: {/* ... */ }; break;

case 1: {/* ... */ }; break;

case 2: {/* ... */ }; break;

case 3: {/* ... */ }; break;

default: {/* ... */ }

}

}

To understand why, consider what happens when x is neither 0, 1, 2 or 3. If we invoke g(4), for instance, we evaluate x and control immediately passes to the default label (because none of the other case labels is 4). However, if we invoke f(4), we'd have to perform four evaluations: x==0 (false); x==1 (false); x==2 (false); and x==3 (false); before we reach the final else statement.

As well as improving performance, the switch also allows us to control the flow of execution. The break statements allow us to exit each individual case, but if we remove any of these breaks then execution will simply fall through to the next case, and the next, until a break or return is encountered. Thus the ordering of cases can be important to ensure execution flows correctly from one label to the next (even the default label need not be the final label).

For an example of execution falling through case labels, a function that tests if a character is a vowel or not might be written as follows:

bool is_vowel (const char x) {

switch (x) {

case 'a':

case 'e':

case 'i':

case 'o':

case 'u': return true;

}

return false;

}

Writing this same function using an if statement:

bool is_vowel (const char x) {

if (x=='a' x=='e' x=='i' x=='o' x=='u') return true;

return false;

}

We can even write this function without the if at all:

bool is_vowel (const char x) {

return x=='a' x=='e' x=='i' x=='o' x=='u';

}

Often when we have a choice of implementations, the one that is easier to read and that best expresses the logic is usually the best choice (the 'keep simple concepts simple' principal). However, we must also consider which is the most efficient implementation. In this case, if x were 'u', we'd have to evaluate 5 equality operators and 4 logical OR operators to determine that the complete expression is actually true. But with the switch version, we only need to evaluate x once and immediately jump to the 'u' label, which returns true.

A good compiler will recognise that all three functions do exactly the same thing and will generate the most optimal code (the equivalent of the switch statement) regardless of which version we use. However, only by examining the machine code can we determine if that is actually the case. If not, we must write the optimal code by hand, but that need not mean sacrificing readability.

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

A switch statement is a bit like a series of if-else-if statements. Consider the following:

void f (int i)

{

if (i==1) {/* ... */}

else if (i==2) {/* ... */}

else if (i==3) {/* ... */}

else {/* ... */}

}


In the above example, each if statement has to be evaluated in sequence until one is found to be true. So if i were zero, that's three evaluations in total. Rewriting this code using a switch statement reduces the number of evaluations to just one:


void f (int i)

{

switch (i)

{

case 1: /* ... */; break;

case 2: /* ... */; break;

case 3: /* ... */; break;

default: /* ... */;

}


Each case label represents a possible value for i. Execution immediately jumps to the appropriate case label and continues from there until a break, return or goto statement is encountered.


In some cases it would be desirable to consider two or more possible values, such as when i is either 1 or 2:


void f (int i)

{

if (i==1 i==2) {/* ... */}

else if (i==3) {/* ... */}

else {/* ... */}

}


We can achieve this more efficiently with a switch as well:


void f (int i)

{

switch (i)

{

case 1:

case 2: /* ... */; break;

case 3: /* ... */; break;

default: /* ... */;

}


Here, case labels 1 and 2 mark the same piece of code because case 1 has no code of its own and doesn't have a break. Therefore execution flows through to case 2 whether i is 1 or 2.


The programmer clearly has more control over the flow of execution in a switch statement than with the simpler if-else-if statements, thus making it possible to perform more complex conditional branching using a simpler logical structure.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Difference between if and switch in C?
Write your answer...
Submit
Still have questions?
magnify glass
imp