Lecture 9- Select Structure (1)
Lecture 9- Select Structure (1)
|
Introduction
04/27/202
5
Lecture 9: Switch Select Statement |
Switch Statement-Flowchart
04/27/202
5
Lecture 9: Switch Select Statement |
Switch selective structure
syntax
switch(variable)
{
case constant 1:
statements;
break;
case constant2:
statements
break;
…
default:
default group of statements
break
}
04/27/202
5
Lecture 9: Switch Select Statement |
Example
04/27/202
5
Lecture 9: Switch Select Statement |
#include<iostream>
using namespace std;
int main()
{
int num1,num2,sum, subtract, product;
float quotient;
char choice;
cout<<"Input the first number\n";
cin>>num1;
cout<<"Input the second number\n";
cin>>num2
cout<<"Which arithmetic operation would you like to perform
to the two numbers?\n";
cout<<"+ Addition\n";
cout<<"- Subtraction\n";
cout<<"/ Division\n";
cout<<"* Multiplication\n";
cin>>choice;
04/27/202
5
Lecture 9: Switch Select Statement |
switch(choice)
{
case '+':
sum=num1+num2;
cout<<"Sum is: "<<sum<<endl;
break;
case '-':
subtract=num1-num2;
cout<<"subtraction is: "<<subtract<<endl;
break;
case '*':
product=num1*num2;
cout<<"Product is: "<<product<<endl;
break;
case '/':
quotient=num1/num2;
cout<<"Quotient is: "<<quotient<<endl;
break;
default:
cout<<"Invalid choice selected";
break;
} //end of switch statement
system("pause");
return 0;
}
04/27/202
5
Lecture 9: Switch Select Statement |
How switch works
04/27/202
5
Lecture 9: Switch Select Statement |
Switch statement
04/27/202
5
Lecture 9: Switch Select Statement |
Class Exercise
04/27/202
5
Lecture 9: Switch Select Statement |