0% found this document useful (0 votes)
5 views5 pages

Switch

Uploaded by

lysarith09
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views5 pages

Switch

Uploaded by

lysarith09
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Switch:

- ជំរ សមួ
ើ យក្នុងចំរោមជំរ សជារ្ច
ើ ន
ើ ។
- ដំរ ើ កា រក្ើតរ ង
ើ រៅតាមកា រសមើគ្នន វាង Logic-Expression ជាមួយ Value នើមួយៗ។
Syntax:
switch (Logic-Expression)
{
case value1:
Statement1;
break;
case value2:
Statement2;
break;
....................
....................
case valuen-1:
Statementn-1;
break;
default:
Statement n(Default);
break;
}
Example 1: បរងកត
ើ ក្មមវ ិធើមួយតាមរាងខាងរ្កាម៖
បញ្ចូ ល លទ្ធផល
0 ចាក្រចញ
1 ជួបជាមួយ Front office
2 ជួបជាមួយ Administrator
3 ជួបជាមួយ Academic
4 ជួបជាមួយ IT
5 ជួបជាមួយ Lecturer
C++ Code:
#include<iostream>
using namespace std;

int main(){
//Switch
//Varaible Declaration
int number;
string message;
//Read from Outside
cout << "------------------------" << endl;
cout << "0. Quit" << endl;
cout << "1. Front Office" << endl;
cout << "2. Administrator" << endl;
cout << "3. Academic" << endl;
cout << "4. IT" << endl;
cout << "5. Lecturer" << endl;
cout << "------------------------" << endl;
cout << "Input Number: ";
cin >> number;
//Switch
switch (number)
{
case 0:
message = "Good Bye!!!";
break;
case 1:
message = "Welcome to my office";
break;
case 2:
message = "Welcome to Administrator";
break;
case 3:
message = "Request for my score";
break;
case 4:
message = "Asking for my computer";
break;
case 5:
message = "Ask permission";
break;
default:
message = "Please, input between 0-5";
break;
}
cout << "------------------------" << endl;
cout << "Message: " << message << endl;
cout << "------------------------" << endl;
return 0;
}
Output:
Example 2: បរងកើតក្មមវ ិធើមួយតាមរាងខាងរ្កាម៖
បញ្ចូ ល៖
- រលខទ្ើ១
- រលខទ្២

បញ្ចូ ល លទ្ធផល
1 គ នាផលបូក្
2 គ នាផលដក្
3 គ នាផលគុ
4 គ នាផលផលចចក្
5 គ នាសំ ល់
C++ Code:
#include<iostream>
using namespace std;

int main(){
//Switch
//Varaible Declaration
int number_operator, number1, number2;
//Read from Outside
cout << "Read Number1:";
cin >> number1;
cout << "Read Number2:";
cin >> number2;
cout << "Input Operators" << endl;
cout << "1. Addition" << endl;
cout << "2. Substraction" << endl;
cout << "3. Multiplication" << endl;
cout << "4. Division" << endl;
cout << "5. Modulus" << endl;
cout << "----------------" << endl;
cout << "Input Number of Operator[1-5]: ";
cin >> number_operator;
//Switch
switch (number_operator)
{
case 1:
cout << "Addition: " << (number1 + number2) << endl;
break;
case 2:
cout << "Substraction: " << (number1 - number2) << endl;
break;
case 3:
cout << "Multiplication: " << (number1 * number2) << endl;
break;
case 4:
cout << "Division: " << (number1 / number2) << endl;
break;
case 5:
cout << "Modulus: " << (number1 % number2) << endl;
break;
default:
cout << "Please, input between 1-5";
break;
}
return 0;
}
Output:

You might also like