rev='made'/> name='keywords'/> name='author'/> ANURANAN: C Programming: Switch Case

Welcome Note

Hi,
I'm Anisuzzaman Babla . I am a full stack Android Application Developer. I am working with Android for more than two years. I have completed my graduation from IIT, Jahangirnagar University. I have developed a series of commercially successful apps on Android. Moreover, 10+ of my Apps are live at Google play store. .
Thank You

C Programming: Switch Case


Switch case statements are a substitute for long if statements that compare a variable to several "integral" values ("integral" values are simply values that can be expressed as an integer, such as the value of a char). The basic format for using switch case is outlined below. The value of the variable given into switch is compared to the value following each of the cases, and when one value matches the value of the variable, the computer continues executing the program from that point.
switch ( <variable> ) {
case this-value:
if <variable> == this-value break; case th
Code to execut eat-value: Code to execute if <variable> == that-value
s not eq
break; ... default: Code to execute if <variable> do eual the value following any of the cases break;
}
int a = 10;
int b = 10;
switch ( a
int c = 20; ) {
*/ b
case b: /* Cod ereak; case c:
break; defau
/* Code */ lt: /* Code */ break;
}
The default case is optional, but it is wise to include it as it handles any unexpected cases. It can be useful to put some kind of output to alert you to the code entering the default case if you don't expect it to. Switch statements serve as a simple way to write long if statements when the requirements are met. Often it can be used to process input from a user. 

Below is a sample program, in which not all of the proper functions are actually declared, but which shows how one would use switch in a program.
#include <stdio.h>
void playgame() {
ame called" ); } void loadgame()
printf( "Play g{ printf( "Load game called" ); }
lay multiplayer game c
void playmultiplayer() { printf( " Palled" ); } int main() { int input;
"2. Load game\n" ); printf(
printf( "1. Play game\n" ); printf( "3. Play multiplayer\n" ); printf( "4. Exit\n" );
switch ( input ) {
printf( "Selection: " ); scanf( "%d", &input );
*/ playgame(); break; case
case 1: /* Note the colon, not a semicolo n 2: loadgame(); break; case 3: playmultiplayer();
default:
break; case 4: printf( "Thanks for playing!\n" ); break; printf( "Bad input, quitting!\n" ); break; } getchar();
}
This program will compile, but cannot be run until the undefined functions are given bodies, but it serves as a model (albeit simple) for processing input. If you do not understand this then try mentally putting in if statements for the case statements. Default simply skips out of the switch case construction and allows the program to terminate naturally. If you do not like that, then you can make a loop around the whole thing to have it wait for valid input. You could easily make a few small functions if you wish to test the code. 

No comments:

Post a Comment