answersLogoWhite

0


Best Answer

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.

User Avatar

Wiki User

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

Wiki User

6y ago

The if and switch statements are selection statements allowing code to branch according to the evaluation of a control expression.

An if statement has the following syntax:

if (expression) {

statement; // executes when expression is true

}

if (expression) {

statement; // executes when expression is true

} else {

statement; // executes when expression is false

}

An else clause is only required when we wish to branch to one of two different statements. The braces are optional when the statements are simple statements but compound statements must always be enclosed in braces.

Either statement may be another if statement (a nested if statement), however the most interesting case is when the else statement body is a nested if:

if (x==1) {

statement;

} else if (x==2) {

statement;

} else if (x==3) {

statement;

} else { statement;

}

Here, if x==1 evaluates false then we must evaluate x===2. If that also evaluates false then we must evaluate x==3. So, for any given execution of this statement, we must perform at least one evaluation or as many as three evaluations to determine which of the four statements will actually be executed.

What we really want to do here is jump to the appropriate statement depending on the value of x, and that's precisely what a switch does:

switch (x) {

case 1:

statement;

break;

case 2:

statement;

break;

case 3:

statement;

break;

default:

statement;

}

Unlike the nested-if statement, the expression (x) is evaluated just once and control immediately passes to the appropriate case label. If the expression evaluates to anything other than the given case labels, control passes to the default label instead.

Note that the expression must evaluate to a value of integral type (either an integer or a character).

The break statements are necessary in order to prevent fall-through to the next case label. However, fall-through can be useful when two or more case labels must execute the same statement. Consider the following example which counts all the vowels in a string:

int count_vowels (char* str) {

int cnt = 0;

while (char c=*str++) {

switch (c) {

case 'a':

case 'A':

case 'e':

case 'E':

case 'i':

case 'I':

case 'o':

case 'O':

case 'u':

case 'U':

++cnt; // increment count for all specified cases

}

}

return cnt;

}

Implementing this same function using an if statement may require less code, but it is highly inefficient:

int count_vowels (char* str) {

int cnt = 0;

while (char c=*str++) {

c = tolower (c);

if (c=='a' c=='e' c=='i' c=='o' c=='u') ++cnt;

}

return cnt;

}

The inefficiency comes from the fact that every consonant requires 5 evaluations of c whereas the switch only requires the one evaluation. And while converting to lowercase means we can omit the evaluation of uppercase characters (which would require 10 evaluations for every consonant), that conversion has a runtime cost that we don't incur with the switch version.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What are the similarities between if else and switch case in c program?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Discount program using java switch case?

This is not a question.


Which comma is used for characters in switch case statements?

There is only one comma, but it is not used in switch-case. Character literals are between apostrophes: 'x'


Writ a program in c to display day of the week using in switch case?

/* write a program to print Days of Week using switch-case structure */ #include<stdio.h> #include<conio.h> void main() { int n; clrscr(); printf("\n Enter Day of weak as Number 1 to 7 "); scanf("%d",&n); switch(n) { case 1: printf("\n MONDAY "); case 2: printf("\n TUESDAY"); case 3: printf("\n WEDNESDAY"); case 4: printf("\n THURSDAY"); case 5: printf("\n FRIDAY"); case 6: printf("\n SATURDAY"); case 7: printf("\n SUNDAY"); default : printf("\n no operation is required"); } getch(); }


What is the meaning of fall through in java language?

A "fall through" is what we call it when a case in a switch statement doesn't end in a break, return, throw, or any other control-breaking statement. In these cases, program execution continues to the next case block (regardless of the value in the switch), and so control "falls through" to the case below it. Here is an example of a typical switch block: switch(n) { case 0: System.out.println("zero"); break; case 1: System.out.println("one"); case 2: System.out.println("two"); break; } Notice the break statements in cases 0 and 2. These are used to break out of the switch block so that only one value is printed out. If n is set to 1 before executing this code, "one" would be printed out and then the program would continue to the next statement and print out "two" as well. That is a fall through.


Write a java program using switch case to find zodiac signs?

Let's say you want a method which will determine if the given character is a vowel, consonant, or other (non-letter). // Will return a String representation of what the given character is: // "vowel" "consonant" or "other" public static final String getTypeOfChar(final char c) { // since chars are an integer data type in Java, we can switch on them switch(c) { case 'a': // all of these cases "fall through" to the next non-case statement case 'e': // if any of them matches case 'i': case 'o': case 'u': return "vowel"; case 'b': // again, all of these cases fall through case 'c': case 'd': case 'f': case 'g': case 'h': case 'j': case 'k': case 'l': case 'm': case 'n': case 'p': case 'q': case 'r': case 's': case 't': case 'v': case 'w': case 'x': case 'y': case 'z': return "consonant"; default: // if we have no matches yet, do this return "other"; } }

Related questions

What are the similarities between if else and switch case?

If else and switch case both are used to control the flow of program.


Discount program using java switch case?

This is not a question.


What is case in java?

Case is used to label each branch in the switch statement in Java Program


List out the similarities between call-ret and push pop instructions?

In the case of call-return, that something is the program counter. In the case of push-pop, that something is up to the programmer, and it can be one of AF, BC, DE, or HL.


I am using Borland C compiler and making a program using switch cases while loading program I'm getting error that case bypasses initialization of a local variable case 4?

You cannot declare variables inside a case label. Declare them outside of the switch.


Which comma is used for characters in switch case statements?

There is only one comma, but it is not used in switch-case. Character literals are between apostrophes: 'x'


How does the case statement work in c?

prog1: #include<stdio.h> int main() { switch(1) { int i=0; case 1:printf("%d",i); } getchar(); return 0; } Output: garbage value. prog2: #include<stdio.h> int main() { switch(1) { printf("Inside Switch"); case 1:printf("Case 1\n"); } printf("Outside Switch"); getchar(); return 0; } Output: Case 1 Outside Switch. The statements before a case labelled statement seem unreachable according to program 2 but then why don't i get an error for an undeclared variable i in the first program (only a warning). Would be really helpful if someone could explain in detail that how the switch statement is treated internally.


What is output if we don't write break statement in switch-case in c sharp?

This question cannot be generally answered, the output depends on the actual program.


Writ a program in c to display day of the week using in switch case?

/* write a program to print Days of Week using switch-case structure */ #include<stdio.h> #include<conio.h> void main() { int n; clrscr(); printf("\n Enter Day of weak as Number 1 to 7 "); scanf("%d",&n); switch(n) { case 1: printf("\n MONDAY "); case 2: printf("\n TUESDAY"); case 3: printf("\n WEDNESDAY"); case 4: printf("\n THURSDAY"); case 5: printf("\n FRIDAY"); case 6: printf("\n SATURDAY"); case 7: printf("\n SUNDAY"); default : printf("\n no operation is required"); } getch(); }


What is the meaning of fall through in java language?

A "fall through" is what we call it when a case in a switch statement doesn't end in a break, return, throw, or any other control-breaking statement. In these cases, program execution continues to the next case block (regardless of the value in the switch), and so control "falls through" to the case below it. Here is an example of a typical switch block: switch(n) { case 0: System.out.println("zero"); break; case 1: System.out.println("one"); case 2: System.out.println("two"); break; } Notice the break statements in cases 0 and 2. These are used to break out of the switch block so that only one value is printed out. If n is set to 1 before executing this code, "one" would be printed out and then the program would continue to the next statement and print out "two" as well. That is a fall through.


Case studies are particularly accurate due to similarities?

Case studies can provide in-depth insights into specific phenomena or situations by examining similarities within a particular group or context. By focusing on detailed information and patterns, case studies can offer a rich understanding of complex issues and how they unfold in real-life scenarios.


What are similarities and differences between computer case and system unit?

System unit refers to the housing plus the components inside the box. While a computer case is just a 'case' itself w/o the hardware components attached to it.