Important points to be noted while using a switch statement
In what ways does a switch statement differ from if statement?
Switch works faster than an equiavalent if-else ladder. How come?
The compiler generates a jump table for a switch during compilation. During execution it simply refers the jump table to decide which case should be executed. But in if-elses are slower because the conditions are executed at execution time.
Useful Tips to avoid pitfalls using pitfalls
1) The earlier example programs that used switch may give you the wrong impression that you can use only cases arranged in ascending order 1, 2, 3 and default. You can, put the cases in any order. Here is an example of scrambled case order.
#include <stdio.h> #include <conio.h> main() { int n; clrscr(); printf("Enter n: "); scanf("%d", &n); switch (n) { case 3: printf("Three"); break; case 1: printf("One"); break; case 2: printf("Two"); break; default: printf("Other Value"); } getch(); } Output:- Enter n: 1 One Enter n: 2 Two Enter n: 3 Three |
2) The earlier example programs that used switch may give you the wrong impression that you can use only cases must contain integer values. You are also allowed to use char values in case.
#include <stdio.h> #include <conio.h> main() { char ch; clrscr(); printf("Enter a character: "); scanf("%c", &ch); switch (ch) { case ‘v’: printf("I am in case V \n"); break; case ‘a’: printf("I am in case a \n"); break; case ‘x’: printf("I am in case x \n"); break; default: printf("Other Value"); } getch(); } Output:- Enter a character: a I am in case a Note: When we use ‘v’, ‘a’, ‘x’ they are actually replaced by the ASCII values (118, 97, 120) of these character constants. 3) A common set of statements can also execute for multiple cases. This can be done in the following program.
switch (Integer expression)
{ case constant: case constant: case constant: <statement> <statement> …………… …………… break; case constant: case constant: case constant: <statement> <statement> …………… …………… break; …………………………... …………………………… …………………………… …………………………… case constant: case constant: case constant: <statement> <statement> …………… …………… break; default: <statement> <statement> ……………. ……………. } Note: This version uses a switch to provide a more efficient implementation. If an alphabet a is entered, the case ‘r’ is satisfied and since there are no statements to be executed in this case, the control automatically reaches the next case i.e case ‘R’ and executes all the statements in this case.
#include <stdio.h>
#include <conio.h> main() { char ch; clrscr(); printf("Enter any alphabets : "); scanf("%c", &ch); switch (ch) { case ‘r’: case ‘R’: printf("Red Color \n"); break; case ‘g’: case ‘G’: printf("Green Color \n"); break; case ‘b’: case ‘B’: printf("Blue Color\n"); break; default: printf("Other Value"); } getch(); } 4) Every statement in a switch must belong to some case or the other. If a statement doesn’t belong to any case, the compiler won’t report an error. However, the statement would never get executed. For example in the following program, the printf("I doesn’t belong to any case"); never goes to work.
#include <stdio.h> #include <conio.h> main() { int n; clrscr(); printf("Enter n: "); scanf("%d", &n); switch (n) { printf("I doesn’t belong to any case"); case 1: printf("One"); break; case 2: printf("Two"); break; case 3: printf("Three"); break; default: printf("Other Value"); } getch(); } 5) Even if there are multiple statements to be executed in each case, there is no need to enclose them within a pair of braces (unlike if, and else).
6) If no default case, then the program simply falls through the entire switch and continues with the next instruction (if any) that follows the closing brace of switch 7) The break statement when used in a switch takes the control outside the switch. However, use of continue will not take the control to the beginning of switch as one is likely to believe. This is because switch is not a looping statement unlike while, for or do-while. 8) The value of any expression can check in a switch. Thus the following switch statements are legal. switch(m+n) switch(45+23 % k*4) switch(a<5 && b>8) 9) Expressions can also be used in case provided they are constant expressions. Thus, 3+7 is correct. However, case a+b is incorrect. 10) The switch statement is very useful while writing menu driven programs. Here, try to write Program to explain the use of switch statement for performing arithmetic operations with displaying menu’s. 11) A switch may occur within another. Nested Structure
A switch statement can use as part of the statement sequence of an outer switch. That is, a switch may occur within another switch. Such statements are called as nested switch statements. This is called nested switch. switch (Outer-switch-expression)
{ case constant: switch (Inner-switch-expression) { case constant: <statement> <statement> …………… …………… break; case constant: <statement> <statement> …………… …………… break; …………………………… …………………………… case constant: <statement> <statement> …………… …………… break; default: <statement> <statement> ……………. } <statement> <statement> …………… break; case constant: <statement> <statement> …………… …………… break; …………………………... …………………………… …………………………… …………………………… case constant: <statement> <statement> …………… …………… break; default: <statement> <statement> ……………. ……………. } |
- Home
-
C Language
- C++
- Java
-
B.C.A
- Subject Details
- COBOL
- Digital Logic Fundamentals
- Fundamental of Digital Computers
- Microprocessor and its applications
- Programming in C
- Computer Graphics
- Operating System
- Programming in C++ and Data Structures
- DBMS
- Programming in Java
- Data Communication and Networking
- Software Engineering
- Software Testing
- Web Technology
- Elective - I >
- Elective - II >
- Elective - III >
- Maths Questions >
- English
-
B.Sc
- B.Sc (Cs) Subject Details
- Computer Architecture and Organization
- Digital Electronics and Microprocessor
- Data Structure Using C++
- Microprocessor and Its Applications
- Programming in C
- Programming in C++ and Data Structures
- Programming in Java
- Visual Programming
- Operating System
- DBMS
- Software Engineering
- Data Communication and Networking
- Software Testing
- Web Technology
- Computer Networks
- OOAD
- Multimedia Systems
- M.C.A
- M.Sc (CS)
- M.Sc (IT)
- Conduct Us
- Programming Language
- Programming Logic
- Computer Terms
- Job Selection Process
- Learn English Using Tamil
- Spoken English in Tamil
- Conversational Tamil
- English
- Interview