Title: The switch Statement
1The switch Statement
Selection Revisited
2Problem
- Using OCD, design and implement a program that
allows the user to perform an arbitrary
temperature conversion (Celsius-to-Kelvin,
Kelvin-to-Celsius, Fahrenheit-to-Celsius,
Celsius-to-Fahrenheit, Fahrenheit-to-Kelvin, or
Kelvin-to-Fahrenheit).
3Preliminary Analysis
- One way to solve the problem is to provide a menu
of choices, from which the user selects the
operation they want to perform
Please enter a - to convert Fahrenheit to
Celsius b - to convert Celsius to Fahrenheit
c - to convert Fahrenheit to Kelvin d - to
convert Kelvin to Fahrenheit e - to convert
Celsius to Kelvin f - to convert Kelvin to
Celsius q - to quit --gt
4Behavior main()
- Our program should display its purpose and a menu
of choices. It should then read the users
choice. It should then prompt for and read the
temperature to be converted. It should then
compute the converted temperature by applying to
the input temperature the particular conversion
specified by the users choice. It should then
display the converted temperature.
5Objects main()
- Description Type Kind Name
purpose string constant --
menu string constant MENU
choice char variable choice
input temp. double variable tempIn
output temp. double variable tempOut
6Operations main()
- Description Predefined? Library?
Name
display strings yes iostream ltlt
read a double yes iostream gtgt
convert a temp. no -- --
display double yes iostream ltlt
7Algorithm main()
- 0. Display via cout the purpose of the program.
- 1. Loop
- a. Display MENU via cout.
- b. Read choice from cin.
- c. If choice is q, terminate repetition.
- d. Prompt for and read tempIn from cin.
- e. Compute tempOut by converting tempIn using
choice. - f. Display tempOut.
- End loop.
8Organization
- Well need
- a main function
- six conversion functions
- a function to apply the right conversion
- The conversion functions seem likely to be
reuseable someday, so well create a library
named Heat in which to store them.
9Behavior FahrenheitToCelsius()
- Our function should receive from its caller the
temperature to be converted. It should return
the result of subtracting 32.0 from that
temperature, and then dividing the difference by
1.8.
10Objects FahrenheitToCelsius()
- Description Type Kind Name
the original double variable originalTemp
temperature
the converted double variable --
temperature
We can use this as a pattern for each of the
temperature conversion functions -- they differ
only in the particular formula they use.
11Coding Conversion Prototypes
- / Heat.h
- ...
- /
- double FahrenheitToCelsius(double originalTemp)
- double CelsiusToFahrenheit(double originalTemp)
- double FahrenheitToKelvin(double originalTemp)
- double KelvinToFahrenheit(double originalTemp)
- double CelsiusToKelvin(double originalTemp)
- double KelvinToCelsius(double originalTemp)
12Operations FahrenheitToCelsius()
- Description Predefined? Library?
Name
receive a double yes built-in
subtract doubles yes built-in -
divide doubles yes built-in /
return a double yes built-in return
13Algorithm FahrenheitToCelsius()
- 0. Receive originalTemp.
- 1. Return (originalTemp - 32.0) / 1.8.
We can use this algorithm as a pattern for each
of the temperature conversion functions -- each
differs only in the particular formula used.
14Coding Conversion Definitions
- / Heat.cpp
- ...
- /
- double FahrenheitToCelsius(double originalTemp)
-
- return (originalTemp - 32.0) / 1.8
-
- double CelsiusToFahrenheit(double originalTemp)
-
- return originalTemp 1.8 32.0
-
- double FahrenheitToKelvin(double originalTemp)
-
- return (originalTemp - 32.0) / 1.8 273.0
-
15Organization (ii)
- Once our library is completed, we can design and
implement a function to perform the particular
conversion specified by the users menu choice. - We will call this function Convert().
- Since it is less likely to be reuseable than our
conversion functions, we will store its prototype
and definition in the same file as our main
function.
16Behavior Convert()
- Our function should receive from its caller the
temperature to be converted and the users menu
choice. Based on the menu choice, our function
should return the result of applying the
appropriate temperature conversion function to
the received temperature.
17Objects Convert()
- Description Type Kind Name
the temp. double variable origTemp being
converted
a menu choice char variable choice
the converted double variable --
temperature
Our prototype is thus double Convert(double
origTemp, char choice)
18Operations Convert()
- Description Defined? Library? Name
return a double yes built-in return
convert F-to-C yes Heat ...
convert C-to-F yes Heat ...
convert F-to-K yes Heat ...
convert K-to-F yes Heat ...
convert C-to-K yes Heat ...
convert K-to-C yes Heat ...
perform selection yes built-in switch based
on a char
19Algorithm Convert()
- 0. Receive origTemp, choice.
- 1. If choice is a return FahrenheitToCelsius(or
igTemp) - Else if choice is b return
CelsiusToFahrenheit(origTemp) - Else if choice is c return
FahrenheitToKelvin(origTemp) - Else if choice is d return
KelvinToFahrenheit(origTemp) - Else if choice is e return
CelsiusToKelvin(origTemp) - Else if choice is f return
KelvinToCelsius(origTemp) - Else display error message and return 0.0.
We could implement this algorithm with a
multi-branch if, but lets learn something new.
20Coding Convert()
- / main.cpp
- ...
- /
- // rest of main.cpp
- double Convert(double origTemp, char choice)
-
- switch (choice)
-
- case a return FahrenheitToCelsius(origTemp
) - case b return CelsiusToFahrenheit(origTemp
) - case c return FahrenheitToKelvin(origTemp)
- case d return KelvinToFahrenheit(origTemp)
- case e return CelsiusToKelvin(origTemp)
- case f return KelvinToCelsius(origTemp)
- default cerr ltlt \nUnsupported menu
choice - ltlt choice ltlt in Convert()
ltlt endl - return 0.0
21Discussion
The switch statement provides an alternative
means of doing multi-branch selection.
- double Convert(double origTemp, char choice)
-
- switch (choice)
-
- case a return FahrenheitToCelsius(origTemp
) - case b return CelsiusToFahrenheit(origTemp
) - case c return FahrenheitToKelvin(origTemp)
- case d return KelvinToFahrenheit(origTemp)
- case e return CelsiusToKelvin(origTemp)
- case f return KelvinToCelsius(origTemp)
- default cerr ltlt \nUnsupported menu
choice - ltlt choice ltlt in Convert()
ltlt endl - return 0.0
-
22Organization (iii)
- Once our library and conversion function have
been designed and completed, we can proceed to
implement the algorithm for our main function. - The primary control structure is a forever loop
that continues repetition until the user enters
menu choice q...
23Coding main()
- include ltiostreamgt // cin, cout, ltlt,
gtgt, ... - using namespace std
- include Heat.h //
FahrenheitToCelsius(), ... - double Convert(double originalTemp, char
menuChoice) - int main()
-
- const MENU \nPlease enter\n
- a - to convert Fahrenheit to
Celsius\n - b - to convert Celsius to
Fahrenheit\n - c - to convert Fahrenheit to
Kelvin\n - d - to convert Kelvin to
Fahrenheit\n - e - to convert Celsius to
Kelvin\n - f - to convert Kelvin to
Celsius\n - q - to quit\n
- --gt
24Coding main()
- char choice
- double tempIn, tempOut
- for ()
-
- cout ltlt MENU
- cin gtgt choice
- if (choice q choice Q) break
- cout ltlt \nEnter the temperature to be
converted - cin gtgt tempIn
- tempOut Convert(tempIn, choice)
- cout ltlt \nThe converted temperature is
- ltlt tempOut ltlt endl
-
25Testing
This program converts temperatures. Please
enter a - to convert Fahrenheit to Celsius
b - to convert Celsius to Fahrenheit c - to
convert Fahrenheit to Kelvin d - to convert
Kelvin to Fahrenheit e - to convert Celsius to
Kelvin f - to convert Kelvin to Celsius q -
to quit\n --gt a Enter the temperature to be
converted 212 The converted temperature is 100
26Testing
Please enter a - to convert Fahrenheit to
Celsius b - to convert Celsius to Fahrenheit
c - to convert Fahrenheit to Kelvin d - to
convert Kelvin to Fahrenheit e - to convert
Celsius to Kelvin f - to convert Kelvin to
Celsius q - to quit\n --gt b Enter the
temperature to be converted 0 The converted
temperature is 32
27Testing
Please enter a - to convert Fahrenheit to
Celsius b - to convert Celsius to Fahrenheit
c - to convert Fahrenheit to Kelvin d - to
convert Kelvin to Fahrenheit e - to convert
Celsius to Kelvin f - to convert Kelvin to
Celsius q - to quit\n --gt c Enter the
temperature to be converted 32 The converted
temperature is 273 ...
28Summary
- To perform multibranch selection, C provides an
alternative statement called the switch
statement. - Well learn more of the details of the if and
switch statements next time, including when you
should use which statement.