Skip to main content

Lesson 5: switch case in C and C++

Switch case statements are a substitute for long if statements that compare a variable to several "integral" values ("integral" values are simply values that can be expressed as an integer, such as the value of a char). The basic format for using switch case is outlined below. The value of the variable given into switch is compared to the value following each of the cases, and when one value matches the value of the variable, the computer continues executing the program from that point.
switch ( <variable> ) {
case this-value:
  Code to execute if <variable> == this-value
case that-value:
  Code to execute if <variable> == that-value
  Code to execute if <variable> does not equal the value following any of the cases
The condition of a switch statement is a value. The case says that if it
 has the value of whatever is after that case then do whatever follows 
the colon. The break is used to break out of the case statements. Break 
is a keyword that breaks out of the code block, usually surrounded by 
braces, which it is in. In this case, break prevents the program from 
falling through and executing the code in all the other case statements.
 An important thing to note about the switch statement is that the case 
values may only be constant integral expressions. Sadly, it isn't legal 
to use case like this:
int a = 10;
int b = 10;
int c = 20;

switch ( a ) {
case b:
  // Code
case c:
  // Code
  // Code
The default case is optional, but it is wise to include it as it handles
 any unexpected cases. Switch statements serves as a simple way to write
 long if statements when the requirements are met. Often it can be used 
to process input from a user.

Below is a sample program, in which not all of the proper functions are 
actually declared, but which shows how one would use switch in a 
#include <iostream>

using namespace std;

void playgame()
    cout << "Play game called";
void loadgame()
    cout << "Load game called";
void playmultiplayer()
    cout << "Play multiplayer game called";
int main()
  int input;
  cout<<"1. Play game\n";
  cout<<"2. Load game\n";
  cout<<"3. Play multiplayer\n";
  cout<<"4. Exit\n";
  cout<<"Selection: ";
  cin>> input;
  switch ( input ) {
  case 1:            // Note the colon, not a semicolon
  case 2:            // Note the colon, not a semicolon
  case 3:            // Note the colon, not a semicolon
  case 4:            // Note the colon, not a semicolon
    cout<<"Thank you for playing!\n";
  default:            // Note the colon, not a semicolon
    cout<<"Error, bad input, quitting\n";
This program will compile, but cannot be run until the undefined 
functions are given bodies, but it serves as a model (albeit simple) for
 processing input. If you do not understand this then try mentally 
putting in if statements for the case statements. Default simply skips 
out of the switch case construction and allows the program to terminate 
naturally. If you do not like that, then you can make a loop around the 
whole thing to have it wait for valid input. You could easily make a few
 small functions if you wish to test the code. 

Popular posts from this blog


Agricultural Research Service

Eligibility Test/ Senior Research Fellowship Examination 

The Agricultural Scientists Recruitment Board (ASRB)
 holds a Competitive Examination for recruiting Scientists of the ARS in the pay scale of Rs. 8,000-13,500 in the ICAR Institutes, combined with National Eligibility Test (NET) for recruitment of Lecturers and Assistant Professors by the State Agricultural Universities (SAUS) and for award of ICAR Senior Research Fellowships.

The selected candidates for Agricultural Research Service must serve in the institutes to which they are posted until they find appointment for higher positions through selection at other institutes.

(i) Candidates successful in ARS are appointed as Scientists in the Indian Council of Agricultural Research in the pay scale of Rs. 8,000-13,500.

(ii) Candidates clearing the National Eligibility Test are recommended to various State Agricultural Universities who will consider them for appointment as Lecturers or Assistant Pro…


The Union Public Service Commission (U.P.S.C.) conducts Civil Services' Examination once a year in two stages. The Preliminary Examination (Objective Type) for selection of candidates for the Main Examination is held in the month of May.

The Civil Services Main Examination
 is held in the months of October/November. Blank application forms and other particulars are published in the Employment News, generally in the month of December.

The last date for the submission of applications to the Secretary, Union Public Service Commission, Dholpur House, Shahjahan Road, NewDelhi-11001 1 is usually the last week of January of the year of examination.

The Combined Civil Services Examination is conducted for Recruitment to the following Services/Posts:

1. Indian Administrative Service.
2. Indian Foreign Service.
3. Indian Police Service.
4. Indian P & T Accounts & Finance Service, Group 'A'.
5. Indian Audit and Accounts Service, Group 'A'.
6. Indian Customs and Central Excise S…

Aptitude Test Practice Questions - With Answers

Question 1. Which of the following is least like the others?
A.    cube
B.    sphere
C.    pyramid
D.    circle

D (because the circle is the only two-dimensional figure)

Question 2. Consider a language which uses the following set of characters:
Small set: { a b c }
Large set: { A B C }
Punctuation set: { x y }
This language must follow the following rules:
  1.    A punctuation character must end all series.
  2.    A series can have up to but no more than 4          characters,including punctuation characters.

Does the following series follow all the rules of the language defined above?
  A.    Yes
  B.    No

A (the series has only four characters and ends in a punctuation character)

Question 3. Consider the following flow chart for a customer:

The person in No.1 is:
    A.   Married, with children
    B.   Married, with at least one son
    C.   Unmarried, with at least one daughter
    D.   Unmarried, with at least one son
    E.   Unmarried, with no children