Switch Statement

C++BasicsInput in C++Conditions → 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 <iostream>
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;
}
}

NEXT: Loops in C++