140 likes | 256 Views
Variables: Named Storage Locations. Variables must be defined or declared before they can be used so that appropriate memory storage can be allocated for them. Examples of declaration (or definition) statements int number_of_tests ; float test_average ;
E N D
Variables: Named Storage Locations Variables must be defined or declared before they can be used so that appropriate memory storage can be allocated for them. Examples of declaration (or definition) statements intnumber_of_tests; float test_average; int grade_test1, grade_test2, grade_test3; char letter_grade;
Assigning Values to Variables • = is the assignment operator • number_of_tests = 3; • letter_grade = ‘B’; • test_average = 88.56; • Type of literal assigned should match the type declaration/definition of the variable • If a floating point value is assigned to a variable of type ‘int’ only the integer part will be stored (fractional part is truncated NOT rounded) • Care should be taken to not exceed the maximum storage value of the data type Note: Until an assignment statement is executed, the contents of the storage area for a variable are garbage (unpredictable)!
Initializing Variables in the Definition When a variable is declared (defined), it can also be initialized with an assignment statement. You can also initialize it with an assignment statement that is not part of the declaration. Both examples below accomplish the same task. However, when possible, it is best to initialize a variable as close to where it is used in the source code. This practice is a matter of style and will be required in this course. Examples: intnumber_of_tests= 3; or intnumber_of_tests; number_of_tests = 3:
Arithmetic Operators Arithmetic expressions can be used in assignment statements. Below is a table of the arithmetic operators we will be using:
Evaluating Arithmetic ExpressionsPrecedence • Evaluate left to right (except unary negation) • ()contents of parenthesis are evaluated first • - when used as unary negation is next • *, / and % are then next in order of appearance • + and – are last (the – here is the binary subtraction operator) Examples: 4 + 3 * 2 = 10 (4 + 3) * 2 = 14 6 + 8 % 3 = 8
Exponents C++ has no exponent operators so a library function is required. A library in C++ and other languages is a group of predefined functions that perform usually common tasks. If you need to raise a value to the 5th power you could write: value_raised = value * value * value * value * value; Imagine doing this to raise a number to the 20th or even a higher power. Instead we use the pow(number, exponent) function. number and exponent are called arguments. value_raised= pow(value,5.0); Note: To use any predefined C++ mathematical function you must include the cmath header file. #include <cmath>
More on Assignment Multiple Assignment: Assign the same value to several variables in a single assignment statement (Do not use this feature in this class!) Example: The statements below initializes all 3 variables to zero int number1, number2, number3; number1 = number2 = number3 = 0; Combined Assignment: The variable name can appear on BOTH sides of the assignment operator. NOTE: The variable must have a valid value BEFORE this kind of assignment statement is used. Example: The statement below increments the number by 2 int number = 2; number = number + 2; Note: The resulting value stored in memory for number is now 4 These two kinds of assignment statements are very common in programming. However, as beginners, we will not use multiple assignment in this class.
Combined Assignment Operators Special operators are available in C++ to be used for combined assignment statements. In the examples below we will assume that Number has been initialized to 10 Operator Example Resulting Value of Number += number += 2; 12 -= number -= 2; 8 *= number *= 2; 20 /= number /= 2; 5 %= number %= 2; 0
Mixing Data Types in Arithmetic Expressions • Data Type Ranking – A data type out ranks another data type if it can hold a larger number • long double > double > float > unsigned long > long > unsigned int > int • Exception: if int and long are same size then unsigned int > long • char, shorts and unsigned shorts used in mathematical expressions are promoted to int • Exception: if short is same size as int then unsigned short is promoted to unsigned int • When an operator is used with two values of differing types then the lower ranking type is promoted to the same type as the higher ranking type • When the result of an expression evaluation is assigned to a variable it will be converted to the type of the variable
Integer Division If both operands of a division statement are integers the result will be an integer with any fractional component being discarded. In an expression the literal 18 is an integer but 18.0 is a floating point number. Be VERY careful and mindful when using mixed mode arithmetic.
Overflow and Underflow Based on the storage size of data types in any particular system there is a fixed range of values that can be stored in a variable of a give data types. Overflow: The assignment of a value LARGER than the storage capacity of the data type of the variable can hold Underflow: The assignment of a value SMALLER than the storage capacity of the data type of the variable can hold This is a danger when working at the limits of a data type’s storage capacity.
Type Casting: Control Type Promotion Manually static_cast<data type>(value) For example: Type casting can be used to avoid integer division int test1, test2, test3, test_sum; float test_average; … (input values for the three tests) test_sum = test1 + test2 + test3; test_average = static_cast<float>(test_sum) / 3;
Watch Out! int test1, test2; float avg; test1 = 100; test2 = 83; //What value does avg have after each of these //assignment statements are executed? avg = test1 + test2 / 2; avg = (test1 + test2) / 2; avg = static_cast<float>(test1 + test2) / 2; avg = static_cast<float>((test1 + test2) / 2); avg = (test1 + test2) / 2.0;