160 likes | 179 Views
Lecture 2: THE BASIC ELEMENTS OF C++ (Cont.) In this lecture, we continue explaining the basic elements of C++. Other Integer Types NOTE: the size of type int is system dependent. In contrast, types long and short have fixed sizes no matter what system is used. Type long
E N D
Lecture 2: THE BASIC ELEMENTS OF C++ (Cont.) In this lecture, we continue explaining the basic elements of C++.
Other Integer Types NOTE: the size of type int is system dependent. In contrast, types long and short have fixed sizes no matter what system is used. Type long Always occupies يشغل4 bytes (the same as type int on 32-bit Windows systems). Thus it has the same range from –2,147,483,648 to 2,147,483,647. It can be written as long int or long. EX: long int var1; // equivalent to: long var1;
Floating Point Types Represent numbers with a decimal place like 3.1415927, 0.0000625, and 10.2 They have both an integer part (to the left of the decimal point) and a fractional part (to the right). Used for measurable quantities such as distance, area, and temperature. The maximum number of significant digits is called the precision. that is, the number of decimal places. The precision in float values is six or seven. The maximum number of significant digits in values belonging to the double type is 15.
Floating Point Types (Cont.) 1. Type float Stores numbers in the range of about 3.4x1038 to 3.4x1038 with a precision of 7 digits. It occupies 4 bytes (32 bits) in memory. EX: float x; //Variable declaration statement //instructs the system to allocate //memory space and name it x Variable of type float in memory.
Floating Point Types (Cont.) 2. Type double Similar to float except that it requires more memory space and provide a wider range of values and more precision. Requires 8 bytes of storage and handles numbers in the range from 1.7x10–308 to 1.7x10308 with a precision of 15 digits. In C++, by default, floating-point numbers are considered of type double. Therefore, if you use the data type float, certain compilers might give you a warning message, such as “truncation from double to float”. To avoid such warning messages, you should use the double data type. Variable of type double
Floating Point Types (Cont.) The default type of floating-point numbers is double. Therefore, if you declare a named constant of type float, then you must specify that the value is of type float as follows: const float CONVERSION = 2.54f; Otherwise, the compiler will generate a warning message. Note that 2.54f says that it is a float value. Recall that the memory size for float values is four bytes and eight bytes for double values. Because memory size is of little concern these days, as indicated earlier, we can use the type double to work with floating-point values.
Example // circarea.cpp // demonstrates floating point variables #include <iostream> //for cout, etc. using namespace std; int main() { float rad; //variable of type float const float PI = 3.14159F; //type const float cout << “Enter the radius of a circle: ”; //prompt cin >> rad; //get radius float area = PI * rad * rad; //find area cout << “Area is: ” << area << endl; //display answer return 0; } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Here’s a sample interaction with the program: Enter the radius of a circle: 0.5 Area is 0.785398
Type bool Variables of type bool can have only two possible values: true and false. Is mostcommonly used toholdtheresultsofcomparisons. Isalpha less thanbeta? If so, a bool value is given the value true; if not, it’s given the value false. Summary
Variables Defined at Point of Use You can define variables throughout a program, not just at the beginning (defined before the first executable statement). But Variables that are used in many places in a function are better defined at the start of the function. Multiple Definitions Define and/or initialize many variables on one line, using the same data type keyword and separating the variable names with commas. EX:long pop1=2425785, pop2=47, pop3=9761;
Saving and Using the Value of an Expression Youcanusethevalueofavariableinalaterexpressionwithoutusingtheexpressionitself. To save the value of an expression and use it in a later expression, do the following: • Declare a variable of the appropriate data type. • Assign the value of the expression to this variable using the assignment statement. This saves the value of the expression into the new variable. • Wherever the value of the expression is needed, use the variable holding the value. EX: double degree1, degree2, degree3; cout<<"enter your 3 marks\n"; cin>>degree1>>degree2>>degree3; You should not do the following (although it is correct): cout<< "The undesired average is" << (degree1+degree2 +degree3)/3<<endl; The solution is: double total=0.0; double avg=0.0; total= degree1+degree2 +degree3; avg=total/3; cout << "The desired average is: " << avg<< endl;
Arithmetic Assignment Operators - Are one of several ways that C++ offers to shorten and clarify your code. - When adding something to an existing value (or performing some other arithmetic operation on it). The syntax: - Combines an arithmetic operator and an assignment operator and eliminates the repeated operand. There are arithmetic assignment operators corresponding to all the arithmetic operations: += -= *= /= %= EX: total = total + item; // adds item to total Will be: total += item; //without space between + and = otherwise will give error //adds item to total
Arithmetic Assignment Operators (Cont.) Arithmetic assignment operator
Example: // assign.cpp // demonstrates arithmetic assignment operators #include <iostream> using namespace std; int main() { int ans = 27; ans += 10; //same as: ans = ans + 10; cout << ans << “\t”; ans -= 7; //same as: ans = ans - 7; cout << ans << “\t”; ans *= 2; //same as: ans = ans * 2; cout << ans << “\t”; ans /= 3; //same as: ans = ans / 3; cout << ans << “\t”; ans %= 3; //same as: ans = ans % 3; cout << ans << “\t”; return 0; } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Here’s the output from this program: 37 30 60 20 2
Increment and Decrement Operators int count; count = count + 1; increments the value of count by 1. To execute this assignment statement, the computer first evaluates the expression on the right, which is count + 1. It then assigns this value to the variable on the left, which is count. These statements are used to keep track of how many times certain things have happened. To expedite the execution of such statements, C++ provides the increment operator, ++, which increases the value of a variable by 1, and the decrement operator, – –, which decreases the value of a variable by 1. Increment and decrement operators each have two forms: pre and post. The syntax is: • Pre-increment: ++count //increments the value of count by 1 • Post-increment: count++ //increments the value of count by 1 • Pre-decrement: – –count //decrements the value of count by 1 • Post-decrement: count– – //decrements the value of count by 1 Because both the increment and decrement operators are built into C++, the value of the variable is quickly incremented or decremented without having to use the form of an assignment statement.
What is the difference between the pre and post forms of these operators? Pre-increment If ++x is used in an expression, first the value of x is incremented by 1, and then the new value of x is used in the expression. Post-increment if x++ is used in an expression, first the current value of x is used in the expression, and then the value of x is incremented by 1. EX: x = 5; y = ++x; cout<<“the value of y is: ”<<y<<endl; //6 cout<<"the new value of x is: "<<x<<endl; //6 BUT x = 5; y = x++; cout<<“the value of y is: ”<<y<<endl; //5 cout<<"the new value of x is: "<<x<<endl; //6
Sample questions: 1. What is the difference between: • cout << “x+y”; and cout << x+y ; • cout<<3 + 4 * 5; and cout<<(3 + 4) * 5 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 3. What is the output of: a) #include <iostream> using namespace std; int main() { cout << 6 % 8 << endl << 7 % 8 << endl << 8 % 8 << endl << 9 % 8 << endl << 10 % 8 << endl; return 0; } //////////////////////////////////////////////////////////////////////////////////////////////// b) cout << 3 / 2 + 5.5;