chapter 3. expressions and interactivity csc 125 introduction to c++

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

Upload: clifton-parsons

Post on 17-Dec-2015

217 views

Category:

Documents


1 download

TRANSCRIPT

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

Chapter 3. Expressions Chapter 3. Expressions and Interactivityand Interactivity

Csc 125 Introduction to C++Csc 125 Introduction to C++

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

Type ConversionType Conversion

• Operations are performed between Operations are performed between operands of the same type.operands of the same type.

• If not of the same type, C++ will If not of the same type, C++ will convert one to be the type of the convert one to be the type of the otherother

• This can impact the results of This can impact the results of calculations.calculations.

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

Data type rankingData type ranking

RanRankk

TypeType #Byt#Bytee

rangerange

long doublelong double 1010 ±3.4E-4932 and ±1.1E4832 ±3.4E-4932 and ±1.1E4832

doubledouble 88 ±1.7E-308 and ±1.7E308 ±1.7E-308 and ±1.7E308

floatfloat 44 ±3.4E-38 and ±3.4E38 ±3.4E-38 and ±3.4E38

unsigned unsigned longlong

88 0 to 0 to 18,446,744,073,709,551,61618,446,744,073,709,551,616

longlong 88 -9,223,372,036,854,775,808 to +-9,223,372,036,854,775,808 to +

unsigned intunsigned int 44 0 to 4,294,967,2950 to 4,294,967,295

intint 44 -2,147,483,648 to -2,147,483,648 to +2,147,483,648+2,147,483,648

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

Type coercionType coercion

• Type CoercionType Coercion: automatic conversion : automatic conversion of an operand to another data typeof an operand to another data type

• PromotionPromotion: convert to a higher type: convert to a higher type

• DemotionDemotion: convert to a lower type: convert to a lower type

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

Type coercionType coercion

• charchar, , shortshort, , unsigned shortunsigned short automatically promoted to automatically promoted to intint

• When operating on values of different When operating on values of different data types, the lower one is promoted to data types, the lower one is promoted to the type of the higher one.the type of the higher one.

• When using the When using the == operator, the type of operator, the type of expression on right will be converted to expression on right will be converted to type of variable on lefttype of variable on left

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

Overflow and UnderflowOverflow and Underflow

• When a variable is assigned a value that is When a variable is assigned a value that is too large or too small in range for that too large or too small in range for that variable’s data type, the variable overflows variable’s data type, the variable overflows or underflows.or underflows.

• Variable contains value that is ‘wrapped Variable contains value that is ‘wrapped around’ set of possible valuesaround’ set of possible values

• No warning or error message is given, if an No warning or error message is given, if an overflow or underflow occurs, the program overflow or underflow occurs, the program will use the incorrect number, and therefore will use the incorrect number, and therefore produce incorrect resultsproduce incorrect results

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

Type castingType casting

• Manual data type conversionManual data type conversion

• The general format of a type cast The general format of a type cast expressionexpression– static_cast<DataType> (Value)static_cast<DataType> (Value)

– ExampleExample

double number=4.6;double number=4.6;

int val;int val;

val=static_cast<int> (number)val=static_cast<int> (number)

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

Type castingType casting

int months, books;int months, books;

double perMonth;double perMonth;

perMonth=static_cast<double>(books)/perMonth=static_cast<double>(books)/monthsmonths

perMonth=static_cast<double>(books/perMonth=static_cast<double>(books/months)months)

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

Type casting (Another Type casting (Another Example)Example)void main()void main(){{

int number=99;int number=99;

cout<<number<<endl;cout<<number<<endl;cout<<static_cast<char>(number)<<endl;cout<<static_cast<char>(number)<<endl;

}}

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

Named constantNamed constant

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

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

Named constantNamed constantvoid main()void main(){{

float tuition,payment;float tuition,payment;int creditHours;int creditHours;

count<<“How many credit hours you\’ll take in spring? ”;count<<“How many credit hours you\’ll take in spring? ”;cin>>creditHours;cin>>creditHours;tuition=creditHours*128;tuition=creditHours*128;

cout<<“Monthly payment= ”<<tuition/5<<endl;cout<<“Monthly payment= ”<<tuition/5<<endl;

count<<“How many credit hours you\’ll take in Fall? ”;count<<“How many credit hours you\’ll take in Fall? ”;cin>>creditHours;cin>>creditHours;tuition=creditHours*128;tuition=creditHours*128;

cout<<“Monthly payment= ”<< tuition/4 <<endl;cout<<“Monthly payment= ”<< tuition/4 <<endl;}}

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

Named constantNamed constant

• A named constant is really a variable A named constant is really a variable whose content is ready-only, and whose content is ready-only, and cannot be changed while the cannot be changed while the program is runningprogram is running

• const double TUITION_RATE=128;const double TUITION_RATE=128;

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

Named constantNamed constantvoid main()void main(){{

float tuition,payment;float tuition,payment;int creditHours;int creditHours;const short tuitionRate=128;const short tuitionRate=128;

count<<“How many credit hours you\’ll take in spring? ”;count<<“How many credit hours you\’ll take in spring? ”;cin>>creditHours;cin>>creditHours;tuition=creditHours*?;tuition=creditHours*?;

cout<<“Monthly payment= ”<<tuition/?<<endl;cout<<“Monthly payment= ”<<tuition/?<<endl;

count<<“How many credit hours you\’ll take in Fall? ”;count<<“How many credit hours you\’ll take in Fall? ”;cin>>creditHours;cin>>creditHours;tuition=creditHours*?;tuition=creditHours*?;

cout<<“Monthly payment= ”<< tuition/? <<endl;cout<<“Monthly payment= ”<< tuition/? <<endl;}}

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

Named constantNamed constant

• To define common value that is To define common value that is difficult to remember,difficult to remember,

const double PI=3.1415926;const double PI=3.1415926;

• To indicate that array’s size. Here is To indicate that array’s size. Here is an examplean example

const int SIZE=21;const int SIZE=21;

char name[SIZE];char name[SIZE];

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

Multiple assignmentMultiple assignment

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

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

Combined assignment Combined assignment operatorsoperators

• Assume x=10;Assume x=10;

x=x+4;x=x+4;

x=x-2;x=x-2;

x=x*10;x=x*10;

x=x/5;x=x/5;

x=x%4;x=x%4;

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

combined assignment combined assignment operatorsoperators

• C++ provides a special set of operators designed C++ provides a special set of operators designed specifically for these jobsspecifically for these jobs

• Compound operatorsCompound operators

OperatoOperatorr

ExamplExamplee

Equivalent toEquivalent to

+=+= x +=5;x +=5; x=x+5;x=x+5;

-=-= y -=2;y -=2;

*=*= z*=10;z*=10;

/=/= a/=b;a/=b;

%=%= c%=3;c%=3;

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

More examplesMore examples

result*=a+5;result*=a+5;