160 likes | 372 Views
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
E N D
Selection Robert Stumpf, Professor Computer Information Systems California State Polytechnic University Pomona selection
Selection • Bohm and Jacopini • Selection • Precedence selection
Bohm and Jacopini • Bohm and Jacopini stated that a Structured program contains three constructs: 1. Sequence 2. Selection 3. Iteration selection
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
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
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
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
Selection • Basic Selection (without a block):if ( a < b ) big = b; else big = a; selection
Selection • Selection using a block:if ( a < b ) { big = b; } else { big = a; } selection
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
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
Precedence The precedence chart that previously contained *, /, %, +, and – needs to be expanded • – minus • * multiplication/ division% modulus (remainder) • + addition - subtraction • = assignment selection
Precedence The precedence chart now is eight levels: • – minus! not • * multiplication/ division% modulus (remainder) • + addition - subtraction selection
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
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
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