Download presentation
Presentation is loading. Please wait.
Published byPhoebe Aubrie Pearson Modified over 8 years ago
1
Logical Operator It is useful to test multiple conditions OperatorANDORNOT Symbol&&||! ConditionBOTHEITHERREVERSE
2
int stupid = 1, lazy = 1; if ((!stupid) && (!lazy)) printf(“You have bright future\n”); if ((stupid) && (!lazy)) printf(“You still have future\n”); if ((!stupid) && lazy) printf(“We shall see….\n”); if (stupid && lazy) printf(“Future in trouble!!\n”); Logical Operator Guess what is the output… Output Future in trouble!!
3
int study=0, good_mood, entertainment, tired, no_time; printf(“I will study soon, please wait….\n”); while (!study) { if (!good_mood) entertainment = 1; else tired = 1; if (entertainment) no_time = 1; if (!no_time && !tired) study = 1; } printf(“OK, I am studying now.”); Logical Operator Guess what is the output… Output I will study soon, please wait….
4
int score; char grade; printf(“Please enter your score: ”); scanf(“%d”, &score); if ((score 74) grade = ‘A’; if ((score 59) grade = ‘B’; if ((score 39) grade = ‘C’; if ((score 0) grade = ‘F’; printf(“The grade for %d is %c”, score, grade); Logical Operator Application example: what’s the output if the user input 45 Output Please enter your score: 45 The grade for 45 is C
5
int year, leap=0; printf(“Please enter a year: ”); scanf(“%d”, &year); if (((year%4)==0) && ((year%100)!=0)) leap = 1; if (((year%400)==0) && ((year%100)==0)) leap = 1; printf(“%d is %s a leap year”, year, (leap) ? “” : “not”); Logical Operator Application example: What’s the output if the user enter 2000 Output 2000 is a leap year
6
Arithmetic Operators (Revisit) There are 2 types of arithmetic operators in C: unary operators and binary operators. Unary operators are operators that require only one operand. Binary operators are operators that require two operands.
7
Unary Opearators: C Operation OperatorExample Positive +a = +3 Negative- b = -a Increment++i++ Decrement--i-- i++ is equivalent to i = i + 1 i-- is equivalent to i = i-1
8
It is also possible to use ++i and --i instead of i++ and i--. However, the two forms have a slightly yet important difference. Consider this example: int a = 9; printf(“%d\n”, a++); printf(“%d”, a); The output would be: 9 10
9
But if we have: int a = 9; printf(“%d\n”, ++a); printf(“%d”, a); The output would be: 10 a++ would return the current value of a and then increment the value of a. ++a on the other hand increment the value of a before returning the value.
10
Binary Operators: C Operation Operator Example Addition+ a+3 Subtraction- a - 6 Multiplication* a*b Division/ a/c Modulus% a%x The division of variables of type int will always produce a variable of type int as the result.
11
Assignment Operators Assignment operators are used to combine the “=” operator with one of the binary arithmetic operators. OperatorExampleEquivalent Statement += c += 7c = c + 7 -= c -= 8c = c - 8 *= c *= 10c = c * 10 /= c /= 5c = c / 5 %= c %= 5c = c % 5
12
Precedence Rules Precedence rules come into play when there is a mixed of arithmetic operators in one statement. For example: x = 3 * a - ++b%3; The rules specify which of the operators will be evaluated first. Precedence Level Operator Associativity 1 (highest) unary+ unary -right to left ++ --right to left 2 * / % left to right 3 + -left to right 4 (lowest) = += -= *= /= %= right to left
13
If we intend to have the statement evaluated differently from the way specified by the precedence rules, we need to specify it using parentheses ( ). For example: x = 3 * ((a - ++b)%3); The expression inside a parentheses will be evaluated first. The inner parentheses will be evaluated earlier compared to the outer parentheses.
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.