Friday 30 August 2013

The “switch-case” Statement | C Programming Language Tutorial pdf



The “switch-case” Statement

=> The switch statement causes a particular group of statements to be chosen from several available groups.
=> The selection is based upon the current value of an expression which is included within the switch statement.
=> The switch statement is a multi-way branch statement.
=> In a program if there is a possibility to make a choice from a number of options, this structured selected is useful.
=> The switch statement requires only one argument of int or char data type, which is checked with number of case options.
=> The switch statement evaluates expression and then looks for its value among the case constants.
=> If the value matches with case constant, then that particular case statement is executed.
=> If no one case constant not matched then default is executed.
=> Here switch, case and default are reserved words or keywords.
=> Every case statement terminates with colon “:”.
=> In switch each case block should end with break statement, i.e.

Syntax:
switch(variable or expression)
{
case Constantvalue-1: Block -1;
(or)
Statement-1;
break;
case Constantvalue-2: Block -2;
(or)
Statement-2;
break;
_ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _
case Constantvalue-n: Block -n;
(or)
Statement-n;
break;
default: default – block; (or) Statement;
}

(i) The switch( ) Organization:
=> The entire case structure following switch( ) should be enclosed with pair of curly braces { }.
=> In the block the variable or expression can be a character or an integer.
=> Each case statement must contain different constant values.
=> Any number of case statements can be provided.
=> If the case structure contains multiple statements, they need not be enclosed within pair of curly braces.

(ii) The switch( ) Execution:
=> When one of the cases satisfies, the statements following it are executed.
=> In case there is no match, the default case is executed.
Flow Chart:
Write a program to provide multiple functions such as 1. Addition 2. Subtraction 3. Multiplication 4. Division 5. Remainder 6. Larger out of two 7. Exit using “switch” statement.
# include<stdio.h>
# include<conio.h>
main( )
{
int a, b, c, ch;
clrscr ( ) ;
printf(“\t = = = = = = = = = = = = = =”);
printf (“n\t MENU”);
printf(“\n\t= = = = = = = = = = =”);
printf(“\n \t [1] ADDITION” );
printf(“\n \t [2] SUBTRACTION” );
printf(“\n \t [3] MULTIPLICATION” );
printf(“\n \t [4] DIVISION” );
printf(“\n \t [5] REMAINDER” );
printf(“\n \t [6] LARGER OUT OF TWO” );
printf(“\n \t [7] EXIT” );
printf(“\n \t = = = = = = = = = =”);
printf(“ \n\n\t ENTER YOUR CHOICE:”);
scanf(“%d”, &ch);
if(ch < = 6 && ch >=1)
{
printf(“ENTER TWO NUMBERS:”);
scanf(“%d %d”, &a, &b);
}
switch(ch)
{
case 1: c = a+b ;
printf(“ \n Addition: %d”, c);
break;
case 2: c=a-b;
printf(“\n Subtraction: %d”, c);
break;
case 3: c = a* b ;
printf(“\n Multiplication: %d”, c);
break;
case 4: c = a / b;
printf(“\n Division: %d”, c);
break;
case 5: c = a % b;
printf(“ \n Remainder: %d”, c);
break;
case 6: if (a > b)
printf(“\n \t %d is larger than %d”, a, b);
else if (b > a)
printf(“ \n \t %d is larger than %d ”, b, a);
else
printf(“\n \t %d and %d are same”, a, b);
break;
case 7: printf( “ \ n Terminated by choice”);
exit( );
break;
default: printf(“ \ n invalid choice”);
}
getch ( );
}

Output:
= = = = = = = = =
MENU
= = = = = = = = =
[1] ADDITION
[2] SUBTRACTION
[3] MULTIPLICATION
[4] DIVISION
[5] REMAINDER
[6] LARGER OUT OF TWO
[7] EXIT
= = = = = = = = = = = = = = =
Enter your choice: 6
Enter two numbers: 8 9
9 is larger than 8
=> Write a program to display the traffic control signal lights based on the following.
=> If user entered character is R or r then print RED Light Please STOP.
=> If user entered character is Y or y then print YELLOW Light Please Check and Go.
=> If user entered character is G or g then print GREEN Light Please GO.
=> If user entered some other character then print THERE IS NO SIGNAL POINT.

# include<stdio.h>
# include<conio.h>
main( )
{
char L;
clrscr( );
printf(“ \n Enter your Choice( R,r,G,g,Y,y):”);
scanf(“%c”, &L);
switch(L)
{
case „R?:
case „r?: printf(“RED Light Please STOP”);
break;
case „Y?:
case „y?: printf(“YELLOW Light Please Check and Go”);
break;
case „G?:
case „g?: printf(“GREEN Light Please GO”);
break;
default: printf(“THERE IS NO SIGNAL POINT ”);
}
getch( );
}

Output:
Run-1:
Enter your Choice(R,r,G,g,Y,y): g
GREEN Light Please GO
Run-1:
Enter your Choice(R,r,G,g,Y,y): G
GREEN Light Please GO

Program:
Write a program to convert years into (1) Minutes (2) Hours (3) Days (4) Months (5) Seconds. Using switch ( ) Statements.
# include<stdio.h>
# include<conio.h>
main( )
{
long int ch, min, hrs, ds, mon, yrs, sec;
“Programming with C” course material
Department of CSE, GIT, GU Page 53
clrscr( ) ;
printf(“\n [1] MINUTES \n [2] HOURS \n [3] DAYS \n [4] MONTHS \n
[5] SECONDS \n [6] EXIT \n Enter your Choice:”);
scanf (“%d”, &ch);
if(ch > 0 && ch < 6)
{
printf(“Enter Years:”);
scanf(“%d”, &yrs);
}
mon = yrs * 12;
ds = mon * 30;
hrs = ds * 24;
min = hrs * 60;
sec = min * 60;
switch(ch)
{
case 1: printf(“\n Minutes : %ld”, min);
break;
case 2: printf(“ \n Hours : % ld”, hrs);
break;
case 3: printf(“ \n Days : %ld”, ds);
break;
case 4: printf( “ \n Months: %ld”, mon);
break;
case 5: printf(“ \n Seconds : %ld”, Sec);
break;
case 6: printf(“\n Terminated by Choice”);
exit( ) ;
break;
default: printf(“ \n Invalid Choice”);
}
getch ( ) :
}

0 comments:

Post a Comment

 

C Programming Language Interview Questions and Answers Tutorial for beginners. Copyright 2013 All Rights Reserved