Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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.

2 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

3 1. Review. §The selection or branching code is implemented by the if statement. §if (condition) //antecedent § statement1; // consequent §else statement2; // optional alternate consequent §From last time: §Why is testing more complex once ifs are used? §How many times will we need to run the program?

4 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?)

5 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.

6 Truth-tables (cont.). §A.Negation (!). ! X § F T § T F §B.Conjunction (&&). X && Y § T T T § T F F § F F T § F F F

7 Truth-tables (cont.). §C.Disjunction (| |). X | | Y § T T T § T T F § F T T § F F F §What if we had: X && (Y | | Z)? §How many rows on the truth-table? §How do we know? Formula.

8 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?

9 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?

10 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) | |

11 Full precedence. §1) ! + - (as one-place operators) §2) *, /, % §3) +, - (as binary operators) §4), >= §5) ==, != §6) && §7) || §8) = (assignment)

12 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…..

13 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;

14 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

15 Forms of the if statement (cont.). §Compound/block ifs (cont.). §Likewise §else {// begin male § Male++; § Console.Write (“Male”); §} // end male §Note use of commenting

16 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”);

17 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.”);

18 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.”);

19 4. The switch statement. §See example: Switch.cs §Note 1: switch condition and case label list §Note 2: break. What does it do? §Note 3: default. Why? §Limitation of switch? Only designed for ordinal values. Meaning?


Download ppt "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."

Similar presentations


Ads by Google