310 likes | 427 Views
ECE 1305 Introduction to Engineering and Computer Programming. Section 05 Variables, Data Types and Operators. Variables. Variables must be named with a valid identifier . Must start with a letter or an underscore Must contain only letters, digits, or the underscore. C++ is case sensitive
E N D
ECE 1305Introduction to Engineering and Computer Programming Section 05Variables, Data Types and Operators
Variables • Variables must be named with a valid identifier. • Must start with a letter or an underscore • Must contain only letters, digits, or the underscore. • C++ is case sensitive • myvariable and Myvariable are two different identifiers.
Identifiers • It is common C++ practice to: • name functions with all lowercase letters interest() • name variables with initial lowercase letters the uppercase letters to separate words variableInterestRate; • name constants with all uppercase INTEREST_RATE;
Variable Declarations • All variables must be explicitly declared. • This forces the programmer to design the program before coding. • A variable declaration has the form Type_Name Variable_Name; • Examples int numberOfBeans double weightOfBeans
Variable Declarations • Variable declarations do two things: • Name the variable • Instructs the compiler to set aside memory space for the variable • Different variable types have different memory requirements. • Operators may perform differently depending on the data type.
Data Types • All numbers are not created equal. Different types of numbers have different memory requirements. • Functions and operators act on specific types of numbers and return results of a specific number type. • Only numbers in a certain range can be represented in a computer. A calculation may result in an overflow error.
short 15 bits ( – 32,767 ... 32,767 ) Sign (1 bit) unsigned short 16 bits ( 0 ... 65,535 ) 31 bits int ( – 2,147,483,647 ... 2,147,483,647 ) Sign (1 bit) 32 bits unsigned long ( 0 ... 4,294,967,295 ) Integer Types
Floating Point Representation • An non-integer number may be approximated by the form mantissa 2 exponent • For example 0.11001001 2 00000101 which equals 0.78515625 2 5 = 25.125 in Decimal • All floating point numbers stored in a computer memory are only approximations.
float (Single Precision) 8 bits 23 bits exponent fraction Sign (1 bit) ( 10 –37 ... 10 38 with 7 significant digits of accuracy ) Sign (1 bit) 11 bits 20 bits double (Double Precision) exponent fraction 32 bits fraction ( 10 –307 ... 10 308 with 15 significant digits of accuracy ) Floating Point Types
The Assignment Operator • The assignment operator assigns a value to a variable. • The expression on the right-hand side of the equal sign is evaluated. The resulting value is assigned to the memory location identified on the left-hand side of the equal sign.
The Assignment Operator • Example: double temperature; temperature = 98.6; double distance, rate, time; rate = 100.0; time = 3.2; distance = rate * time;
The Assignment Operator • The assignment operator is not the same as the mathematical equality sign. int n; n = n + 1; • new_value = old_value + increment
Uninitialized Variables • When a variable is declared it is not assigned a value. The programmer cannot assume it starts with a value of zero! • The memory location may hold garbage. • Variables may be assigned a value when they are declared. int numberOfBeans = 6237; double weightOfBeans = 25.87;
Basic Mathematical Operators * multiplication / division % remainder (modulo division) + addition - subtraction = assignment
Arithmetic Operator Precedence • Mathematical Operations are performed with the following precedence. Parentheses used to group expressions (...) Unary + , Unary – Multiplication * , Division / , Modulo Division % Binary + , Binary – Assignment = • Binary Operators of equal precedence are evaluated from left – to – right.
Priority of Operators double answer = 4.8 / -2.0 + (8.5 – 12.0 * 0.5) * 10.0; 4.8 / -2.0 + (8.5 – 12.0 * 0.5) * 10.0 4.8 / -2.0 + (8.5 – 12.0 * 0.5) * 10.0 4.8 / -2.0 + (8.5 – 6.0) * 10.0 4.8 / -2.0 + 2.5 * 10 4.8 / -2.0 + 2.5 * 10 -2.4 + 25 Results in the variableanswer assigned the value 22.6
Combined Operators • C++ allows the assignment operator to be combined with other mathematical operators.
Integer Division int a = 38; int b = 7; int z = 38/7; Results in the variable z assigned the value 5 int a = 38; int b = 7; int z = 38%7; Results in the variable z assigned the value 3
Automatic Conversion of Data Types int k = 5, m = 4, n; double x = 1.5, y = 2.1, z; z = k + x; k is promoted to a double and added to x z is assigned the value 6.5 n = k + x; k is promoted to a double and added to x n is assigned the value 6, the assignment operator converts the number to int and the fraction is lost
Automatic Conversion of Data Types int k = 5, m = 4, n; double x = 1.5, y = 2.1, z; z = m * y; m is promoted to a double and multiplied with y The variable z is assigned the value 8.4 n = x * y; The variable n assigned the value 3 The assignment operator converts the number to int and the fraction is lost.
Explicit Conversion of Data Types int k = 5, m = 4, n; double z; z = k / m; The variable z assigned the value 1.0
Data Type Conversion • An expression my be converted from one data type to another. int k = 5, m = 4, n; double z; z = double(k) / double(m); k and m are converted to double before the division. The variable z is assigned the value 1.25
Rounding Numbers cout << int(15.6) << endl; 15 cout << int(15.6 + 0.5) << endl; 16 cout << int(15.49 + 0.5) << endl; 15
Type Casting • Although the method of type conversion shown above seems simple and effective, the “preferred” method uses the form static_cast<Type>(Expression) • Example double x = 7.2; int ans = 21/static_cast<int>(x)
Other Data Types • Single characters may be stored as type char. • Text may be stored as type string. If strings are used in a program, the C++ file must have the preprocessor directive #include<string> • Logical values (true and false) may be stored as type bool (for Boolean).
Increment Operators • The increment operator, ++, adds one to the value of an expression. • The effect of the operator is different depending on the location of the operator. int n = 2; int m = 2*(n++); • has the same effect as int n = 2; int m = 2*n; n = n + 1;
Increment Operators • The effect of the operator is different depending on the location of the operator. int n = 2; int m = 2*(++n); • has the same effect as int n = 2; n = n + 1; int m = 2*n;
Decrement Operators • The increment operator, --, subtracts one to the value of an expression. • The effect of the operator is different depending on the location of the operator. int n = 2; int m = 2*(n--); • has the same effect as int n = 2; int m = 2*n; n = n - 1;
Decrement Operators • The effect of the operator is different depending on the location of the operator. int n = 2; int m = 2*(--n); • has the same effect as int n = 2; n = n - 1; int m = 2*n;
Types of Errors • Syntax Errors • A violation of the rules of the programming language. • Generally detected by the C++ compiler. • Run-time Errors • An attempt to perform an illegal operation like divide by zero. • Error message at run-time, or error detected by debugging software. • Logic Errors • Computer does exactly what it is told to, unfortunately it is not solving the intended problem. • Programmer must check the result each step in the process until the error is identified.