Whoops! There was a problem loading more pages. Retrying... Whoops! There was a problem previewing this document. Retryi
Numeric Data Types, Operations and Functions By Kondwani Makanda
Learning outcomes By the end of this presentation you should be able to understand the following:
• • • • • • • • •
Assignment statements and assignment operator Named constants Numerical Datatypes and Operators Numerical Literals Compound Assignment Operators Increment and Decrement Operators Relational and Comparison Operators Logical Operators Functions
Assignment statement and assignment expression • An assignment statement designates a value for a variable
Named Constants or constants • is an identifier that represents a permanent value (values that will not change) • e.g. PI or π
• use the const keyword • The syntax of using const is • const datatype CONSTANTNAME/ IDENTIFIER = value; • const double PI = 3.14159; • Constants are normally in UPPERCASE
Why use constants? • There are three benefits of using constants: • you don’t have to repeatedly type the same value; • if you have to change the constant value (e.g, from 3.14 to 3.14159 for PI), you only change it at a single location in the source code; • descriptive constant names make the program easy to read.
Numerical datatypes and operators • Numeric Types • Every data type has a range of values • The compiler allocates memory space for each variable/constant according to its data type • C++ provides primitive data types for numeric values, characters, and Boolean values. • C++ uses three types of integers: short, int, and long. • Each integer type comes in two flavours: signed and unsigned • Half of the numbers represented by a signed int are negative and the other half are non-negative • All the numbers represented by an unsigned int are non-negative
Numerical datatypes and operators CONT’D • Numeric Types • C++ uses three types for floating-point numbers: float, double and long double. • The double type is usually twice as big as float • the double is known as double precision • float is single precision • The long double is even bigger than double • For most applications using double type is desirable
Type
Typical Bit Width
Typical Range
char
1byte
-127 to 127 or 0 to 255
unsigned char signed char int
1byte 1byte 4bytes
0 to 255 -127 to 127 -2147483648 to 2147483647
unsigned int signed int
4bytes 4bytes
0 to 4294967295 -2147483648 to 2147483647
short int unsigned short int signed short int long int
2bytes Range Range 4bytes
-32768 to 32767 0 to 65,535 -32768 to 32767 -2,147,483,648 to 2,147,483,647
signed long int unsigned long int float
4bytes 4bytes 4bytes
same as long int 0 to 4,294,967,295 +/- 3.4e +/- 38 (~7 digits)
double
8bytes
+/- 1.7e +/- 308 (~15 digits)
long double
8bytes
+/- 1.7e +/- 308 (~15 digits)
wchar_t
2 or 4 bytes
1 wide character
Numerical datatypes and operators CONT’D • Numeric Types • C++ defines constants INT_MIN, INT_MAX, LONG_MIN, LONG_MAX, FLT_MIN, FLT_MAX, DBL_MIN, and DBL_MAX in the header file • The constants are useful in programming • You can know the values of these constants by: • cout = 6) is true (b+4 > a*c) // evaluates to false, since (3+4 > 2*6) is false ((b=2) == a) // evaluates to true
Logical operators (!, &&, ||) • The operator ! is the C++ operator for the Boolean operation NOT • It has one operand to its right and inverts it, producing false if its operand is true, and true if its operand is false
!(5 == 5) // evaluates to false because the expression at its right (5 == 5) is true !(6 6) ) // evaluates to true (true || false)
• In using logical operators, C++ evaluates what is necessary from left to right to come up with the combined relational result, ignoring the rest. • Therefore, in the last example ((5==5)||(3>6)), C++ evaluates first whether 5==5 is true, and if so, it never checks whether 3>6 is true or not. • This is known as short-circuit evaluation Operator && ||
Short-circuit if the left-hand side expression is false, the combined result is false (the right-hand side expression is never evaluated) if the left-hand side expression is true, the combined result is true (the right-hand side expression is never evaluated)
Bit-wise operators
Operator & | ^
asm equivalent AND OR XOR
~
NOT
>
SHL SHR
Description Bitwise AND Bitwise inclusive OR Bitwise exclusive OR Unary complement inversion) Shift bits left Shift bits right
(bit
functions • Functions are blocks of code that perform a number of pre-defined commands to accomplish something productive • C++ functions come in two varieties: • those with return values and those without them.
• An example of function that has return value • sqrt(); • x = sqrt(6.25);
• The expression sqrt(6.25) is termed a function call, the invoked function is termed the called function • The value in the parentheses (6.25) is information that is sent to the function; it is said to be passed to the function • A value that is sent to a function this way is called an argument or parameter
• The sqrt() function calculates the answer to be 2.5 and sends that value back to the calling function; the value sent back is termed the return value of the function
Functions cont’d • Before the C++ compiler uses a function, it must know what kind of arguments the function uses and what kind of return value it has • That is, does the function return an integer? a character? a number with a decimal fraction? or something else? • If it lacks this information, the compiler won’t know how to interpret the return value • The C++ way to convey this information is to use a function prototype statement
• Please note that a C++ program should provide a prototype for each function
used in the program.
• A function prototype does for functions what a variable declaration does for variables: It tells the compiler what types are involved • double sqrt (double);
// function prototype
Functions variations • Some functions require more than one item of information • These functions use multiple arguments separated by commas • For example, the math function pow() takes two arguments and returns a value equal to the first argument raised to the power given by the second argument. It has this prototype: • double pow(double, double); // prototype of a function with two arguments • e.g. answer = pow(5.0, 8.0); // function call with a list of arguments
• Other functions take no arguments • Its prototype looks like this • int rand(void); // prototype of a function that takes no arguments • myGuess = rand(); // function call with no arguments, implicit declaration that there are no arguments
Functions variations cont’d • There are also functions that have no return value • this function sends a value to the screen instead of to the calling program, it doesn’t require a return value • This is indicated using the keyword void for the return type • e.g. void bucks(double); // prototype for function with no
return value
User-defined functions #include using namespace std; void simon (int);
// function prototype for simon()
int main() Simon says touch your toes 3 times. { Pick an integer: 512 simon(3); // call the simon () function Simon says touch your toes 512 times. cout > count; simon(count); // call it again cout