Fundamental Programming 310201 Fundamental Programming More on Selection.
-
Upload
silas-burns -
Category
Documents
-
view
228 -
download
1
Transcript of Fundamental Programming 310201 Fundamental Programming More on Selection.
Fundamental Programming 310201
Selection statements
We have been using one C++ selection statement – if-then-else. In this class we explore other selection statements – statements that allow us to perform different tasks, depending on the input data.
– switch – a specialised version of ‘if ‘– ternary operators (three parts )
Fundamental Programming 310201
If-then-else – a review
Let’s start out by reviewing what we know about the if-then-else statement…
Fundamental Programming 310201
if-else statementif (Age < 18){
cout << “A child!“ << endl;}else {
cout << “An adult!“ << endl;}
Fundamental Programming 310201
if-else-if statementif (Age < 13){
cout << “A child!“ << endl;}else if ((Age >= 13 ) && (Age <= 17)){
cout << “A teenager!“ << endl;}else{
cout << “An adult!“ << endl;}
Fundamental Programming 310201
Bracesif (Age < 13)
cout << “A child!“ << endl;else if ((Age >= 13 ) && (Age <= 17))
cout << “A teenager!“ << endl;else
cout << “An adult!“ << endl;
Not required if branch has only one statement.We recommend you always use braces in this course. Sometimes we omit them to fit our examples on a slide.
Fundamental Programming 310201
Nested if/elseif (Gender == ‘M’) if (Age < 18) cout << “A male child!“ << endl; else cout << “A man!“ << endl;else if (Age < 18) cout << “A female child!“ << endl; else cout << “A woman!“ << endl;
Nested if/else is different from if-else-if...
Fundamental Programming 310201
Nested if/else vs if-then-else-if
Gender = ‘M’
else
Age< 18
else elseAge< 18
malechild
man femalechild
woman
Age< 13 Age >= 13
&&Age <= 17
Age> 17
child
teenager
adult
Fundamental Programming 310201
ActivityCan our nested if/else example be re-code as an if-then-else-if?
If so, do so!
If so, can a nested if/else always be re-coded as an if-then-else-if?
If so, why not always use if-then-else-if instead of a nested if/else?
Fundamental Programming 310201
Activity Feedbackif ((Gender == ‘M’) && (Age < 18)) cout << “A male child!“ << endl;else if (Gender == ‘M’) cout << “A man!“ << endl;else if (Age < 18) cout << “A female child!“ << endl;else cout << “A woman!“ << endl;
Fundamental Programming 310201
Activity FeedbackNested if/else can always be re-coded as an if-then-else-if.
We do not always use if-then-else-if instead of a nested if/else because:
• conditions become more complicated
• we may wish to perform some common processing for all cases in a sub-branch
Fundamental Programming 310201
Activity Feedbackif (Gender == ‘M’) { NbrMales = NbrMales + 1; if (Age < 18) cout << “A male child!“ << endl; else cout << “A man!“ << endl; }else { : }
Fundamental Programming 310201
Handling other casesOne must always be aware of the need to handle other cases..
What are the cases that we are expected to be able to handle?
Do we need to consider the possibility of encountering other cases?
If so, how should we handle them?
Fundamental Programming 310201
Nested if/elseif (Gender == ‘M’) if (Age < 18) cout << “A male child!“ << endl; else cout << “A man!“ << endl;else if (Gender == ‘F’) if (Age < 18) cout << “A female child!“ << endl; else cout << “A woman!“ << endl;else cout << “Unknown gender!“ << endl;
Fundamental Programming 310201
Other selection statements
Selection statements allow us to perform different tasks in our programs, depending on the input data.
The if/else statement is the only selection statement we need. However, most programming languages provide other selection statement for convenience.
Fundamental Programming 310201
Other selection statementsIn C++, the switch statement is an alternative to the if-then-else-if statement
if-then-else-if is more powerful than switch – there are tasks you can handle with if-then-else-if that you can’t handle with switch
For example, a menu-driven program might start like this...
Fundamental Programming 310201
Other selection statementsData Processing Application===========================Select from the menu below: 1 – Load input data from disk 2 – Enter input data 3 – Save input data to disk 4 – Process input data 5 – Display output data 6 – Clear input data 0 – Exit Enter your selection ==> 2:
This main function of this program may include the following if-then-else-if statement...
Fundamental Programming 310201
int MenuSelection = 1;while (MenuSelection != 0){ DisplayMenuAndObtainSelection( MenuSelection ); if ( MenuSelection == 1 ) LoadInputDataFromDisk( InputData ); else if ( MenuSelection == 1 )
EnterInputData( InputData ); else if ( MenuSelection == 3 )
SaveInputDataToDisk( InputData ); else if ( MenuSelection == 4 )
ProcessInputData( InputData, OutputData ); else if ( MenuSelection == 5 )
DisplayOutputData( OutputData ); else if ( MenuSelection == 6 )
ClearInputData( InputData ); else if ( MenuSelection != 0 )
cout << ”Invalid menu selection!";}
Or, we can use a switch statement...
functions
Fundamental Programming 310201
int MenuSelection = 1;while (MenuSelection != 0){ DisplayMenuAndObtainSelection( MenuSelection ); switch ( MenuSelection ) { case 1: LoadInputDataFromDisk( InputData ); break;
case 2: EnterInputData( InputData ); break;
case 3: SaveInputDataToDisk( InputData ); break;
case 4: ProcessInputData( InputData, OutputData ); break;
case 5: DisplayOutputData( OutputData ); break;
case 6: ClearInputData( InputData ); break;
case 0: break; default: cout << ”Unknown menu selection!"; }}
notes: a little easier to read than if-then-else-if
Fundamental Programming 310201
int MenuSelection = 1;while (MenuSelection != 0){ DisplayMenuAndObtainSelection( MenuSelection ); switch ( MenuSelection ) { case 1: LoadInputDataFromDisk( InputData ); break;
case 2: EnterInputData( InputData ); break;
case 3: SaveInputDataToDisk( InputData ); break;
case 4: ProcessInputData( InputData, OutputData ); break;
case 5: DisplayOutputData( OutputData ); break;
case 6: ClearInputData( InputData ); break;
case 0: break; default: cout << ”Unknown menu selection!"; }}
notes: on break jump to end of switch statement
Fundamental Programming 310201
int MenuSelection = 1;while (MenuSelection != 0){ DisplayMenuAndObtainSelection( MenuSelection ); switch ( MenuSelection ) { case 1: LoadInputDataFromDisk( InputData ); break;
case 2: EnterInputData( InputData ); break;
case 3: SaveInputDataToDisk( InputData ); break;
case 4: ProcessInputData( InputData, OutputData ); break;
case 5: DisplayOutputData( OutputData ); break;
case 6: ClearInputData( InputData ); break;
case 0: break; default: cout << ”Unknown menu selection!"; }}
notes: default handles cases not handled above
Fundamental Programming 310201
ActivityFor previous slide:
what is effect of removing the following line? case 0: break;
what is effect of removing second line below?
case 4: ProcessInputData( InputData,OutputData ); break;
Fundamental Programming 310201
Activity Feedbackeffect of removing the following line is that the program will say “Unknown menu selection” before exiting case 0: break;
effect of removing second line below, is that the program will process data AND display output data when user selects 4
case 4: ProcessInputData( InputData,OutputData ); break;
Fundamental Programming 310201
The switch statementswitch is a convenient replacement for simple if-then-else-if statements
however, switch can only be used when the selection depends on the value of a variable of type integer or char (characters are stored as an integer, using ASCII coding system)
switch ( <integer or char variable> ) {...}
Fundamental Programming 310201
The switch statementor, more fully…
switch ( <integer or char variable> ) { case < constant or literal integer or char >: < statements to handle this case> break; : < other cases > : default: < statements to handle cases not handled above> }
notes: must be a variable of type integer or char
Fundamental Programming 310201
The switch statementor, more fully…
switch ( <integer or char variable> ) { case < constant or literal integer or char >: < statements to handle this case> break; : < other cases > : default: < statements to handle cases not handled above> }
notes: integer or char literal or constant – eg: 1, 'A', EXIT
Fundamental Programming 310201
The switch statementor, more fully…
switch ( <integer or char variable> ) { case < constant or literal integer or char >: < statements to handle this case> break; : < other cases > : default: < statements to handle cases not handled above> }
notes: don’t forget the colon
Fundamental Programming 310201
The switch statementor, more fully…
switch ( <integer or char variable> ) { case < constant or literal integer or char >: < statements to handle this case> break; : < other cases > : default: < statements to handle cases not handled above> }
notes: break; is optional
Fundamental Programming 310201
Optional break;?switch ( CharMenuSelection ){ case 'a': case 'A': ProcessSelectionA; break; case 'b': case 'B': ProcessSelectionB; break; :}
Fundamental Programming 310201
Ternary operatorC++ also has a selection operator – an operator that selects between one of two expression, depending on the input data
operators are applied to expressions to produce values of interest:
(FahrenheitTemp - 32) / 1.8
like switch, the ternary operator is simply a convenience – the role it plays can be performed by if/else...
Fundamental Programming 310201
Ternary operator
The following example outputs “Pass” or “Fail”, depending on value of Mark : cout << (( Mark >= 50 ) ? "Pass" : "Fail ");
syntax: (( <condition> ) ? <expression 1> : <expression 2>)
same as: if ( Mark >= 50 ) cout << "Pass"; else cout << "Fail";
if condition is True, expression 1 is evaluated; otherwise, expression 2 is evaluated
Fundamental Programming 310201
Ternary operator
As you know, the power of expression comes from the ability to nest simple expressions inside more complex expressions
The ternary operator provides some useful additional flexibility to the power of expressions
Fundamental Programming 310201
Summary
Nested if/else statements provide a convenient alternative to if-then-else-if statements
The switch statements provide a convenient alternative to simple if-then-else-if statements
The ternary operator provides some useful additional flexibility to the power of expressions