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.
Chat with our AI personalities
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 is not a question.
There is only one comma, but it is not used in switch-case. Character literals are between apostrophes: 'x'
/* 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(); }
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.
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"; } }