Presentation is loading. Please wait.

Presentation is loading. Please wait.

CPS120: Introduction to Computer Science Operations Lecture 9.

Similar presentations


Presentation on theme: "CPS120: Introduction to Computer Science Operations Lecture 9."— Presentation transcript:

1 CPS120: Introduction to Computer Science Operations Lecture 9

2 The Assignment Operator The assignment operator is the equal symbol (=) The assignment operator changes the value of the variable to its left after evaluating the expression on its right For example: sum = 3 + 1000;  The variable sum ends up with the value 1003 salary = 40000; poundsPressure = 15 + 12; sum = original + 300; salary = salary + raise;

3 Multiple Assignments i = j = k = 10; // initializes all three variables to the value, 10 This statement is equivalent to: i = 10; j = 10; k = 10;

4 Common Arithmetic Operators + for addition - for subtraction * for multiplication / for division % for modulus (like finding the remainder of a division problem)

5 Compound Operators Example 1: j += 1; // is the same as j = j + 1 Example 2: total /= 2; // is the same as total = total / 2;

6 Increments and Decrement The incrementing (++) and decrementing (--) operators are useful at times if used carefully counter++; is equivalent to counter = counter + 1; and counter += 1; counter--; is equivalent to counter = counter - 1; and counter -= 1; Use the incrementing and decrementing operators with variables in statements such as these, that is with no other operators or code except the variable name that is being incremented or decremented.

7 Order of Operations Obey the order of operations Perform the following mathematical operations in this order: Parentheses, Exponentiation, Multiplication, Division, Addition and Subtraction Multiplication and division are of equal precedence, so you must work left to right within an algebraic expression The modulus operator (%) has the same precedence as * and / but is higher in precedence than + and -. Addition and subtraction work left to right within an algebraic expression as well Use parentheses if necessary to override the order of operations in order to produce the desired result

8 Promotion Promotion is supported in C++ to allow your expressions or statements to temporarily convert "smaller" data types to "larger" ones in order to provide an answer to a calculation Remember, promotion of a variable can occur when the expression is calculated, you are unable to store a floating point number in a space reserved for an integer, so the floating point number is truncated The wrong result can result

9 Typecasting Typecasting is the automatic conversion from one data type to another data type To typecast a variable, supply the name of the data type you want to use to interpret the variable, followed by the variable placed in parenthesis circumference = PI * (long double) (diameter);

10 Try to avoid overflow, which is a condition when an integer becomes too large for its data type You may not be able to tell from the output (without checking it carefully, at least) that this run-time error is occurring int total; total = 2147483647; total++; cout << total;  -2147483648 would display because 2147483647 is the maximum value that can be stored in a variable of the data type int The C++ constant INT_MAX can be used in a C++ program anywhere you wish the maximum possible value of an int to be used Overflow

11 Overflow Details Think of the legal range of the integer data type as being a number line that goes from - 2147483648 to 2147483647 When a value exceeds the right (positive) end of that number line, it "overflows" and wraps around and maps to a position on the left (negative) end of the number line It is possible to overflow the double and char data types as well.

12 Underflow a condition when a floating-point number becomes too small for its data type such as when a large negative exponent is used. double a = 1e-323; cout << a << endl; cout << a / 10 << endl; causes 0 to display for the second value due to underflow of the double data type.

13 Decision Making In Computers A circuit quite simply allows one out of two choices to be made depending on its inputs When decisions are made in a computer program, they are simply the result of a computation in which the final result is either TRUE or FALSE The value zero (0) is considered to be FALSE by C++. Any positive or negative value is considered to be TRUE

14 Using Relational Operators Relational operators provide the tools with which programs make decisions with true and false evaluations == equal to NOTE: this is two equals symbols next to each other, not to be confused with the assignment operator, = > greater than = greater than or equal to <= less than or equal to != not equal to

15 Using Logical Operators When complex decisions must be coded into an algorithm, it may be necessary to "chain together" a few relational expressions (that use relational operators) This is done with logical operators (also called Boolean operators.) && is the logical AND operator || is the logical OR operator ! is the logical NOT operator

16 Truth Tables Use this truth table to determine the results of the logical operators. In this table, 1 represents TRUE and 0 represents FALSE. Note that the ! symbol (the logical NOT operator) changes a TRUE to a FALSE.

17 Order of Logical Operations Logical operators may be mixed within evaluation statements but the following order of preference must be respected: 1. NOT operator (!) 2. AND operator (&&) 3. OR operator (||) Short-circuit evaluation in C++ allows the compiler to quickly evaluate a logical expression in some cases without having to completely evaluate each part of the expression

18 Complete order of operations The complete order of operations including all of the arithmetic, relational, and logical operators including all of the basic arithmetic, relational, & logical operators is: *, /, % +, -, =, ==, != ! && ||

19 Sample Evaluations - True The following control expressions have the resulting value of TRUE assuming that the integer variables a, b, & c have the values a = 1, b = 2, & c = 3. (a b || b > 0 && b > 1) (a > 0) (a) (-a) (a = 3)

20 Sample Evaluations: False While the following control expressions evaluate to FALSE where a = 1, b = 2, and c = 3: (a - 1) (!a) (0 * c) (c - a + b) Remember that the ! symbol (the logical NOT operator) changes a TRUE to a FALSE.

21


Download ppt "CPS120: Introduction to Computer Science Operations Lecture 9."

Similar presentations


Ads by Google