Top Banner

of 29

Chapter 3.pptx

Nov 04, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript

Chapter 3 Arithmetic in C

Chapter 3Arithmetic in COperatorsOperators tell the computer how toprocess the data. It connects data in the expression andequationTypes of operators used in calculationand problem solving include Mathematical Relational LogicalOperators continue.Two concepts related to operator are Operand Resultant Operands are the data being process Resultant is the answer that result when theoperation is complete.Example 5 + 7 = 12Operands are 5 and 7 Operator is + Resultant is 12 Operands can be constant or variable Data types of resultant and operands will depend on the operatorTypes of OperatorMathematical operators Addition (+) Subtraction (-) Multiplication (*)Division (/)Modulo division (%) FunctionsTypes of OperatorRelational operators Equal to (==) Less than (< ) Greater than (>) Less than or Equal to (=)The resultant of relational operation is logicaldata type i.e. TRUE or FALSE The next set of actions will depend on theresult of the relational expressionE.g. Balance > 500, if the expression TRUEwithdraw 100, if the expression FALSEwithdraw 50Types of OperatorsLogical Operator OR (||) AND (&&) Not (!)Use to connect relational expression (decision-making expression) To perform operation on logical dataExample of Operator

Operator Hierarchy (Precedence)

EXPRESSIONSIt is a sequence of operands and operators that reduce to a single value.Operator : It is a symbolic token that represents an action to betaken.Ex: * is an multiplication operator.Operand: An operand receives operators action.Ex: A+ B In this A and B are operands and + is an operator.Note: Expressions always evaluate to a single value.There is no limit to the number of operator and operand sets in an expression

Ex: 5.0/9.0*(fah 32.0)Setting up Numerical ExpressionNormal equation X(3Y+4) - 4Y X+6 Appropriate Computer Equation X* (3*Y+4)-4*Y/(X+6)Evaluating Mathematical ExpressionConsider 5*(X+Y)-4*Y/(Z+6) Value of X = 2, Y=3, Z=6 Evaliation 5 * (X + Y) 4 * Y / (Z+6)

Evaluation Relational Expression

Getting Started With C

Preprocessor directivesAn instruction to pre-processor Standard library header (p154,Deitel) E.g. #include for std input/output #include Conversion number-text vise-versa, memory allocation, random numbers #include string processingTypes of operatorsTypes of operators are: Arithmetic operators (+ , - , * , / , %) Relational operators (> , < , == , >= , , < >=, 10) e.g2 while (b != 10) e.g3 if(kod == 1) print(Pegawai); Reminder: Dont confuse == (relational op.) with = (assignment op.)More on logical operatorsLogical operators are manipulation of logice.g1 let say b=8, c=10,if ((b > 10) && (c 10))e.g3 if ((kod == 1) && (salary > 2213))Truth table for &&(logical AND) operator

Truth table for ||(logical or) operator

Compound assignment operatorTo calculate value from expression and store it in variable, we use assignment operator (=) Compound assignment operator combine binary operator with assignment operatorE.g. val +=one; is equivalent to val = val + one;E.g. count = count -1; is equivalent tocount -=1;count--;--count;Unary OperatorsObviously operating on ONE operand Commonly used unary operators Increment/decrement { ++ , -- } Arithmetic Negation { - }Logical Negation { ! }Usually using prefix notationIncrement/decrement can be both a prefix and postfixUnary Operators (Eg.)

Operator Precedence & symbol in Clanguage

Formatted Output with printfTo display result of the program can bedone by using keyword printf andoperator as shown below: printf(formating,variable) ;

//this is variable declarationint a, b; //Line 1char grade; //Line 2//examples of input and output statements a = 25; grade = A; //Line3printf(a = ,a) ; //Line 4printf(Enter two integers: ) ; //Line 5scanf (%d%d,&a,&b) ;printf(The numbers you entered are %d %d,a,b); //Line 6printf(Your grade is %c ,grade); //Line 10END OF CHAPTER