The switch statement

The switch statement in C++. Construction and Example. The switch statement is used when we have large data of the same variable type.

Construction: The switch statement

switch (variable)
{
case 1:
   statement
   break;
case 2:
   statement
   break;
case 3:
   statement
   break;
default:
   statement
   break;
}


For example, write a program in C++ that take roll numbers (from 300 to 303) and show their numbers obtained.

#include
using namespace std;
int main()
{
   int a;
   cout << "Please enter you Roll Number: ";
   cin >> a;
switch (a)
{
case 300:
   cout << "Marks Obtained: 566";
   break;
case 301:
   cout << "Marks Obtained: 490";
   break;
case 302:
   cout << "Marks Obtained: 600";
   break;
case 303:
   cout << "Marks Obtained: 765";
   break;
default:
   cout << "The Roll Number you entered is Incorrect";
   break;
}
}

RELATED PAGES
- If / else conditions
- The switch statement


Basic Financial Terms
Basic Financial terms in Corporate and Mathematical Finance
Financial Portfolio Analysis
Selection of assets, risk and return, and portfolio analysis
Financial Mathematics Notes
View the online notes for Financial Mathematics (CT1)
Financial Computing with C++
Learn Financial Computing with C++ step by step