Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 240 – Lecture 7 Boolean Operations, Increment and Decrement Operators, Constant Types, enum Types, Precedence.

Similar presentations


Presentation on theme: "CS 240 – Lecture 7 Boolean Operations, Increment and Decrement Operators, Constant Types, enum Types, Precedence."— Presentation transcript:

1 CS 240 – Lecture 7 Boolean Operations, Increment and Decrement Operators, Constant Types, enum Types, Precedence

2 Booleans – Review Booleans are values corresponding to the truth of a logical statement. True and False In many programming languages, they're treated explicitly as unique values TRUE and FALSE or similarly but written differently. However, in C, Booleans are treated numerically. 0 is false Everything else is true Any numeric-typed value can be treated as a Boolean value.

3 Booleans – Review Comparison Operators
These should be covered from previous classes in other languages. The only thing that is different should be the way they're written. The comparison operators are as follows: x > y x greater than y x >= y x greater than or equal to y x == y x equal to y x <= y x less than or equal y x < y x less than y x != y x not equal to y

4 Boolean – Disjunction and Conjunction
Up to this point in the course, no assignment you've needed to do required more than one Boolean statement. You could always nest if-statements if needed. However, it's often the case that you can not fully determine a condition with just one Boolean statement. What if a condition involves more than two values that need to be compared? A disjunction is a combination of two or more logical statements which is true only if at least one of it's logical statements are true. A conjunction is a combination of two or more logical statements which is true only if all of it's logical statement are true.

5 Booleans – && and || operators
In C, there are Boolean operators for combining other Boolean statements together. The "logical and" (&&) operator indicates a conjunction. The "logical or" (||) operator indicates a disjunction. x && y x || y x && y > z Again, in C, Booleans are numeric in their value, so these operations return 1 and 0 for TRUE and FALSE respectively. You can make longer conjunctions or disjunctions with these operators, as well. w && x && y && z w || x || y || z

6 Booleans – !, the "not" operator
Sometimes, an existing logical statement is the exact opposite of what we want for a Boolean condition. Instead of rewriting it, we can invert it's value to the opposite truth value. This is called the "negation" of the statement. The unary not (!) operator, inverts the Booleans value of its operand. !1 evaluates to 0 !0 evaluates to 1 However, since non-zero numbers are considered true, !x evaluates to 0, for non-zero x

7 Booleans – De Morgan's Laws
De Morgan's Laws are a relationship between conjunctions and disjunctions which allows you to switch from one to the other. !(x || y) is equal to (!x && !y) !(x && y) is equal to (!x || !y) The laws in English: the negation of a disjunction is the conjunction of the negations; the negation of a conjunction is the disjunction of the negations; Oftentimes, this produces better-readable code. This also works for longer conjunctions.

8 Booleans – Short-circuiting Property
The "logical and" and "logical or" operators have an interesting property call short-circuiting. When the final result of the computation is obvious from the first operand, the second operand is not evaluated. x && y x || y For the "logical and", if x is false, we don't need to evaluate y. For the "logical or", if x is true, we don't need to evaluate y. For example, the calls below to doSomething() never happen. 0 && doSomething() 1 || doSomething()

9 Operators – ++ and -- We very briefly discussed the ++ operator when we introduced for- loops. These operators are designed to make changes to memory before or after the evaluation of an expression. ++x pre-increment operator; adds 1 to x before expression is eval x++ post-increment operator; adds 1 to x after expression is eval --x pre-decrement operator; minus 1 from x before expression is eval x-- post-decrement operator; minus 1 from x after expression is eval These only work on expressions which describe a location in memory! Variable names (x), array elements (a[i]), value-at addresses (*ptr)

10 Variables – const type Often times, you want to allocate memory for a value that will not change once it's calculated the first time. Symbolic constants are not ideal because they are simply in-text replacements. const int var = 7; By using the keyword const, we can indicate that the variable should be stored in read-only memory by the compiler. Where and how exactly is implementation specific on a per-compiler basis. Constant variables can only be assigned a value once and it must be during declaration. Otherwise, it will only ever contain junk.

11 Types – enum types Enumerations are ordered listings of things of the same kind. Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday January, February, March, April, May, … You get the idea. Since the elements of those lists are ordered, we can treat them as numeric values instead. enum name {value1, value2, …}; Declaring an enum type does not take up any memory. Each value in the enum declaration corresponds to its position as an integer. The value names can be used elsewhere in the code as if they were proper values.

12 Types – enum examples Months
enum month {JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC}; Days of the Week enum dow {SUN, MON, TUE, WED, THU, FRI, SAT}; Boolean enum boolean {FALSE, TRUE};

13 Variables – enum variables
When using enum types, we create multiple integer constants which can be used as values. We can also store those values in variables of the specific enum type. For example, with days of the week: enum dow {SUN,MON,TUE,WED,THU,FRI,SAT}; To declare a variable of type enum dow, enum dow name; enum dow today = THU; This takes up the same space in memory as an integer. enum variables always default to the first value when declared.

14 Precedence and Associativity of Operators
sign dereference address casting Associativity ( ) [ ] -> Left to right ! ~ * & (type) sizeof right to left * / % left to right left to right << >> left to right < <= > >= left to right = = != left to right & left to right ^ left to right | left to right && left to right || left to right ?: right to left = += -= *= /= %= &= ^= |= <<= >>= right to left , left to right Precedence

15 Precedence and Associativity
Operators in the same row have the same precedence Operators are in order of decreasing precedence Associativity Determine the order if the operators have the same precedence


Download ppt "CS 240 – Lecture 7 Boolean Operations, Increment and Decrement Operators, Constant Types, enum Types, Precedence."

Similar presentations


Ads by Google