Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fundamental Programming 310201 Fundamental Programming More on Selection.

Similar presentations


Presentation on theme: "Fundamental Programming 310201 Fundamental Programming More on Selection."— Presentation transcript:

1 Fundamental Programming 310201 Fundamental Programming More on Selection

2 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 )

3 Fundamental Programming 310201 If-then-else – a review Let’s start out by reviewing what we know about the if-then-else statement…

4 Fundamental Programming 310201 if statement if (Age < 18) { cout << “A child!“ << endl; }

5 Fundamental Programming 310201 if-else statement if (Age < 18) { cout << “A child!“ << endl; } else { cout << “An adult!“ << endl; }

6 Fundamental Programming 310201 if-else-if statement if (Age < 13) { cout << “A child!“ << endl; } else if ((Age >= 13 ) && (Age <= 17)) { cout << “A teenager!“ << endl; } else { cout << “An adult!“ << endl; }

7 Fundamental Programming 310201 Braces if (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.

8 Fundamental Programming 310201 Nested if/else if (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...

9 Fundamental Programming 310201 Nested if/else vs if-then-else-if Gender = ‘M’ else Age < 18 else Age < 18 male child manfemale child woman Age < 13 Age >= 13 && Age <= 17 Age > 17 child teenager adult

10 Fundamental Programming 310201 Activity Can 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?

11 Fundamental Programming 310201 Activity Break

12 Fundamental Programming 310201 Activity Feedback if ((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;

13 Fundamental Programming 310201 Activity Feedback Nested 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

14 Fundamental Programming 310201 Activity Feedback if (Gender == ‘M’) { NbrMales = NbrMales + 1; if (Age < 18) cout << “A male child!“ << endl; else cout << “A man!“ << endl; } else { : }

15 Fundamental Programming 310201 Handling other cases One 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?

16 Fundamental Programming 310201 Nested if/else if (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;

17 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.

18 Fundamental Programming 310201 Other selection statements In 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...

19 Fundamental Programming 310201 Other selection statements Data 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...

20 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

21 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

22 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

23 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

24 Fundamental Programming 310201 Activity For 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;

25 Fundamental Programming 310201 Activity Break

26 Fundamental Programming 310201 Activity Feedback effect 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;

27 Fundamental Programming 310201 The switch statement switch 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 ( ) {...}

28 Fundamental Programming 310201 The switch statement or, more fully… switch ( ) { case : break; : : default: } notes: must be a variable of type integer or char

29 Fundamental Programming 310201 The switch statement or, more fully… switch ( ) { case : break; : : default: } notes: integer or char literal or constant – eg: 1, 'A', EXIT

30 Fundamental Programming 310201 The switch statement or, more fully… switch ( ) { case : break; : : default: } notes: don’t forget the colon

31 Fundamental Programming 310201 The switch statement or, more fully… switch ( ) { case : break; : : default: } notes: break; is optional

32 Fundamental Programming 310201 Optional break; ? switch ( CharMenuSelection ) { case 'a': case 'A': ProcessSelectionA; break; case 'b': case 'B': ProcessSelectionB; break; : }

33 Fundamental Programming 310201 Ternary operator C++ 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...

34 Fundamental Programming 310201 Ternary operator The following example outputs “Pass” or “Fail”, depending on value of Mark : cout = 50 ) ? "Pass" : "Fail "); syntax: (( ) ? : ) same as: if ( Mark >= 50 ) cout << "Pass"; else cout << "Fail"; if condition is True, expression 1 is evaluated; otherwise, expression 2 is evaluated

35 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

36 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


Download ppt "Fundamental Programming 310201 Fundamental Programming More on Selection."

Similar presentations


Ads by Google