Elementary Programming with C - Session 3: Operators and Expressions

Explain Assignment Operator Understand Arithmetic Expressions Explain Relational and Logical Operators Understand Bitwise logical operators and expressions Explain casts Understand Precedence of Operators Expressions Combination of Operators and Operands

ppt25 trang | Chia sẻ: candy98 | Lượt xem: 374 | Lượt tải: 0download
Bạn đang xem trước 20 trang tài liệu Elementary Programming with C - Session 3: Operators and Expressions, để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
Operators and ExpressionsSession 31Operators and ExpressionObjectivesExplain Assignment OperatorUnderstand Arithmetic ExpressionsExplain Relational and Logical OperatorsUnderstand Bitwise logical operators and expressionsExplain castsUnderstand Precedence of OperatorsOperators and Expression Expressions Combination of Operators and OperandsExample 2 * y + 5OperandsOperatorsOperators and ExpressionThe Assignment Operatorvariable_name = expression;lvaluervalueAssignmentoperator The assignment operator(=) can be used with any valid C expression (Left value)(Right value)Operators and ExpressionMultiple Assignment Many variables can be assigned the same value in a single statementa = b = c = 10;However, you cannot do this :int a = int b = int b = int c = 10Operators and ExpressionOperators 4 TypesArithmeticRelationalLogicalBitwiseOperators and ExpressionArithmetic ExpressionsMathematical expressions can be expressed in C using arithmetic operators Examplesa * (b + c/d)22++i % 75 + (c = 3 + 8)Elementary Programming with C/Session 3/ 7 of 25Relational & Logical Operators-1Test the relationship between two variables, or between a variable and a constantUsed toRelational OperatorsElementary Programming with C/Session 3/ 8 of 25Logical operators are symbols that are used to combine or negate expressions containing relational operatorsRelational & Logical Operators-2Expressions that use logical operators return zero for false, and 1 for trueExample: if (a>10) && (a 3 AND 3 3 AND 3 3 AND 33 AND 33] AND [3 10 AND (2+2^4-8/4 > 6 OR (211)) The solution is: 1. 5+9*3^2-4 > 10 AND (2+2^4-8/4 > 6 OR (True AND False)) The inner parenthesis takes precedence over all other operators and the evaluation within this is as per the regular conventions 2. 5+9*3^2-4 > 10 AND (2+2^4-8/4 > 6 OR False)Changing Precedence-2Elementary Programming with C/Session 3/ 23 of 253. 5+9*3^2-4 >10 AND (2+16-8/4 > 6 OR False) Next the outer parentheses is evaluated 4. 5+9*3^2-4 > 10 AND (2+16-2 > 6 OR False) 5. 5+9*3^2-4 > 10 AND (18-2 > 6 OR False) 6. 5+9*3^2-4 > 10 AND (16 > 6 OR False)7. 5+9*3^2-4 > 10 AND (True OR False)8. 5+9*3^2-4 > 10 AND TrueChanging Precedence-3Elementary Programming with C/Session 3/ 24 of 259. 5+9*9-4>10 AND True The expression to the left is evaluated as per the conventions10. 5+81-4>10 AND True11. 86-4>10 AND True12. 82>10 AND True13. True AND True14. TrueChanging Precedence-4Elementary Programming with C/Session 3/ 25 of 25
Tài liệu liên quan