Boolean Data Type & Expressions Outline Basic Data Types 1. Boolean Data Type & Expressions Outline Numeric 2. Basic Data Types – int 3. C Boolean Data Types: char or int/ – float Boolean Declaration Non-numeric 4. Boolean or Character? – char 5. Boolean Literal Constants #include <stdio.h> 6. Boolean Expressions/ int main () Boolean Operations ¡ /* main */ 7. Example: Boolean Literal Constants & Expressions float standard_deviation, relative_humidity; int count, number_of_silly_people; 8. Example: Boolean Variables char middle_initial, hometown[30]; ¢ 9. Relational Operations /* main */ 10. Example: Relational Expressions 11. Structure of Boolean Expressions 12. Precedence Order of Boolean Operations 13. Another Boolean Precedence Example 14. Example: Boolean Precedence Order 15. Example: More Relational Expressions 16. Example: More Relational Expressions (continued) 17. Short Circuiting 18. Exercise: Writing a Program 1 2 C Boolean Data Type: char or int Boolean or Character? The C data type typically used for storing Boolean values is char, Question: How does the C compiler know that a particular char although int will also work. declaration is a Boolean rather than a character? Like numeric data types, Booleans have particular ways of being Answer: It doesn’t. stored in memory and of being operated on. % cat shortcircuit.c #include <stdio.h> Conceptually, a Boolean value represents a single bit in memory, char int int main () although the and data types aren’t implemented this ¡ /* main */ way — if for no other reason than that computers can’t address a const int maximum_short_height_in_cm = 170; single bit, since the smallest collection of bits that they can address int my_height_in_cm = 160; char I_am_Henry = 1; is a byte (or, in a few cases, a word). char I_am_tall; char my_middle_initial = 'J'; Boolean Declaration I_am_tall = char CS1313 lectures are fascinating; (!I_am_Henry) || (my_height_in_cm > maximum_short_height_in_cm); printf("I_am_Henry = %d n", I_am_Henry); This declaration tells the compiler to grab a group of bytes, name printf("my_height_in_cm = %d n", my_height_in_cm); CS1313 lectures are fascinating printf("I_am_tall = %d n", I_am_tall); them , and think of them printf("my_middle_initial = %c n", my_middle_initial); as storing a Boolean value (either True or False). ¢ /* main */ How many bytes? % gcc -o shortcircuit shortcircuit.c % shortcircuit I_am_Henry = 1 Even though conceptually a Boolean represents a single bit, in prac- my_height_in_cm = 160 tice char variables are usually implemented using 8 bits (1 byte): I_am_tall = 0 my_middle_initial = J CS1313 lectures are fascinating : ? ? ? ? ? ? ? ? Whether a char is a Boolean or a character depends entirely on how you use it in the program. 3 4 Boolean Literal Constants Boolean Expressions A Boolean literal constant can have either of two possible values Just like a numeric arithmetic expression, a Boolean expression is a (but not both): combination of Boolean terms (such as variables, named constants and literal constants), Boolean operators (e.g., !, &&, ||, relational to represent false: 0 comparisons) and parentheses. to represent true: anything other than 0 (usually 1) I am happy We can use Boolean literal constants in several ways: !I am happy In declaring and initializing a named constant: it is raining && it is cold const char true = 1; it is raining || it is cold In declaring and initializing a variable: (!it is raining) || (it is cold && I am happy) char getting a bad grade = 0; In an assignment: Boolean Operations this is my first guess = 1; In an expression: Like arithmetic operations, Boolean operations come in two vari- Henry is short && 1; eties: unary and binary. A unary operation is an operation that uses only one term; a binary operation uses two terms. Boolean opera- The first two of these uses are considered good programming prac- tions include: tice, AND SO IS THE THIRD, which is a way that Booleans are Operation Name Kind Operator Usage Effect different from numeric data; that is, it’s acceptable to use a Boolean Identity Unary None x None literal constant in an assignment statement. Negation Unary ! !x Inverts value of x Conjunction Binary && x && y 1 if both x is nonzero As for using Boolean literal constants in expressions, it’s not so (AND) and y is nonzero; much that it’s considered bad programming practice, it’s just that otherwise 0 it’s kind of pointless. Disjunction Binary || x || y 1 if either x is nonzero (Inclusive OR) or y is nonzero, or both; otherwise 0 Note: C Boolean expressions evaluate either to 0 (representing false) or to 1 (representing true). 5 6 Example: Example: Boolean Variables Boolean Literal Constants & Expressions % cat prog_logic.c #include <stdio.h> % cat lgcexprsimple.c #include <stdio.h> int main () ¡ /* main */ int main () ¡ int project_due_soon; /* main */ int been_putting_project_off; const char true = 1, false = 0; int start_working_on_project_today; printf(" true = %d, false = %d n", true, false); printf("Is it true that you have a "); printf("!true = %d, !false = %d n", !true, !false); printf("programming project due soon? n"); printf(" n"); scanf("%d", &project_due_soon); printf("true || true = %d n", true || true); printf("Is it true that you have "); printf("true || false = %d n", true || false); printf("been putting off working on it? n"); printf("false || true = %d n", false || true); scanf("%d", &been_putting_project_off); printf("false || false = %d n", false || false); start_working_on_project_today = printf(" n"); project_due_soon && been_putting_project_off; printf("true && true = %d n", true && true); printf("Is it true that you should start "); printf("true && false = %d n", true && false); printf("working on it today? n"); printf("false && true = %d n", false && true); printf("ANSWER: %d n", printf("false && false = %d n", false && false); ¢ start_working_on_project_today); /* main */ ¢ /* main */ % gcc -o lgcexprsimple lgcexprsimple.c % gcc -o prog_logic prog_logic.c % lgcexprsimple % prog_logic true = 1, false = 0 Is it true that you have a programming project due soon? !true = 0, !false = 1 1 Is it true that you have been putting off working on it? true || true = 1 1 true || false = 1 Is it true that you should start working on it today? false || true = 1 ANSWER: 1 false || false = 0 true && true = 1 true && false = 0 false && true = 0 false && false = 0 Notice that a Boolean expression always evaluates to either 1 or 0. 7 8 Relational Operations Example: Relational Expressions A relational operation is a binary operation that compares two nu- % cat relational.c meric operands and produces a Boolean result. For example: #include <stdio.h> int main () CS1313 lab section == 14 ¡ /* main */ cm per km != 100 int CS1313_size, METR2413_size; age < 21 printf("How many students are in CS1313? n"); scanf("%d", &CS1313_size); number of students <= number of chairs printf("How many students are in METR2413? n"); credit hours > 30 scanf("%d", &METR2413_size); electoral votes >= 270 printf("%d == %d: %d n", CS1313_size, METR2413_size, CS1313_size == METR2413_size); printf("%d != %d: %d n", CS1313_size, METR2413_size, The relational operations are: CS1313_size != METR2413_size); printf("%d < %d: %d n", CS1313_size, METR2413_size, CS1313_size < METR2413_size); Operation Name Operand Usage Result printf("%d <= %d: %d n", CS1313_size, METR2413_size, Equal == x == y 1 if the value of x is exactly CS1313_size <= METR2413_size); the same as the value of y; printf("%d > %d: %d n", CS1313_size, METR2413_size, CS1313_size > METR2413_size); otherwise 0 printf("%d >= %d: %d n", CS1313_size, METR2413_size, Not Equal != x != y 1 if the value of x is CS1313_size >= METR2413_size); different from the value of y; ¢ /* main */ otherwise 0 % gcc -o relational relational.c % relational Less Than < x < y 1 if the value of x is less How many students are in CS1313? than the value of y; 64 otherwise 0 How many students are in METR2413? Less Than Or <= x <= y 1 if the value of x is less 57 64 == 57: 0 Equal To than or equal to the value of y; 64 != 57: 1 otherwise 0 64 < 57: 0 Greater Than > x > y 1 if the value of x is greater 64 <= 57: 0 than the value of y; 64 > 57: 1 64 >= 57: 1 otherwise 0 Greater Than Or >= x >= y 1 if the value of x is greater Equal To than or equal to the value of y; otherwise 0 9 10 Structure of Boolean Expressions Precedence Order of Boolean Operations A Boolean expression can be long and complicated. For example: In the absence of parentheses to explicitly state the order of opera- a || b || c && d && !e tions, the order of precedence is: 1. relational operations, left to right Terms and operators can be mixed together in almost limitless vari- ety, but they must follow the rule that a unary operator has a term 2. !, left to right immediately to its right and a binary operator has terms on both its 3. &&, left to right left and its right. 4. ||, left to right After taking into account the above rules, the expression as a whole Parentheses can be placed around any unary or binary expression: is evaluated left to right. For example: (a || b) || (c && (d && (!e))) 0 && 1 || 1 && 1 Putting a term in parentheses may change the value of the expres- 0 || 1 && 1 sion, because a term inside parentheses will be calculated first. For example: 0 || 1 a || b && c 1 is evaluated as “b AND c, OR a,” but (a || b) && c but is evaluated as “a OR b, AND c.” 0 && (1 || 1) && 1 0 && 1 && 1 So, Boolean expressions look and behave a lot like arithmetic ex- pressions. 0 && 1 0 11 12 Another Boolean Precedence Example Example: Boolean Precedence Order ! 0 || 1 % cat lgcexpr.c #include <stdio.h> 1 || 1 int main () ¡ /* main */ printf("0 && 1 || 1 && 1 = %d n", 1 0 && 1 || 1 && 1); printf("0 && (1 || 1) && 1 = %d n", 0 && (1 || 1) && 1); but printf("! 0 || 1 = %d n", ! 0 || 1); printf("!(0 || 1) = %d n", ! (0 || 1) !(0 || 1)); ¢ /* main */ % gcc -o lgcexpr lgcexpr.c ! 1 % lgcexpr 0 && 1 || 1 && 1 = 1 0 && (1 || 1) && 1 = 0 0 ! 0 || 1 = 1 !(0 || 1) = 0 Rule of Thumb: if you can’t remember the priority order of the op- erators, use lots of parentheses.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages9 Page
-
File Size-