C Programming for Beginners

Switch Statements

Switch Statements in C

Switch Statements in C

In previous chapters we have studied basic operators, IF and ELSE conditions and different types of loops.

You can view all the source code and additional examples in the given url Newtum github repository  link over here.

In this chapter we will study about switch statements.

switch statements

Does the word switch ring any bells to you? Well it’s a simple switch. When  press 1, it should do this thing, when press 2 it should do that thing. 

To use Switch Statements we will write a program, which will accept input as a number, and will say you are in switch 1, switch 2 etc.

Let’s write the program. 

Now we will write printf statement , which will prompt user to enter the input. Then a scanf statement. Then we write switch statement. We write switch and in round brackets we put the switch case variable. In our case its i. 

Switch statement is followed by curly brackets. Inside curly brackets we write CASES. So in our program we will write

case 1: And inside case 1 we write printf saying “You have pressed 1 st Switch” Well this is the syntax of writing switch case statement. We can have multiple cases inside a switch.

So here we will write switch

case 2: And inside that we will put printf saying , “You have pressed 2nd Switch”

We will write case 3 and case 4 as well, 

Now here is the special case called as default case. In switch statement it’s a must to include a default case. Suppose you have only 4 cases and user presses 7. Then switch goes to default case.

You can add a default case. No need of case word just write default.

Lets run the program.

main()
{

int i;
printf(“enter the value : “);
scanf(“%d”,&i);
switch(i)
{
case 1:
printf(“You have pressed 1st Switch”);
break;
case 2:
printf(“You have pressed 2nd Switch”);
break;
default:
printf(“default case”);
}

}

You can view the source code from Newtum github repository  link over here.

Output :

enter the value : 1
You have pressed 1st Switch

system is asking for switch number. Now we will enter 3. Well we got output that , “You pressed 3rd Switch” and “You Pressed 4th Switch” and also the default switch. But this is the wrong output. Because we have just pressed 3 rd switch. This is because we forgot to add the break statement.

Let me explain to you again. Look at the code. Here when we enter 3 , switch directly jumps to 3rd case , but it can’t avoid case 4 and default case. To handle this we need to add break after every case.

We will add break in case 1,2,3,4. No break is required in default case since its the last.

Let’s execute the program again.

main()
{

int i;
printf(“enter the value : “);
scanf(“%d”,&i);
switch(i)
{
case 1:
printf(“You have pressed 1 st Switch”);
break;
case 2:
printf(“You have pressed 2nd Switch”);
break;
case 3:
printf(“You have pressed 3rd Switch”);
break;
default:
printf(“default case”);
}

}

This is our output screen. Now we will enter 2. Well we got the correct output.

Output :

enter the value : 2
You have pressed 2nd Switch

Now run the program again and enter the input 6. Well we got the default case output. It’s because case 6 is not available. Whenever we enter an input which is not available as a case, system goes to default case.

Output :

enter the value : 6

default case

Let’s write a more meaningful example. Suppose you want to

write a program which will do addition, subtraction, multiplication and division depending upon the user’s choice

In this example we don’t want case 1,2,3. We will have A for addition, S for subtraction, M for Multiplication, D for division.

Let’s open our editor again. Here we will declare 3 variables, variable input 1, input 2 and result to perform the operation. And another character variable “option” to take the input form user for performing operation.

Now we will prompt the user to enter the input1 and input2 using printf statement. We will accept the input using scanf statement. Here we have a float variable to ensure that division gives the correct output.

Now we will write printf statements. Formatted nicely. 

Please enter A for Addition , Please enter S Subtraction. Same for multiplication and Division.

In the end we will write please press E for Exit. Now we will accept the user input in the variable option.

write switch statement, Input value to switch will be “option” variable. we will write case A for addition. Please make a note that we have enclosed A in single inverted commas. character has to be enclosed in single inverted commas.

We will write switch statements like, In case A, we write result = input1 + input2.

Similarly for case S, we will write result = input1 – input2.

Inside case of Multiplication i.e M, we will write result = input1 * input2 and For Division case D,

result = input1 / input2 in the end we will write Case E for Exit function (exit(0)) all Switch statements follwed by Break statement except Exit case.

Exit will terminate the program immediately without going further.

Remember you don’t have to write break after exit since exit means end.

Now we will write default case , and in default case we will print “Sorry Wrong choice ” using printf statement and we will add exit after this.

main()
{

char option;
int input1, input2, operation, result;
printf(“Please enter\n A for addition,\n S for subtraction,\n M for Multiplication,\n D for division,\n E for exit : “);
scanf(“%c”,&option);

printf(“\nEnter value for input1 : “);
scanf(“%d”,&input1);
printf(“\nEnter value for input2 : “);
scanf(“%d”,&input2);

switch(option)
{
case ‘A’:

result = input1 + input2;
printf(“%d + %d = %d”, input1, input2, result);
break;
case ‘S’:
result = input1 – input2;
printf(“%d – %d = %d”, input1, input2, result);
break;
case ‘M’:
result = input1 * input2;
printf(“%d * %d = %d”, input1, input2, result);
break;
case ‘D’:
result = input1 / input2;
printf(“%d / %d = %d”, input1, input2, result);
break;
case ‘E’:
exit(0);
default:
printf(“Sorry Wrong choice”);
exit(0);
}

}

Well this is our switch  statement. After this we will write printf to display the result.

Let’s execute the program. We will input 10 and 3. Now system is asking for options. Let’s give S for Subtraction.

You can view the source code from Newtum github repository  link over here.

Output :

Please enter
A for addition,
S for subtraction,
M for Multiplication,
D for division,
E for exit : S

Enter value for input1 : 10

Enter value for input2 : 3
10 – 3 = 7

And we got the output 7.

Now run the program again.  Now we select L. Well system said “Sorry Wrong choice”.

Please enter
A for addition,
S for subtraction,
M for Multiplication,
D for division,
E for exit : L
Sorry Wrong choice

Now we will again run the program and enter E. System exits.

You can execute this in your system and check all the other options as well.

Well this was about switch statement.  Please note that switch statement accepts only an integer and a character as input. If you declare that as a float it wont run.

We can solve all our use cases without using goto statement. We are not covering goto explanation in our video. But if you still want to go through this,

You can download the source code from Newtum github repository  link over here.

Generally people use switch case & often compare them with ELSE IF statements. Yes, we can write all our switch programs using ELSE IF. Depending upon the requirement, Software Developer chooses the best method.

Well this was the first season of C Language , here we have studied Basics of C , IF and ELSE Statements, Loops and Switch Statements.

In the next season we will learn topics like Functions, Pointers, Arrays, Strings, Structures, File Operations and many more.

All the examples that you have studied and additional examples on the same topic are available in the given url Newtum github repository  

 

Facebook Comments
(Visited 1 times, 1 visits today)