0% found this document useful (0 votes)
5 views

Lecture 9- Select Structure (1)

The document discusses the switch selective structure in programming, primarily used for checking multiple constants for an expression, often in menu scenarios. It includes syntax examples, a sample program for performing arithmetic operations based on user input, and explains how the switch statement evaluates conditions. Additionally, it highlights the limitations of switch statements, such as only being able to compare expressions against constants.

Uploaded by

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

Lecture 9- Select Structure (1)

The document discusses the switch selective structure in programming, primarily used for checking multiple constants for an expression, often in menu scenarios. It includes syntax examples, a sample program for performing arithmetic operations based on user input, and explains how the switch statement evaluates conditions. Additionally, it highlights the limitations of switch statements, such as only being able to compare expressions against constants.

Uploaded by

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

Lecture 9: Select Structure

|
Introduction

• The objective of the switch selective


structure is to check several possible
constants for an expression.
• Mostly used for menus.

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

• Using the select structure write a program


that allows a user to input two integers
and then select the arithmetic operation
they would like to perform on the integers;
+ for addition
- for substraction
/ for division
* for multiplication

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

• It works in the following way: switch


evaluates variable and checks if it is
equivalent to constant1.
• if it is, it executes group of statements 1
until it finds the break statement. When it
finds this break statement the program
jumps to the end of the switch selective
structure.
• If it is not, it checks if it is equivalent to
constant2… etc.

04/27/202
5
Lecture 9: Switch Select Statement |
Switch statement

• Switch can only be used to compare an


expression against constants.
• Therefore we cannot put variables as
labels
– E.g. case n:
• or ranges
– E.g. case 1..3
• because they are not valid C++ constants

04/27/202
5
Lecture 9: Switch Select Statement |
Class Exercise

• Use a switch statement to rewrite the


following code.

04/27/202
5
Lecture 9: Switch Select Statement |

You might also like