The Selection Control Structure 2. 0. Questions on Program 3? 1.Review. 2. Truth-tables. 3. Forms of...
-
Upload
godwin-merritt -
Category
Documents
-
view
212 -
download
0
Transcript of The Selection Control Structure 2. 0. Questions on Program 3? 1.Review. 2. Truth-tables. 3. Forms of...
![Page 1: The Selection Control Structure 2. 0. Questions on Program 3? 1.Review. 2. Truth-tables. 3. Forms of the if statement. 4. The switch statement.](https://reader035.fdocuments.in/reader035/viewer/2022072013/56649e565503460f94b4e920/html5/thumbnails/1.jpg)
The Selection Control Structure 2.0. Questions on Program 3?
1. Review.2. Truth-tables.
3. Forms of the if statement.4. The switch statement.
![Page 2: The Selection Control Structure 2. 0. Questions on Program 3? 1.Review. 2. Truth-tables. 3. Forms of the if statement. 4. The switch statement.](https://reader035.fdocuments.in/reader035/viewer/2022072013/56649e565503460f94b4e920/html5/thumbnails/2.jpg)
0. Questions on Program 3?
1) Note: you do not have to enter the date as a string—you can use separate, numeric, variables.
2) To check if something is evenly divisible, you can use modulus e.g.
if ((X % 100) == 0)
Console.Write (X + “ is evenly divisible by 100”);
3) Check your answer for “normal” and “unusual” cases.
Normal: 1996 (leap year) 1999 (Not a leap year).
Unusual: 1700, 1800, 1900 and 2100 are not leap years, but 1600, 2000, and 2400 are leap years
![Page 3: The Selection Control Structure 2. 0. Questions on Program 3? 1.Review. 2. Truth-tables. 3. Forms of the if statement. 4. The switch statement.](https://reader035.fdocuments.in/reader035/viewer/2022072013/56649e565503460f94b4e920/html5/thumbnails/3.jpg)
1. Review.
The selection or branching code is implemented by the if statement.
if (condition) //antecedent statement1; // consequentelse
statement2; // optional alternate consequentFrom last time:Why is testing more complex once ifs are used? How many times will we need to run the program?
![Page 4: The Selection Control Structure 2. 0. Questions on Program 3? 1.Review. 2. Truth-tables. 3. Forms of the if statement. 4. The switch statement.](https://reader035.fdocuments.in/reader035/viewer/2022072013/56649e565503460f94b4e920/html5/thumbnails/4.jpg)
Review (cont.).
What are conditions?A condition is an expression which….Types.The 3 types of condition are:1. Relational (meaning?)2. Logical (meaning?)3. Boolean (meaning?)
![Page 5: The Selection Control Structure 2. 0. Questions on Program 3? 1.Review. 2. Truth-tables. 3. Forms of the if statement. 4. The switch statement.](https://reader035.fdocuments.in/reader035/viewer/2022072013/56649e565503460f94b4e920/html5/thumbnails/5.jpg)
2. Truth-tables.
How do we evaluate conditions? That is, how do we tell when they will be true, when false?
We need a method which shows every possible combination of T’s and F’s: Truth tables.
Let X, Y, Z stand for conditions.E.g. X could be (Age >= 18)Y could be (Count > 0), etc.
![Page 6: The Selection Control Structure 2. 0. Questions on Program 3? 1.Review. 2. Truth-tables. 3. Forms of the if statement. 4. The switch statement.](https://reader035.fdocuments.in/reader035/viewer/2022072013/56649e565503460f94b4e920/html5/thumbnails/6.jpg)
Truth-tables (cont.).
A. Negation (!). ! X F T T FB. Conjunction (&&). X && Y T T T T F F F F T F F F
![Page 7: The Selection Control Structure 2. 0. Questions on Program 3? 1.Review. 2. Truth-tables. 3. Forms of the if statement. 4. The switch statement.](https://reader035.fdocuments.in/reader035/viewer/2022072013/56649e565503460f94b4e920/html5/thumbnails/7.jpg)
Truth-tables (cont.).
C. Disjunction (| |). X | | Y T T T T T F F T T F F FWhat if we had: X && (Y | | Z)?How many rows on the truth-table?How do we know? Formula.
![Page 8: The Selection Control Structure 2. 0. Questions on Program 3? 1.Review. 2. Truth-tables. 3. Forms of the if statement. 4. The switch statement.](https://reader035.fdocuments.in/reader035/viewer/2022072013/56649e565503460f94b4e920/html5/thumbnails/8.jpg)
Truth-tables (cont.).
Equivalencies.2 conditions are equivalent if, and only if:They have the same pattern of T’s and F’s under
their main connective (logical operator).But what is “the main connective”?Examples:What is !(X | | Y) equivalent to?What is !(X && Y) equivalent to?
![Page 9: The Selection Control Structure 2. 0. Questions on Program 3? 1.Review. 2. Truth-tables. 3. Forms of the if statement. 4. The switch statement.](https://reader035.fdocuments.in/reader035/viewer/2022072013/56649e565503460f94b4e920/html5/thumbnails/9.jpg)
Truth-tables (cont.).
Why does correct logic matter to the programmer?
Consider the following coding error:if ((Grade != ‘A’) | | (Grade != ‘B’)
| | (Grade != ‘C’) | | (Grade != ‘D’) )
Console.Write(“Fail”);
Who fails? Why?
![Page 10: The Selection Control Structure 2. 0. Questions on Program 3? 1.Review. 2. Truth-tables. 3. Forms of the if statement. 4. The switch statement.](https://reader035.fdocuments.in/reader035/viewer/2022072013/56649e565503460f94b4e920/html5/thumbnails/10.jpg)
Truth-tables (cont.).
Therefore, we should code: if ((Grade != ‘A’) && (Grade != ‘B’)
&& (Grade != ‘C’) && (Grade != ‘D’) )
Console.Write (“Fail”);Precedence of logical operators:1) !2) &&3) | |
![Page 11: The Selection Control Structure 2. 0. Questions on Program 3? 1.Review. 2. Truth-tables. 3. Forms of the if statement. 4. The switch statement.](https://reader035.fdocuments.in/reader035/viewer/2022072013/56649e565503460f94b4e920/html5/thumbnails/11.jpg)
Full precedence.
1) ! + - (as one-place operators)2) *, /, %3) +, - (as binary operators)4) <, <=, >, >=5) ==, !=6) &&7) ||8) = (assignment)
![Page 12: The Selection Control Structure 2. 0. Questions on Program 3? 1.Review. 2. Truth-tables. 3. Forms of the if statement. 4. The switch statement.](https://reader035.fdocuments.in/reader035/viewer/2022072013/56649e565503460f94b4e920/html5/thumbnails/12.jpg)
Truth-tables (cont.).
Short-circuit evaluation.When the compiler evaluates A | | B it
can stop with A if…….But it will have to check both if…..When the compiler evaluates A && B it
can stop with A if…….But it will have to check both if…..
![Page 13: The Selection Control Structure 2. 0. Questions on Program 3? 1.Review. 2. Truth-tables. 3. Forms of the if statement. 4. The switch statement.](https://reader035.fdocuments.in/reader035/viewer/2022072013/56649e565503460f94b4e920/html5/thumbnails/13.jpg)
3. Forms of the if statement.
A. Simple if (no else, no blocks). if (Month_Number = = 1)
Console.Write (“January”);
B. if…else. if (Count ! = 0)
Average = Total / Count;
else
Average = 0;
![Page 14: The Selection Control Structure 2. 0. Questions on Program 3? 1.Review. 2. Truth-tables. 3. Forms of the if statement. 4. The switch statement.](https://reader035.fdocuments.in/reader035/viewer/2022072013/56649e565503460f94b4e920/html5/thumbnails/14.jpg)
Forms of the if statement (cont.).
C. Compound/block ifs.These allow the use of compound statements =
a series of statements enclosed in { }Can be used in antecedent/consequent. if (Gender = = ‘F’) { // begin female Female++; Console.Write ( “Female”); } // end female
![Page 15: The Selection Control Structure 2. 0. Questions on Program 3? 1.Review. 2. Truth-tables. 3. Forms of the if statement. 4. The switch statement.](https://reader035.fdocuments.in/reader035/viewer/2022072013/56649e565503460f94b4e920/html5/thumbnails/15.jpg)
Forms of the if statement (cont.).
Compound/block ifs (cont.).Likewiseelse {// begin male Male++; Console.Write (“Male”);} // end maleNote use of commenting
![Page 16: The Selection Control Structure 2. 0. Questions on Program 3? 1.Review. 2. Truth-tables. 3. Forms of the if statement. 4. The switch statement.](https://reader035.fdocuments.in/reader035/viewer/2022072013/56649e565503460f94b4e920/html5/thumbnails/16.jpg)
Forms of the if statement (cont.).
D. Nested ifs.An if inside an if (the guard in Monty Python
and the Holy Grail: if,if, if,…er..) if (Age >5)
if (Age <= 18)
Console.Write (“School”);
else
Console.Write (“College or job”);
![Page 17: The Selection Control Structure 2. 0. Questions on Program 3? 1.Review. 2. Truth-tables. 3. Forms of the if statement. 4. The switch statement.](https://reader035.fdocuments.in/reader035/viewer/2022072013/56649e565503460f94b4e920/html5/thumbnails/17.jpg)
Forms of the if statement (cont.).
Nested ifs (cont.).What does the else match with? Why?E. The problem of the dangling else. if (Grade != ‘F’)
if (Grade == ‘D’)
Console.Write( “Academic probation”);
else
Console.Write(“Failing.”);
![Page 18: The Selection Control Structure 2. 0. Questions on Program 3? 1.Review. 2. Truth-tables. 3. Forms of the if statement. 4. The switch statement.](https://reader035.fdocuments.in/reader035/viewer/2022072013/56649e565503460f94b4e920/html5/thumbnails/18.jpg)
Forms of the if statement (cont.).
Who will fail?Why?Solution? Terminate nested if. if (Grade != ‘F’) { if (Grade == ‘D’) Console.Write( “Academic probation”); } //end else Console.Write(“Failing.”);
![Page 19: The Selection Control Structure 2. 0. Questions on Program 3? 1.Review. 2. Truth-tables. 3. Forms of the if statement. 4. The switch statement.](https://reader035.fdocuments.in/reader035/viewer/2022072013/56649e565503460f94b4e920/html5/thumbnails/19.jpg)
4. The switch statement.
See example: Switch.csNote 1: switch condition and case label listNote 2: break. What does it do?Note 3: default. Why?Limitation of switch?
Only designed for ordinal values.
Meaning?