CUW CSC 250 - The Selection Control Structure 1

Unformatted text preview:

The Selection Control Structure 1.1. Module charts.Module charts (continued):2. Flow-charts.3. Selection.Selection.Selection (cont.).Slide 8Slide 9Slide 10Slide 11Slide 12Slide 13Slide 14Slide 154. Conditions.Conditions (cont.).Slide 18Slide 19Slide 20Slide 21Slide 22Slide 23Slide 24Slide 25Slide 26Slide 27Slide 28Slide 29Slide 30Slide 31The Selection Control Structure 1.1. Module charts.2. Flow-charts. 3. Selection.4. Conditions.1. Module charts.Print amortization tableInitialize Process FinalizeModule charts (continued):Print amortization tableInitialize Process FinalizeGet InputInitialize VariablesCalculate Table LinePrintTable LineCalculateLast LinePrintLast LineUntilPayment >BeginningBalance2. Flow-charts.Problem: Using a loop, process a series of school soccer players. For each player, input the Age and Gender (the Gender could be represented by a character, either 'M' or 'F').The loop should terminate when an age of -1 (a sentinel value) is input. Inside the loop, for each player, add 1 to the appropriate total in the following list:(1) MaleYouth (2) Female Youth(3) Male Adult (4) Female Adult.(Note: A Youth is anyone < 18.)When the loop is exited, display the four totals.3. Selection.Recall that the default control-structure is called the sequential or straight-line control structure.What code gets executed?What is its limitation? What can it not handle?Often, we need to give the user choices.How do we do this?Selection.A. What?Using selection, we can ask T/F questions about our data, and process it accordingly. For example, if Age is input, we can ask if Age >=18. If it is, we can do “adult” processing; otherwise we can do “youth” processing.Age >= 18?YNSelection (cont.).B. Why?In this way, we can process data differentially:Different data values can lead to different processing. E.g. an Age of 21 leads to adult processing, while an age of 15 yields to youth processing.Selection (cont.).C. How?In every procedural language, selection is implemented by some sort of conditional or “if” statement.What is meant by saying something is “conditional”? How is this different from its opposite, “unconditional.” E.g. conditional vs. unconditional love (liberalitas vs. agape/caritas).Selection (cont.).How (cont.).What is meant by a conditional statement?If antecedent condition then dependent consequent, e.g.If it rains, then the farmers will be happy.“it rains” is the antecedent condition, “the farmers will be happy” is the dependent consequent.Selection (cont.).How (cont.).In C#, we code:if (condition) statement1;E.g.if (Age >= 18) Console.Write (“Adult”);Selection (cont.).How (cont.).What happens if an Age of 25 is entered?What happens if an Age of 15 is entered?What if we wanted to display “Youth” as well?if (Age >= 18) Console.Write (“Adult”); // consequent else Console.Write (“Youth”); // alternate consequentSelection (cont.).How (cont.).Note that the ( ) around the condition are mandatory and that there must be a semi-colon after the consequent *and* alternate consequent.See more examples in Ifs1.cs and Ifs2.cs.Selection (cont.).How (cont.).Notes on Ifs1.cs.1. How characters are handled.Note single quotes.Note Convert.ToChar.2. Assignment v. identity.Selection (cont.).How (cont.).Notes on Ifs2.cs.1. Use of character string.2. Note double quotes.3. Note the Equals method.4. Efficiency:Which is more efficient, a long if…else statement or many separate ifs? Why?Selection (cont.).How (cont.).With separate ifs, what happens if the Grade is “A”?4. Testing: Why is testing more complex once ifs are used? How many times will we need to run the program?4. Conditions.An if statement is of the form:if (condition) statement1;But: what exactly is a condition? It is an expression, but of a special type.NOT like 7+57+5 is an arithmetic expression, so evaluates to a numberConditions (cont.).But a condition, such as (Age>= 18) evaluates to…?True or False.I.e. it is a logical (Boolean) expression.Note the significance of the ALU: the arithmetic and logic unit.Conditions (cont.).1. Relational conditions: compare data values using the relational operators:= = identity (‘equals’)!= non-identity (‘does not equal’)<><=, >=Conditions (cont.).Relational conditions can be used to compare numbers (int, float) and single characters.Limitation 1: Be careful with real numbers because of limited precision do { } while (Num ! = 0) // this condition may never // become false. Num may be 0.0003Conditions (cont.).Limitation 2:Strings are arrays of characters and so, although “==“ is overloaded to handle strings, a more sophisticated method is typically used to compare them, such as:if Astring.Equals (AnotherString) Console.Write (“The strings are the same”);Conditions (cont.).How are comparison of characters made? How does it know ‘A’ > ‘B’?ASCII codes. ‘0’ 48 …. ‘9’ 57 ‘A’ 65 …. ‘Z’ 90 ‘a’ 97 …. ‘z’ 122Conditions (cont.).2. Logical conditions.A. Negation. (Not), uses !Reverses truth value !X is true iff X is false.(!Age < 18) is equivalent to (Age >= 18)Note: < and > are not opposites. Fallacy of false alternatives.(!Age = = 18) is equivalent to (Age != 18)Conditions (cont.).Logical conditions.B. Conjunction. (And), uses &&.E.g. if ((Age >= 18) && (Gender = = ‘M’)) Console.Write (“Adult male.”);When is the Write statement executed?When would it NOT be executed?Conditions (cont.).Logical conditions.C. Disjunction. (Or), uses | |.E.g. if ((Grade = = ‘A’) | | (Grade = = ‘B’)) Console.Write (“Well done.”);When is the Write statement executed?When would it NOT be executed?Conditions (cont.).In that case the 2 conditions are mutually exclusive. But could have a test like:if ( (Doughnuts > 50) || (LemonBars > 30) ) Console.Write (“Sufficient for bake sale”);Yet, given the fact that these 2 conditions are not mutually exclusive---and Lutherans’ notorious zeal for bakery---both conditions could be true.Conditions (cont.).Logical conditions.D. Complex conditions. if ( ((Temp <= 75) && (Humidity <= 60)) | | ((Temp <= 85) &&


View Full Document

CUW CSC 250 - The Selection Control Structure 1

Download The Selection Control Structure 1
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view The Selection Control Structure 1 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view The Selection Control Structure 1 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?