Download Important Operators - Lecture Slides | CMSC 106 and more Study notes Computer Science in PDF only on Docsity!
- CMSC 106Lecture Set #
- Set Started:Thursday, September 20,
“boolean” statements^ ^ values possible: true and false^ ^ never both and never neither^ ^ Does not exist as a type in C^ ^ many people use symbolic constants todefine them so they look like they exist^ ^ In C^ ^ 0 is false^ ^ any other value is true
Operator Precedence
right to left =, +=, -=, *=, /=
left to right ||
left to right &&
left to right ==, !=
left to right <, <=, >, >=
left to right +, -
left to right *, /, %
right to left !, unary -, ++, --
Associativity
Operator * note – the unary increment and decrement operators have high precedenceeven when used as postfix but the time of operation is still after others
The "if" statement conditional execution of the statement if (condition)statement; one statement^
---- notice:^ no ; after the (condition)
^ indentation not needed for compiler – needed for people ^ Process:^ ^ condition is tested^ ^ execution continues based on the truth value of the condition^ ^ if true subsidiary statement is executed^ ^ if false subsidiary statement is skipped^ ^ in both cases execution continues with next statement (afterentire if statement)
Beware of assignmentoperator #include <stdio.h>/* reads two ints – praises for followingdirections(?)*/main(){ int x, y;printf(“type the same value twice:”);scanf("%d %d", &x, &y);if (x = y)printf(“Good Job\n”);printf(“We are done here\n”);return 0;} Read carefully – does it really do what it says?
The if/else statement The if/else contains two subsidiary statements; one is alwaysexecuted. if (condition)statement1;elsestatement2; still considered “one statement” but it has 2 subsidiary statements Process: ^ condition is tested ^ execution continues based on the truth value of the condition^ ^ if true subsidiary statement 1 is executed^ ^ if false subsidiary statement 2 is executed ^ in both cases execution continues with next statement (after entire ifstatement)
Nested if statements int month, day;scanf("%d", &day);if (day > 31)if (day <= 60)printf("February\n");--------------------int month, day;scanf("%d", &day);if (day <= 31)month= 1;elseif (day <= 60)month= 2;
Can be inside of a Block or not^ int month, day;scanf("%d", &day);if (day <= 90){ printf(“first third of year\n”);if (day <= 60)if (day <= 31)month= 1;elsemonth=2;elsemonth = 3;}
The Conditional Expression C's only ternary operator condition? expression1 : expression2 if condition is true expression1's value iscalculated and becomes the whole conditional expression'svalue otherwise its value is expression2's value
The Logical Operators^ ^ &&^ (and)
(binary operator)
^ ||^ (or)
(binary operator)
^!^ (not)
(unary, prefix)
^ produce values of 0 and 1 ^ Truth Tables are a good way to showwhat they mean.
Short-circuit Evaluation of Logical Operators^ ^ Once the value of an expression can bedetermined – C stops the evaluation ofthat expression^ ^ with && - if the left operand is false,the whole statement must be false^ ^ with || - if the left operand is true, thewhole statement must be true
Common Mistakes^ ^ Forgetting that relational operators areonly binary operators^ ^ Assuming the && or || can do morethan it can^ ^ Assuming the! has lower precedencethan it does