×
 Introduction
Introduction to Newbie Programmer Series Introduction to Computer The Computer Programming Language Setting up the Computer for Programming
 Lets Get Started !
Beginning C Programming Variables a Handbag ! Basic Variable types in C Basic Formatted Output Basic Formatted Input
 Loops
Looping Repeating Concept Different Kinds of Loops Advanced concepts and trick with Loops
 Operators
Operators, An Introduction Arithmetic Operators Relational Operators Logical Operators Bitwise Operators Miscellaneous Operators and Operators Precedence
 Controlling the Flow
Flow Charts Conditional Statements Using Goto and Label Break and Continue the loops Switch the Cases Some Practical Applications of Control Flow
 The program structure
The program Structure Functions More Concepts of Functions Scope of the Variables across functions How the C language itself Works ? Scope of the Variables across files Static Variables Auto and Register Variables #define PreProcessor Some more PreProcessors
 Arrays and Structures
Introduction to Arrays Arrays, some more Concepts Arrays, Cool Examples Structures Introduction Structure, Some more Concepts Structure, Some Examples
 File Management
Basics of File management in C Steps Involved with File Handling in C
 Miscellaneous
Inside Logic Gates – The Electronic Logic Binary Number System Mathematical Reasoning
Newbie Programmer
full book coming soon
by Shubham Ramdeo

Another way of controlling the flow  switch of case


 

switch

Welcome to the Newbie Programmer Series. In the last post (click here) we have learned how to use break and continue. We have learned how to use them as an alternative to the goto (click here). In this post we will learn about the switch statement. This is made for decision making and is easy alternative for if..else (click here). So if you are new to this series, please go to index (click here) and read out all the previous parts so that you can easily understand what is going down below.


Why use switch ?

Well you are thinking correct that we could have used if else here. But sometimes when the program have too many conditions and many nested if else etc the problem becomes too much complex. And the brackets { } makes it even worse. So in that case to make it simple, we use switch. Also if there are not so much logic operating then instead of using == again and again, switch makes it no need. Its easy and better option for making decisions based on variable values only.

Lets come to the coding stuffs.

How to use switch ?

The switch block look like this :

switch ( variable )
{
     case const-1 :
     //instructions
     break;
 
     case const-2 :
     //instructions
     break;
 
     ...
 
     default :
     //instructions
}

  1. What we do here is that a we take a variable and use it as a switch. For different values of the variables, different instructions will be performed.
  2. The break; is optional. It is used to skip all other and come out of this block of switch.
  3. The default : is optional. It is used as the default instructions if no cases are matched.
Note that there is no need of break; in default as it's the last case so the program will automatically come out of the block even if it's not there.

A simple Example

Look at the following piece of code.

#include<stdio.h>
main()
{
int like;

printf("What do you like the most ? \n");
printf("Enter your choice from 1 to 3 \n");
printf("(1) Video Games \n");
printf("(2) Music \n");
printf("(3) Manga \n");
printf("Enter Your Choice : ");

scanf("%d", &like);

switch(like)
     {
     case 1:
     printf("I love Super Mario. \n");
     break;

     case 2:
     printf("Michael Jackson is my favorite singer. \n");
     break;

     case 3:
     printf("Dragon Ball is the best.  \n");
     break;

     default :
     printf("Bye because you entered wrong choice ! \n");    
     }
}

OUTPUT :

What do you like the most ?
Enter your choice from 1 to 3
(1) Video Games
(2) Music
(3) Manga
Enter Your Choice : 3
Dragon Ball is the Best .
second try 
What do you like the most ?
Enter your choice from 1 to 3
(1) Video Games
(2) Music
(3) Manga
Enter Your Choice : 7
Bye because you entered wrong choice !
What is happening here is that I have used the variable like as a switch. With different values of like, different instructions are to be followed. Using scanf user will enter a choice which is saved in like variable. Then I applied the switch block as shown above. When the user enters something which is not one of the cases, like if I enter 5 instead of 1 or 2 or 3 here, the default case will be executed.

So that's it for today. In the next post, we are doing another interesting stuff. So stay connected.

 

Please share this as much as you can so that we can get connected with more and more people who want to learn programming.

© Shubham Ramdeo

Site Map
with
© Shubham Ramdeo