1 / 36

Fundamental Programming

Fundamental Programming. More on Selection. 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.

carik
Download Presentation

Fundamental Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Fundamental Programming More on Selection

  2. 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. If-then-else – a review Let’s start out by reviewing what we know about the if-then-else statement…

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

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

  6. 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. 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. Nested if/else if (Gender==‘M’) if (Age < 18) cout<<“Amale child!“<<endl; else cout<<“A man!“<<endl; else if (Age < 18) cout<<“Afemale child!“<<endl; else cout<<“A woman!“<<endl; Nested if/else is different from if-else-if...

  9. Nested if/else vs if-then-else-if else Gender = ‘M’ Age < 13 Age > 17 Age >= 13 && Age <= 17 child adult else else Age < 18 Age < 18 teenager male child man female child woman

  10. 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. Activity Break

  12. Activity Feedback if ((Gender==‘M’) && (Age < 18)) cout<<“Amale child!“<<endl; else if (Gender==‘M’) cout<<“A man!“<<endl; else if (Age < 18) cout<<“Afemale child!“<<endl; else cout<<“A woman!“<<endl;

  13. 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. Activity Feedback if (Gender==‘M’) { NbrMales = NbrMales + 1; if (Age < 18) cout<<“Amale child!“<<endl; else cout<<“A man!“<<endl; } else { : }

  15. 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. Nested if/else if (Gender==‘M’) if (Age < 18) cout<<“Amale child!“<<endl; else cout<<“A man!“<<endl; else if (Gender==‘F’) if (Age < 18) cout<<“Afemale child!“<<endl; else cout<<“A woman!“<<endl; else cout<<“Unknown gender!“<<endl;

  17. 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. 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. 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. 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!"; } functions Or, we can use a switch statement...

  21. notes: a little easier to read than if-then-else-if 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!"; } }

  22. notes: on break jump to end of switch statement 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!"; } }

  23. notes: default handles cases not handled above 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!"; } }

  24. 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. Activity Break

  26. 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. 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 ( <integer or char variable> ) {...}

  28. The switch statement or, 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

  29. The switch statement or, 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

  30. The switch statement or, 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

  31. The switch statement or, 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

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

  33. 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. 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"; ifcondition is True, expression 1 is evaluated; otherwise, expression 2 is evaluated

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

More Related