1 / 14

CS 106 Logic and Conditionals (Boolean Expressions)

CS 106 Logic and Conditionals (Boolean Expressions). Enlarging our Repertoire. So far we know how to write VB statements that change the values of variables, including properties of objects

oleg
Télécharger la présentation

CS 106 Logic and Conditionals (Boolean Expressions)

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. CS 106Logic and Conditionals (Boolean Expressions)

  2. Enlarging our Repertoire • So far we know how to write VB statements that change the values of variables, including properties of objects • Our next type of statement is a conditional: it allows us to change what the program does based on the outcome of a test

  3. Tests and Choices • To allow a program to make choices based on conditions we need to introduce a new programming construct • But first we look at how we program the tests that will determine which branch our program takes

  4. Expressions • An expression in VBA is a chunk of code that evaluates to a value • Arithmetic expressions include simple numbers and expressions built with arithmetic operators and functions, such as 44 varA + 12 • String expressions include simple strings and expressions built with the string operator & and possibly string functions “Hello, I’m a string.” “Your cost is “ & FormatCurrency(iceCreamTotal)

  5. The Boolean Data Type(named for George Boole) • A Boolean variable is one that has two possible values: True and False • A Boolean expression is an expression that can evaluate to True or False • The constants True and False are the simplest Boolean expressions • More interesting examples are created by, for example, comparing two things • Complex Boolean expressions are built up out of simple ones using Boolean operations

  6. Relational Operators • Relational Operators are used to create simple Boolean expressions • They include: = equal? < > not equal? < less than? <= less than or equal? > greater than? >= greater than or equal?

  7. Examples of True/False Expressions DimvarA, varB, varCAs Double varA = 1 varB = 2 varC = 2 varA < varB is true varA <= varB is true varC < varB is false varC <= varB is true varB = varC is true ‘note dual role of the symbol =

  8. Logical Operators • More complex Boolean expressions are built using Boolean operators. • The most common Boolean operators are And, Or, and Not • Other Boolean operators, such as exclusive or, are available if needed • Not reverses the truth of its argument: Not (True) = False and vice-versa

  9. And • And is true if and only if both its arguments are true • This is called a truth table. The letters X and Y represent Boolean expressions. We have to look at all possible combinations of T and F for X and Y

  10. Or • Or is true if and only if at least one of its arguments is true. This differs from the usual meaning of “or” (this or is more like and/or)

  11. More Complex Expressions • These can be worked out using truth tables • Consider (Not X) Or (X and Y)

  12. Another Example Show Not (A And B) is logically equivalent to (Not A) Or (Not B)

  13. A Couple of Pointers • Some math/relational expressions do not translate directly. For example, A < B <= C would be written as (A < B) And (B <= C) • Be careful with And and Or. Normally both parts of an And expression are evaluated even if the first one is false, and both parts of an Or expression are evaluated even if the first one is true. Standard example: (X = 0) Or (Y/X <5) (could cause runtime error if X = 0)

  14. Other Uses for Logic • Logical expressions are a part of database queries. If you will be using databases and writing queries, understanding how to form logical expressions will be very useful. • You can use logic to form advanced queries in Google • We’ll limit ourselves to three operators: And, Or, and Not. They are sufficient to write any logical expression, though not always in the simplest way. If you want to use others, feel free, but make sure you understand their truth tables.

More Related