1 / 16

Selection

Selection. Robert Stumpf, Professor Computer Information Systems California State Polytechnic University Pomona. Selection. Bohm and Jacopini Selection Precedence. Bohm and Jacopini. Bohm and Jacopini stated that a Structured program contains three constructs: 1. Sequence

aram
Download Presentation

Selection

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. Selection Robert Stumpf, Professor Computer Information Systems California State Polytechnic University Pomona selection

  2. Selection • Bohm and Jacopini • Selection • Precedence selection

  3. Bohm and Jacopini • Bohm and Jacopini stated that a Structured program contains three constructs: 1. Sequence 2. Selection 3. Iteration selection

  4. Bohm and Jacopini • Java is a block structured language • This means all instructions inside a block are executed in sequence • A block is made up of braces { } • All methods contain at least one block selection

  5. Bohm and Jacopini • Sequence: • Semi colons need special attention • Every statement ends with a semi colon • Blocks never end with a semi colon • This means you will see semi colons before and not after braces: { a = b ; } selection

  6. Bohm and Jacopini • Selection: • Enables one to select one of several alternate paths of code • If there is one decision, there are two paths • If there are two decisions, there are three paths • If there are n decisions, there are n + 1 paths selection

  7. Bohm and Jacopini • Iteration: • Enables one to repeat a block of code • There are several forms: • Some forms have to test to see if process is done at the beginning of the code • Other forms have to test to see if process is done at the end of the code selection

  8. Selection • Basic Selection (without a block):if ( a < b ) big = b; else big = a; selection

  9. Selection • Selection using a block:if ( a < b ) { big = b; } else { big = a; } selection

  10. Selection • Selection using a Nested if:if (amount > 500.0) discount = amount * 0.05; else if (amount > 200.0) discount = amount * 0.02; else discount = amount * 0.00; selection

  11. Selection Selection using logical operators (and or):if ((gender == 'M') && (age < 25)|| (gender == 'F') && (numberCitations > 2)) rate = 0.04; else if ((gender == 'M') || (gender == 'F') && (age < 25)) rate = 0.3; else rate = 0.2; selection

  12. Precedence The precedence chart that previously contained *, /, %, +, and – needs to be expanded • – minus • * multiplication/ division% modulus (remainder) • + addition - subtraction • = assignment selection

  13. Precedence The precedence chart now is eight levels: • – minus! not • * multiplication/ division% modulus (remainder) • + addition - subtraction selection

  14. Precedence The precedence chart now is eight levels: • > relational greater than > = relational greater than or equal< relational less than < = relational less than or equal • = = relational equality ! = relational inequality • && logical and • || logical or • = assignment selection

  15. Summary • Java has traditional selection and iteration statements • Java uses blocks that can be treated as a single statement • It is important to remember the precedence of the operators selection

  16. Thank You • Java has Built in Constructs to facilitate Selection • Any questions should be directed to Professor Robert Stumpf • Email: rvstumpf@csupomona.edu • Web Site:http://www.csupomona.edu/~rvstumpf selection

More Related