I. Boolean expressions A. Boolean values ------------------------------------------ BOOLEAN VALUES IN C For purposes of conditionals (if-statements, conditional expressions) 0 non-zero With #include the type bool and two values: ------------------------------------------ B. Boolean Expressions 1. Boolean operators ------------------------------------------ OPERATORS ON BOOLEANS Negation: ! like Short-circuit operators: && like || like true && B == false && B == true || B == false || B == Bit-wise operators & | ~ ^ 07 & 07 == 05 & 07 == 01 & 07 == 00 & 00 == 07 | 00 == 05 | 07 == 05 | 02 == 05 | 03 == 03 | 00 == ~07 == ~06 == ~05 == ~04 == ------------------------------------------ 2. Boolean-valued expressions ------------------------------------------ BOOLEAN-VALUED EXPRESSIONS Comparisons have _Bool values: 2 < 3 3 > 2 2 == 2 2 != 3 !(2 == 3) 3 <= 3 2 <= 3 3 >= 2 3 >= 3 Be sure to use == for comparisons 2 = 2 // error! int v = 0; if (v = v) { // wrong! printf("true\n"); } else { printf("false\n"); } ------------------------------------------ II. Conditionals in C A. conditional expressions ------------------------------------------ CONDITIONAL EXPRESSIONS Purpose: to compute a value based on the program's state Syntax: ::= ? : Examples: (d != 0.0 ? n / d : 10.0) (n > 0 ? sqrt(n) : 0.0) Semantics: ------------------------------------------ ------------------------------------------ &&, || ARE SYNTACTIC SUGARS E1 && E2 ==> E1 || E2 ==> So "&&", "||" are short-circuit operators, the only evaluate their second argument if necessary Examples: d!=0 && n/d > 3.0 n<0 || sqrt(n) < 100.0 ------------------------------------------ How is this different than the Python conditional expressions? B. if-statements ------------------------------------------ IF-STATEMENTS Purpose: to control what actions are executed based on the program's state. Syntax: ::= ... | ::= ... | { } ::= | | Examples: if (i < len) { sum = sum + i; } if (i < len) { sum = sum + i } else { ; } if (num == 1) { return "one"; } else if (num == 2) { return "two"; } else if (num == 3) { return "three"; } else { return "more"; } Semantics: evaluates the first , ------------------------------------------ How is the if-statement in C different than the if-statement in Python? ------------------------------------------ FOR YOU TO DO Write a function int max(int x, int y) that returns the larger of the ints x and y passed as arguments, so that if after res = max(x,y) the following holds: res >= x and res >= y. ------------------------------------------ C. switch-statements ------------------------------------------ SWITCH STATEMENTS Purpose: Syntax: ::= ... | switch | case | break; | default ::= // known at compile time integer constant expressions can be: integer literals, e.g., 3, 9, 'c', sizeof expressions, e.g., sizeof(buf) a macro that is an integer constant, e.g., NULL, true ... ------------------------------------------ ------------------------------------------ SEMANTICS Semantics: case N: and default: are break; Usual case of a switch is something like: switch (E) { case N1: case N2: S12 break; case N3: S3 break; default: S4 } which is equivalent to: ------------------------------------------ ------------------------------------------ EXAMPLE switch (num) { case 1: res = "one"; break; case 2: res = "two"; break; case 3: res = "three"; break; default: res = "more"; break; } return res; ------------------------------------------ How does this relate to the if-statment we wrote earlier? 1. fall though of cases ------------------------------------------ BEWARE: CASES MAY FALL THROUGH! By default, code for a case may continue into the next case(s). What is the final value of res when num == 1? switch (num) { case 1: res = "one"; case 2: res = "two"; case 3: res = "three"; default: res = "more"; } Moral: Always use break; at the end of each case! Think of the syntax as: ::= ... | ::= { } ::= | ::= break; ::= case : | case : ::= default: break; | default: // at the end ------------------------------------------ 2. example ------------------------------------------ FOR YOU TO DO Using a switch statment, write a function: int parse_answer(char c) that returns 0 if c is 'n' or 'N' 1 if c is 'y' or 'Y' 2 otherwise ------------------------------------------