Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 3. Expressions and Interactivity Csc 125 Introduction to C++

Similar presentations


Presentation on theme: "Chapter 3. Expressions and Interactivity Csc 125 Introduction to C++"— Presentation transcript:

1 Chapter 3. Expressions and Interactivity Csc 125 Introduction to C++

2 Type Conversion Operations are performed between operands of the same type. Operations are performed between operands of the same type. If not of the same type, C++ will convert one to be the type of the other If not of the same type, C++ will convert one to be the type of the other This can impact the results of calculations. This can impact the results of calculations.

3 Data type ranking RankType#Byterange long double 10 ±3.4E-4932 and ±1.1E4832 double8 ±1.7E-308 and ±1.7E308 float4 ±3.4E-38 and ±3.4E38 unsigned long 8 0 to 18,446,744,073,709,551,616 long8 -9,223,372,036,854,775,808 to + unsigned int 4 0 to 4,294,967,295 int4 -2,147,483,648 to +2,147,483,648

4 Type coercion Type Coercion: automatic conversion of an operand to another data type Type Coercion: automatic conversion of an operand to another data type Promotion: convert to a higher type Promotion: convert to a higher type Demotion: convert to a lower type Demotion: convert to a lower type

5 Type coercion char, short, unsigned short automatically promoted to int char, short, unsigned short automatically promoted to int When operating on values of different data types, the lower one is promoted to the type of the higher one. When operating on values of different data types, the lower one is promoted to the type of the higher one. When using the = operator, the type of expression on right will be converted to type of variable on left When using the = operator, the type of expression on right will be converted to type of variable on left

6 Overflow and Underflow When a variable is assigned a value that is too large or too small in range for that variable’s data type, the variable overflows or underflows. When a variable is assigned a value that is too large or too small in range for that variable’s data type, the variable overflows or underflows. Variable contains value that is ‘wrapped around’ set of possible values Variable contains value that is ‘wrapped around’ set of possible values No warning or error message is given, if an overflow or underflow occurs, the program will use the incorrect number, and therefore produce incorrect results No warning or error message is given, if an overflow or underflow occurs, the program will use the incorrect number, and therefore produce incorrect results

7 Type casting Manual data type conversion Manual data type conversion The general format of a type cast expression The general format of a type cast expression –static_cast (Value) –Example double number=4.6; int val; val=static_cast (number)

8 Type casting int months, books; double perMonth; perMonth=static_cast<double>(books)/monthsperMonth=static_cast<double>(books/months)

9 Type casting (Another Example) void main() { int number=99; cout<<number<<endl;cout<<static_cast<char>(number)<<endl;}

10 Named constant For example, assume that the tuition rate is $128 per credit hour. Compute monthly payment for Peter’s tuition and display the amount he needs to pay. For example, assume that the tuition rate is $128 per credit hour. Compute monthly payment for Peter’s tuition and display the amount he needs to pay.

11 Named constant void main() { float tuition,payment; int creditHours; count<<“How many credit hours you\’ll take in spring? ”; cin>>creditHours;tuition=creditHours*128; cout<<“Monthly payment= ”<<tuition/5<<endl; count<<“How many credit hours you\’ll take in Fall? ”; cin>>creditHours;tuition=creditHours*128; cout<<“Monthly payment= ”<< tuition/4 <<endl; }

12 Named constant A named constant is really a variable whose content is ready-only, and cannot be changed while the program is running A named constant is really a variable whose content is ready-only, and cannot be changed while the program is running const double TUITION_RATE=128; const double TUITION_RATE=128;

13 Named constant void main() { float tuition,payment; int creditHours; const short tuitionRate=128; count<<“How many credit hours you\’ll take in spring? ”; cin>>creditHours;tuition=creditHours*?; cout<<“Monthly payment= ”<<tuition/?<<endl; count<<“How many credit hours you\’ll take in Fall? ”; cin>>creditHours;tuition=creditHours*?; cout<<“Monthly payment= ”<< tuition/? <<endl; }

14 Named constant To define common value that is difficult to remember, To define common value that is difficult to remember, const double PI=3.1415926; To indicate that array’s size. Here is an example To indicate that array’s size. Here is an example const int SIZE=21; char name[SIZE];

15 Multiple assignment Multiple assignment means to assign the same value to several variable with one statement. Multiple assignment means to assign the same value to several variable with one statement. –int a, b, c,d; –a=b=c=d=12;

16 Combined assignment operators Assume x=10; Assume x=10;x=x+4;x=x-2;x=x*10;x=x/5;x=x%4;

17 combined assignment operators C++ provides a special set of operators designed specifically for these jobs C++ provides a special set of operators designed specifically for these jobs Compound operators Compound operators OperatorExample Equivalent to += x +=5; x=x+5; -= y -=2; *=z*=10; /=a/=b; %=c%=3;

18 More examples result*=a+5;


Download ppt "Chapter 3. Expressions and Interactivity Csc 125 Introduction to C++"

Similar presentations


Ads by Google