C++ Tutorial





Español Français 中文 Deutsch Portuguese Japanese nederlands
   
 
C++ Tutorial
C++ Language Tutorial - Object Oriented Programing - OOPs History of C++
C++ Language Tutorial - Object Oriented Programing - OOPs Structure of C++
C++ Language Tutorial - Object Oriented Programing - OOPs C++ Datatypes
C++ Language Tutorial - Object Oriented Programing - OOPs Variables
C++ Language Tutorial - Object Oriented Programing - OOPs Constants
C++ Language Tutorial - Object Oriented Programing - OOPs Operators
C++ Language Tutorial - Object Oriented Programing - OOPs Control Structures
C++ Language Tutorial - Object Oriented Programing - OOPs Arrays
C++ Language Tutorial - Object Oriented Programing - OOPs Functions
C++ Language Tutorial - Object Oriented Programing - OOPs Class
C++ Language Tutorial - Object Oriented Programing - OOPs Predefined Functions
C++ Language Tutorial - Object Oriented Programing - OOPs I/O Functions
C++ Language Tutorial - Object Oriented Programing - OOPs String, Character Functions
C++ Language Tutorial - Object Oriented Programing - OOPs Mathematical Functions
C++ Language Tutorial - Object Oriented Programing - OOPs Time Date Functions
C++ Language Tutorial - Object Oriented Programing - OOPs Dynamic Allocation
C++ Language Tutorial - Object Oriented Programing - OOPs Utility Functions
C++ Language Tutorial - Object Oriented Programing - OOPs OOP's Concept
C++ Language Tutorial - Object Oriented Programing - OOPs Special Topics
C++ Language Tutorial - Object Oriented Programing - OOPs Type casting
C++ Language Tutorial - Object Oriented Programing - OOPs Feedback
C++ Language Tutorial - Object Oriented Programing - OOPs Ask Your Doubts
 





Switch Case - Control Structure


Tutorials Cpp

Topic

How is a Switch case used in C++?
Use of "Switch Case" Control Structure.



Explanation

Switch statement compares the value of an expression against a list of integers or character constants.The list of constants are listed using the "case" statement along with a "break" statement to end the execution. If no conditions match then the code under the default statement is executed.

Syntax:
   switch(expression)
     {  
       case constant1:
          Statements
          break
       case constant2:
          Statements
          break
            .
            .
      default
          Statements
     }
Example:

  #include <iostream.h>
  using namespace std;
  int main(void)
   {
    int day;
    cout << "Enter the day of the week between 1-7::";
    cin >> day;
    switch(day)
      {
         case 1:
             cout << "Monday";
             break;
         case 2:
             cout << "Tuesday";
             break;
         case 3:
             cout << "Wednesday";
             break;
         case 4:
             cout << "Thursday";
             break;
         case 5:
             cout << "Friday";
             break;
         case 6:
             cout << "Saturday";
             break;
         default:
             cout << "Sunday";
             break;
      }
    }

Result:
   Enter the day of the week between 1-7::7
   Sunday

In the above Control Structure example the "switch" statement is used to find the day of the week from the integer input got from the user.








Note
C++ is one of the most used programming languages in the world. Also known as "C with Classes". Hope you enjoy this tutorial. Do send your feedback or suggestions on this C++ tutorial. This is a copyright content.

Other Links

web hosting