Presentation is loading. Please wait.

Presentation is loading. Please wait.

Insight Through Computing 3. Introduction to Conditionals Boolean expressions The If-Else Construct And, or, not.

Similar presentations


Presentation on theme: "Insight Through Computing 3. Introduction to Conditionals Boolean expressions The If-Else Construct And, or, not."— Presentation transcript:

1 Insight Through Computing 3. Introduction to Conditionals Boolean expressions The If-Else Construct And, or, not

2 Insight Through Computing What We Cannot Do So Far If the value of the arithmetic expression Dice1 + Dice2 is seven, then increase the value of the variable GamesWon by one. We cannot make a computation contingent upon other things.

3 Insight Through Computing The If-Else Construct Solves this Problem We will introduce this language feature by solving problems about the behavior of a given quadratic on a given interval L <= x <= R.

4 Insight Through Computing Assume Variables b,c,L,R are Initialized E.g., b = input(‘Enter b’:) c = input(‘Enter c’:) L = input(‘Enter L’:) R = input(‘Enter R’:)

5 Insight Through Computing LR The Situation

6 Insight Through Computing Write a fragment that prints “yes” if q(x) increases across the interval and “no” if it does not. Problem 1

7 Insight Through Computing LR No!

8 Insight Through Computing LR Yes! Requirement: x c <= L

9 Insight Through Computing Solution Fragment xc = -b/2; if xc <= L disp(‘Yes’) else disp(‘No’) end

10 Insight Through Computing Write a fragment that prints the maximum value that q(x) attains on the interval. Problem 2

11 Insight Through Computing LR Maximum at L

12 Insight Through Computing LR Maximum at R Depends on whether xc is to the right or left of the interval midpoint.

13 Insight Through Computing Solution Fragment xc = -b/2; Mid = (L+R)/2; if xc <= Mid maxVal = R^2 + b*R + c else maxVal = L^2 + b*L + c end

14 Insight Through Computing Write a fragment that prints “yes” if xc is in the interval and “no” if xc is not in the interval. Problem 3

15 Insight Through Computing LR No! Because xc < L

16 Insight Through Computing LR No! Because R < xc

17 Insight Through Computing LR Yes! Because L <= xc and xc <= R

18 Insight Through Computing Solution Fragment xc = -b/2; if (L <= xc) && (xc <= R) disp(‘Yes’) else disp(‘No’) end Illegal: L <= xc <= R

19 Insight Through Computing Saying the Opposite xc is in the interval [L,R] if L <= xc and xc <= R xc is not in the interval [L,R] if xc < L or R < xc

20 Insight Through Computing Another Solution Fragment xc = -b/2; if (xc < L) || (R < xc) disp(‘No’) else disp(‘Yes’) end

21 Insight Through Computing Solution Fragment xc = -b/2; if (L <= xc) && (xc <= R) disp(‘Yes’) else disp(‘No’) end

22 Insight Through Computing The if-else Construct if end else boolean expression Commands to execute if the expression if TRUE Commands to execute if the expression if FALSE

23 Insight Through Computing Boolean Expressions Their value is either true or false. Made up of comparisons that are either true or false. Connected by logical operators: and, or, not (xc < L) || (R < xc)

24 Insight Through Computing Boolean Expressions (xc < L) || (R < xc) Their value is either true or false. Made up of other (simpler) boolean expressions that are connected by boolean operators: and, or, not

25 Insight Through Computing Arithmetic Expressions Their value is a number. Made up of other (simpler) arithmetic expressions that are connected by arithmetic operators: +, -. *. / (x+3)*(y-z)

26 Insight Through Computing Relational Operators < Less than > Greater than <= Less than or equal to >= Greater than or equal to == Equal to ~= Not equal to

27 Insight Through Computing The And Operator && && ----------------------------------------------- FFF FT F T F F T T T

28 Insight Through Computing The Or Operator || || ----------------------------------------------- FFF FT T T F T T T T

29 Insight Through Computing The not Operator ~ ~ ----------------------------------------------- FT T F

30 Insight Through Computing Question Time X = 6; Y = 8; If X < Y Y = Y/2; else X = X/2; end What is the value of X and Y after the following script is executed: A: X is 3 and Y is 4 B: X is 6 and Y is 8 C: X is 5 and Y is 4 D: X is 3 and Y is 8

31 Insight Through Computing Logical operators (&&, ||) “short-circuit” a > b && c > d true Go on a > b && c > d false Stop Entire expression is false since the first part is false A && condition short- circuits to false if the left operand evaluates to false. A || condition short- circuits to __________ if _________________ ___________________


Download ppt "Insight Through Computing 3. Introduction to Conditionals Boolean expressions The If-Else Construct And, or, not."

Similar presentations


Ads by Google