310 likes | 429 Views
The Selection Control Structure 1. 1. Module charts. 2. Flow-charts. 3. Selection. 4. Conditions. 1. Module charts. Module charts (continued):. Until Payment > Beginning Balance. 2. Flow-charts.
E N D
The Selection Control Structure 1. 1. Module charts. 2. Flow-charts. 3. Selection. 4. Conditions.
Module charts (continued): Until Payment > Beginning Balance
2. 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? Y N
Selection (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 consequent
Selection (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+5 7+5 is an arithmetic expression, so evaluates to a number
Conditions (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.0003
Conditions (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’ 122
Conditions (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) && (Humidity <= 40))) Console.Write (“Nice weather.”);
Conditions (cont.). • 3. Boolean constants and variables. • a. Built in constants are true and false. • b. Boolean variables are flags/switches, variables of type bool that can only have the values true or false. E.g. • bool OverDrawn;
Conditions (cont.). • Boolean constants and variables (cont.). • There are two ways to use the variable: • (1) if (Withdrawal > Balance) OverDrawn = true; else OverDrawn = false; (2) OverDrawn = (Withdrawal > Balance);
Conditions (cont.). • Boolean constants and variables (cont.). • Then one can use the Boolean variable, either: • if (OverDrawn = = true)…. • Or more simply: • if(OverDrawn) Console.Write(“Oh, no, not you again.”);
Conditions (cont.). • Boolean constants and variables (cont.). • To test the opposite value: • if(!OverDrawn) Console.Write (“Modern banking charges will soon rectify this”); • Uses of Boolean variables: • 1. Switches/flags. • 2. Self-documenting abbreviations of complex conditions.
Conditions (cont.). • Boolean constants and variables (cont.). • ValidGrade = ((Grade = = ‘J’) | | (Grade = = ‘M’) | | (Grade = = ‘S’ ) ); if (!ValidGrade) Console.Write (“Invalid staff grade”);