CodesJava

Easy learning with example program codes

CPP Switch


CPP Switch statement is a conditional statement, which is used to choose one of the action to perform from the multiple actions, relative to the true condition.

Syntax:

switch (expression)

{

case value1:

          code to be executed if expression=value1;

break;

 

case value2:

            code to be executed if expression=value2;

break;

 

case value3:

            code to be executed if expression=value3;

break;

 

default:

          code to be executed if expression is different from all values;

}

 

Example:

#include <iostream.h>
using namespace std;
int main()
{
char ch;
cout << "Enter an alphabet: ";
cin >> ch;
switch (ch)
{
case 'a':
cout << "Vowel";
break;
case 'e':
cout << "Vowel";
break;
case 'i':
cout << "Vowel";
break;
case 'o':
cout << "Vowel";
break;
case 'u':
cout << "Vowel";
break;
default:
cout << "Consonant";
}
return 0;
}

Output:

Enter an alphabet: a 
Vowel
Please follow and like us:
Posted in CPP   


Copyright © 2019 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki