CSC141 Computer Science I Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA 19383 zjiang@wcupa.edu
Selection (Decision) Introduction T/F selection MC selection
Selection (Decision) Rolling a dice. Sample execution (click on this link to try http://www.cis.temple.edu/~jiang/dice.exe Each button in the above sample has
No Yes Win/Lost? Double the money Bankrupt Restart
If else statement if (test) action 1 (statements 1) else action 2 (statements 2) //end if action 3 (statement 3)
No Yes Test by boolean expression Action 1 Action 2 Action 3
Boolean expression in test Simple expression Format <Value> <relational operators> <Value> Number value relational operators, Table 3-1, page 113 ==, !=, <, >, <=, >= !!! Number one error: “(a=2)” instead of “(a==2)” char value (not string!) relational operators ==, != (page 121)
Complex expression &&, ||, ! (Table 3-4, page 137) Truth table Precedence order, table 3-10, page 144 Number range, page 144-145
Ex 6: http://www.cs.wcupa.edu/~zjiang/141_ex6.pdf 11/27/2018
Development of a correct decision program Grade.java
Development Process Identify two exclusive options Implement each handling in different action parts Identify the situation (values) for option selection Make an expression so that all the situation value for option part 1 will lead to the test result (boolean value) true. Verify all the situation value for option part 2 will lead to the test false, otherwise, revise the above expression!
60, 61, 62, …, 100 ? 1, 2, …, 58, 59
Ex 7: http://www.cs.wcupa.edu/~zjiang/141_ex7.pdf 11/27/2018
If statement, Code 3-1, page 115 Relational operator ?, page 152 Multiple selection Nested if, Code 3-4, page 129 If else if, Code 3-5, page 133 Example: letter grade
Comments: Nested if for multiple selection problem If case 1 Else if … //end of case 2 if //End of case 1 if
Development of a correct multiple-decision program Letter grade
Ex 9 http://www.cs.wcupa.edu/~zjiang/141_ex9.pdf
Switch Switch([variable]) Case [constant value]: Break (or no break), Code 3-13, 3-14, page 157-159
Project 2 11 case If-else BMI expression simplification Discount Last else no if Nested if and multiple if Avg < 90 && test1 < 90 to attend the final Discount %, ()?