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

Slides:



Advertisements
Similar presentations
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Advertisements

Chapter 3: Expressions and Interactivity. Outline cin object Mathematical expressions Type Conversion and Some coding styles.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Expressions.
CS1 Lesson 3 Expressions and Interactivity CS1 -- John Cole1.
Variables: Named Storage Locations Variables must be defined or declared before they can be used so that appropriate memory storage can be allocated for.
1 9/13/06CS150 Introduction to Computer Science 1 Type Casting.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
Computer Science 1620 Other Data Types. Quick Review: checklist for performing user input: 1) Be sure variable is declared 2) Prompt the user for input.
Copyright © 2012 Pearson Education, Inc. Chapter 3: Expressions and Interactivity.
1 9/17/07CS150 Introduction to Computer Science 1 Type Casting.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 3 Expressions.
CS150 Introduction to Computer Science 1
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 3- 1.
CS Sept 2006 Pick ups from chapters 2 and 3.
CS 1400 Chapter 3: sections Variable initialization Variables may be initialized when declared –Form; type name = initial_value; –Example: int.
CS Jan 2007 Chapter 3: sections Variable initialization Variables may be initialized when declared –Form; type name = initial_value; –Example:
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 3: Expressions and Interactivity.
Representation and Conversion of Numeric Types 4 We have seen multiple data types that C provides for numbers: int and double 4 What differences are there.
Basic Elements of C++ Chapter 2.
Expressions and Interactivity Chapter 3. 2 The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Often.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Expressions and Interactivity.
Copyright © 2012 Pearson Education, Inc. Chapter 3: Expressions and Interactivity.
Chapter 2: Using Data.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Integer numerical data types. The integer data types The integer data types use the binary number system as encoding method There are a number of different.
CPS120: Introduction to Computer Science Operations Lecture 9.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for CMPS 1043 Computer Science I at MSU.
C++ Programming, Namiq Sultan1 Chapter 3 Expressions and Interactivity Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 3 Expressions and Interactivity.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Expressions and Interactivity. 3.1 The cin Object.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Formatting Output.
Simple Data Types Chapter Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 3: Expressions and Interactivity.
CMPSC 121- Spring 2015 Lecture 6 January 23, 2015.
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 3: Expressions and Interactivity.
Last Time…. Operators Arithmetic Operators Assignment Operators Increment/Decrement Operators Relational Operators Logical Operators Expression Statements.
Dr. Sajib Datta Jan 21,  Declare a variable ◦ int height; [note that no value is still assigned]  Assign a variable a value ◦ height =
Chapter Expressions and Interactivity 3. The cin Object 3.1.
Chapter 4: Introduction To C++ (Part 3). The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Information.
Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3: Expressions and Interactivity.
Chapter INTRODUCTION Data Types and Arithmetic Calculations.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
1 Section 3.2b Arithmetic Operators Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Lecture 3 Expressions, Type Conversion, Math and String
Chapter Topics The Basics of a C++ Program Data Types
Chapter 7: Expressions and Assignment Statements
Chapter 2 Elementary Programming
Chapter 3. Expressions and Interactivity
Basic Elements of C++.
Chapter 7: Expressions and Assignment Statements
Assignment and Arithmetic expressions
Type Conversion, Constants, and the String Object
Basic Elements of C++ Chapter 2.
Chapter 1 C for Embedded Systems.
Conversions of the type of the value of an expression
Expressions and Interactivity
3-3 Side Effects A side effect is an action that results from the evaluation of an expression. For example, in an assignment, C first evaluates the expression.
Chapter 1 C for Embedded Systems.
3-3 Side Effects A side effect is an action that results from the evaluation of an expression. For example, in an assignment, C first evaluates the expression.
Expressions and Interactivity
Chapter 3 Operators and Expressions
Standard Version of Starting Out with C++, 4th Edition
Lecture 3 Expressions, Type Conversion, and string
Variables and Constants
Presentation transcript:

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

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.

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

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

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

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

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)

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

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

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.

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; }

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;

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; }

Named constant To define common value that is difficult to remember, To define common value that is difficult to remember, const double PI= ; 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];

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;

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

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;

More examples result*=a+5;