1 / 31

3. Introduction to Conditionals

3. Introduction to Conditionals. Boolean expressions The If-Else Construct And, or, not. What We Cannot Do So Far. We cannot make a computation contingent upon other things. If the value of the arithmetic expression Dice1 + Dice2 is seven, then increase

raven-booth
Télécharger la présentation

3. Introduction to Conditionals

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

  3. The If-Else ConstructSolves 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. Assume Variables b,c,L,Rare Initialized E.g., b = input(‘Enter b’:) c = input(‘Enter c’:) L = input(‘Enter L’:) R = input(‘Enter R’:)

  5. The Situation L R

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

  7. No! L R

  8. Yes! Requirement: xc <= L L R

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

  10. Write a fragment that printsthe maximum value thatq(x) attains on the interval. Problem 2

  11. Maximum at L L R

  12. Maximum at R Depends on whether xc is to the right or left of the interval midpoint. L R

  13. 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. Write a fragment that prints“yes” if xc is in the intervaland “no” if xc is not in theinterval. Problem 3

  15. No! Because xc < L L R

  16. No! Because R < xc L R

  17. Yes! Because L <= xc and xc <= R L R

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

  19. 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. Another Solution Fragment xc = -b/2; if (xc < L) || (R < xc) disp(‘No’) else disp(‘Yes’) end

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

  22. Theif-elseConstruct if boolean expression Commands to execute if the expression if TRUE else Commands to execute if the expression if FALSE end

  23. 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. Boolean Expressions Their value is either true or false. Made up of other (simpler) boolean expressions that are connected by boolean operators: and, or, not (xc < L) || (R < xc)

  25. 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. Relational Operators <Less than >Greater than <=Less than or equal to >=Greater than or equal to ==Equal to ~=Not equal to

  27. The And Operator && && ----------------------------------------------- F F F F T F T F F T T T

  28. The Or Operator || || ----------------------------------------------- F F F F T T T F T T T T

  29. The not Operator ~ ~ ----------------------------------------------- F T T F

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

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

More Related