DOC PREVIEW
CSUN COMP 106 - Operators and Expressions

This preview shows page 1 out of 4 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Operators and expressions February 9, 20061Operators and Expressions Operators and Expressions Larry CarettoComputer Science 106Computing in Engineering and ScienceFebruary 9, 20062Outline• Review last class• Assignment operator• Definition of expressions• Mathematical operators• Operator precedence• The remainder or mod (%) operator3True-False Review Quiz• You do not have to declare a variable before using it in a program• Declaring a variable is the same as assigning it an initial value• You can declare a variable and assign it a value at the same time• Subtracting one from the minimum short integer gives the maximum shortFalseTrueFalseTrue4Another Review Quiz• A program has the following declarationsint k = 10, r = 58, j = 9;• What is the result of the following divisions?r/kj/kr/jr/k = 58/10 = 5 after truncationj/k = 9/10 = 0 after truncationr/j = 58/9 = 6 after truncation5Assignment Operator• Assignment operator is =<variable>= <expression>;• An expression is a variable, a constant, or a combination of variables, constants, and operators• Remember syntax for commands– text entered as printed–<description of item>6Assignment Operator II•<variable>= <expression>;assigns value of expression to variable– myVar = 3; // assigns 3 to myVar– var2 = myVar ; // assigns 3 to var2– set = 10 + var2; // assigns 13 to set• Can have multiple assignments– x = y = z = 0; sets x, y, and z to zero• Look at C++ operators and the rules for their application: operator precedenceOperators and expressions February 9, 200627Operators• Different types of operators– Arithmetic operators give usual arithmetic operations ( + – * / ) and mod (%)– Relational operators give true or false results ( > < >= <= == != )– Logical operators and(&&) or(||) not(!)• Operator precedence: rules for which operators are evaluated first in an expression with more than one operatorx = -yx = y - z8Example of Precedence• What is 4 + 2 * 3?•– Depends on which operator (* or +) has precedence (is done first)– No fixed mathematical rules– Simple calculators often do expressions as entered giving (4 + 2 = 6) * 3 = 18– Programming languages (and scientific calculators) usually give precedence to multiplication giving 4 + (2 * 3 = 6) = 109Arithmetic Operator Precedence• Expressions scanned from left to right• Operators with highest precedence are executed first• Scan is repeated to execute operators with decreasing level of precedence• Highest level is unary minus (-x)• Next is multiply, divide and mod (%)• Lowest is addition and subtraction10The mod Operator• Operator % gives remainder• Like long division before you learned about fractions and decimals• 3 goes into 7 two times with a remainder of 1• 7 % 3 = 1 (7/3 = 2 remainder 1)• What is 3 % 4?• What does it mean if N % 2 is zero?3 (3/4 = 0 remainder 3)N is an even number11Raising Numbers to a Power• C++ does not have an exponentiation operator to evaluate xn• Use the pow function for this purpose – Requires #include <cmath>– double result = pow(number, power);– What is pow(3, 4)?– What is pow(4, 3)?– Note importance of order in function• Can use multiplication, e.g. x * x for x2pow(3, 4) = 34= 81pow(4, 3) = 43= 6412Overcoming Precedence• Use parentheses to group operations to give desired results.• Write code for these equationsyxvuw++=• w = ( u + v ) / ( x + y );cayxvbuaw+++=• What does u + v / x + y; give?• w = ( a * u / b + v ) / ( x + ( y + a ) / c );• Can use extra parenthesesyxvu ++Operators and expressions February 9, 2006313Some Questions• Write C++ expressions for the following:yxwyzxu32++=bayxryzxu−++=u = (x + y * z * z)/(w + x / (3 * y) );u = (x / y / z + r) / (x + y – a / b );14Type Conversion• Can equate variable of one type to value of another type– Internal conversion rules apply– Assigning double variable an int value converts data and adds decimal point– Assigning int variable a double value loses decimal fraction– Declare: int x = 3; double y = 4.8;– What is result of x = y? of y = x?x = 4 y = 3.015Type Conversion II• Type precedence for data types– See text for complete list– double is higher than int• When two unlike data types are used in an operation the lower type is promoted to the higher type before the operation– For int x = 3; double y = 2;– What is result of x / 2? of x / y? of x / 2.0?x/2 is 1x/y is 1.5 x/2.0 is 1.5What is difference between 2 and 2.0?16Type Conversion III• Be careful in order of operations• What are results of following examples?double m = 10, V = 20;double KE = (1/2) * m * V * V;double KE = m * V * V / 2;double KE = 0.5 * m * V * V;double KE = 1 / 2 * m * V * V;double KE = 1 / 2.0 * m * V * V;0200020000200017Type Conversion Functions• static_cast<double>( <expression> ) converts value of <expression> to type double• static_cast<int>( <expression> ) converts value of <expression> to type int• Although the use of static_cast is the preferred operation in modern C++ an older method is simpler18Type Conversion Functions• double( <expression>) converts value of <expression>to type double• int( <expression>) converts value of <expression>to type int• Example: to find average of type int data: int sum, number; double average = double(sum)/double(number);– Do we need both doubles in this statement?No converting one to double will promote the otherOperators and expressions February 9, 2006419More on Type Promotion• Recall kinetic energy exampledouble KE = (1/2) * m * V * V;• Result depends only on the two operands currently being considered– Does not depend on other variables in the expression– Does not depend on left-hand-side variable• What does double x = 1/2; give for x?It gives 0.0 for x20Questions• What values are stored in the variables by the following statements? (27/4 = 6.75)double a;int x = 27, y = 4, z;z = x / y;a = x / y;a = double( x ) / y;z = double( x ) / y;z = 6a = 6.0a = 6.75z = 621More Questions• What values are stored in the variables by the following statements?int a; double x = 12, y =5, z = 3.3, w;w = x / y;a = x / y;w = int( z / y);a = x / int( z );w = 2.4a = 2w = 0a =


View Full Document

CSUN COMP 106 - Operators and Expressions

Download Operators and Expressions
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Operators and Expressions and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Operators and Expressions 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?