Wednesday, 8 January 2014

C Programming Language Tutorial VI-switch statement, break, goto

Menu driven programming
Whenever you go to hotel then you will order items available in menu card. You made order by your choice. Like this we create a program designed with menu and user can enter his/her choice and will get output.
switch - case statement
Switch is very useful for designing menu driven programs.
It is similar to if statement but only the difference is switch execute result by checking value of single variable pass to it.
It supports multi-way decision statement. It allows user to choose a statement among several alternatives.
The switch statement tests the value and then executes the result through different cases. At a time only one case gets executed because every case ends with break statement.
The default part of switch is executed only when none of the case is satisfied.
switch(expression)
{
  case value1:
                    statement;
                    break;
  case value2:
                   statement;
                   break;
  case value3:
                  statement;
                  break;
   |
   |
   |
  case valuen:
                statement;
                break;
   default:
               statement;
}
break Statement
We have already use break statement with switch. This break statement can be use with any looping structure. When a break statement is encountered inside a loop, the loop gets terminated and program control is transferred to the statement immediately after the loop.
Let us see the example for easily understanding of switch statement.
//Prog to perform mathmatical operations.
#include<stdio.h>
#include<conio.h>
void main()
{
 int no1,no2,add,mul,sub;
 float div;
 clrscr();
 printf("\nEnter any two numbers");
 scanf("%d%d",&no1,&no2);
 add = no1 + no2;
 mul = no1 * no2;
 sub = no1 - no2;
 div = no1 / no2;
 printf("\nAddition = %d",add);
 printf("\nSubstraction = %d",sub);
 printf("\nMultiplication = %d",mul);
 printf("\nDivision = %f",div);
 getch();
}
In this program we will get output as:
Enter any two numbers
20
5
Addition = 25
Subtraction=15
Multiplication = 100
Division = 4.0
That is if we only want to check addition then also we got all things. Now let convert this program to menu driven program by using switch then we will enter our own choice for output.
//program using switch statement
#include<stdio.h>
#include<conio.h>
void main()
{
 int no1,no2,choice;
 clrscr();
 printf("\nEnter two numbers");
 scanf("%d%d",&no1,&no2);
 printf("\n==================================");  //menu list
 printf("\n1 . Addition");
 printf("\n2 . Subtraction");
 printf("\n3 . Multiplication");
 printf("\n4 . Division");
 printf("\n5 . Exit");
 printf("\n===================================");
 printf("\nEnter your choice: "); //accepts choice from user
 scanf("%d",&choice);
 switch(choice)  //switch starts working by checking value of choice
 {
   case 1:
              printf("\nAddition = %d", no1+no2);
              break;
   case 2:
               printf("\nSubtraction = %d",no1-no2);
               break;
   case 3:
               printf("\nMultiplication = %d",(no1+no2)/2);
               break;
   case 4:
               printf("\nDivision = %d",no1/no2);
               break;
   case 5:
               exit(0);  //library function for exiting program
   default :   //optional        
              printf("\nWrong Entry");
 }
 getch();
}
o/p:
Enter two numbers
20
5
1 . Addition
2 . Subtraction
3 . Multiplication
4 . Division
5 . Exit
Enter your choice: 3
Multiplication = 100
//You will continue by inputting different choices
Once you executed above program then you will find that after executing single case your program gets terminate. But if you want to continue program un till you select exit case then it is possible by using goto statement.
goto statement
It supports unconditional branching. A goto unconditionally transfers the program control from one point to another point within the program. It uses a label in order to identify the place where the program control needs to transfer.
In above program we first include label at the from where we want to repeat menu. Goto statement will place outside the switch.
//program using switch statement
#include<stdio.h>
#include<conio.h>
void main()
{
 int no1,no2,choice;
 clrscr();
 printf("\nEnter two numbers");
 scanf("%d%d",&no1,&no2);
 //mathmenu is a label - program will repeat from this point
 mathmenu:printf("\n==================================");  //menu list
 printf("\n1 . Addition");
 printf("\n2 . Subtraction");
 printf("\n3 . Multiplication");
 printf("\n4 . Division");
 printf("\n5 . Exit");
 printf("\n===================================");
 printf("\nEnter your choice: "); //accepts choice from user
 scanf("%d",&choice);
 switch(choice)  //switch starts working by checking value of choice
 {
   case 1:
              printf("\nAddition = %d", no1+no2);
              break;
   case 2:
               printf("\nSubtraction = %d",no1-no2);
               break;
   case 3:
               printf("\nMultiplication = %d",(no1+no2)/2);
               break;
   case 4:
               printf("\nDivision = %d",no1/no2);
               break;
   case 5:
               exit(0);  //library function for exiting program
   default :
              printf("\nWrong Entry");
 }
 goto mathmenu;  //label will jump on mathmenu and continue execution till you select exit case
 getch();
}
Following program is to calculate bonus of employee by given grade by using switch statement.

//prog to calculate bonus of emplyoee
#include<stdio.h>
#include<conio.h>
#define p printf  //define shorthand character p for printf and s for scanf
#define s scanf
void main()
{
 int ecode,exp,sal;
 char ename[10],dept[10],grd;
 float bon,netsal;
 clrscr();
 p("\nEnter employees code: ");
 s("%d",&ecode);
 p("\nEnter employees name: ");
 s("%s",&ename);
 p("\nEnter employees department: ");
 s("%s",&dept);
 p("\nEnter employees experience: ");
 s("%d",&exp);
 p("\nEnter employees basic salary: ");
 s("%d",&sal);
 p("\nEnter employees grade: ");
  flushall();    //library function used for scanning single character
 s("%c",&grd);
 if(exp>=2)
 {
  switch(grd)
  {
   case 'A':
                bon = sal * 0.3;
                break;
   case 'B':
                bon = sal * 0.2;
                break;
   case 'C':
                bon = sal * 0.1;
                break;
   case 'D':
                bon = sal * 0.05;
                break;
   default:
               bon = sal * 0.02;
  } //switch closed
 }  //if closed
 else
  bon = 0;
 netsal = sal + bon;
 p("\nBonus = %.2f",bon);
 p("\nNet Salary = %.2f",netsal);
getch();
}
o/p:
Enter employees code: 1
Enter employees name: Sachin
Enter employees department: Marketing
Enter employees experience: 10
Enter employees basic salary: 8000
Enter employees grade: B
Bonus = 1600.00
Net Salary = 9600.00

No comments:

Post a Comment