Lets continue with the statements …….

  • SELECTION STATEMENT:SWITCH STATEMENT

These are similar to multiple if -else statement but its more easy to understand and it doesnot create any confusion for the programmer while writting the program . More over in if -else the complier is bound to check each and every if and else whether the condition matches or not , which is not the case with the switch statement . Here only the type of input is checked if it matches with the case then it follows and execute that particular case only . In this way lot of time and memory is saved .

SO its better to use switch when there are many cases else if 2-3 cases are there then we can use either of them .

SYNTAX

switch(variable) // variable with respect to which you want your cases
{
case a : statements :
break;
case b : statements :
break;
case c : statements :
break;
.
.
.
.
default:statement;
break;
}

here the letters used after the keyword case are the options which are expected from the user to be entered . It can be a number an alphabet ( in this case just write them under single qoutes example : ‘a’ or ‘A’)

a break is given after every case because once our case is found we dont want to waste compliers time and memory hence it is used . Its not necessary to use it .

lastly a default is used here when none of the options matches then complier execute this block .

lets consider the following example , which will clear all your doubts .

EXAMPLE
2.15
2.16
here in this example a menu with options was provided i.e it is expected that user will enter either 1,2 or 3 mow when user enters the option then the case is matched i.e 3rd case which is multiplication then the switch case 3 is invoked and executed and hence the output is obtained .