{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

9. Data Types - ENGR 101 Data Types ENGR 101 Lecture 9 11...

Info icon This preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
ENGR 101 1 Data Types ENGR 101, Lecture 9: 11 Oct 10 Today's Question How high can you count on the fingers of one hand?
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
ENGR 101 2 Announcements Project 4 due Wed 9pm How is your progress on Project 4? A. Have not started yet B. Started, not very far yet C. Have single game mode (option 0) working D. Just fixing last few things E. Completed Two Key Elements of Programming Flow of Control Sequence Selection Iteration Data Representation How do we structure the data that we are acting on?
Image of page 2
ENGR 101 3 Data Types Definition a set of possible values plus , a set of legal operations on those values for example ( int ): Integers within a given range Operations: addition ( + ), subtraction ( - ), etc. Mixed-Type Expressions Even when binary operators are defined for operands of same type, can often apply them to different types. For example, arithmetic operators: 53 + 26.2 53 / 26.2 26.2 - true To evaluate, more restricted operand is promoted to type of the other bool int double
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
ENGR 101 4 Type Coercion Coercion : The implicit (automatic) conversion of a value from one data type to another Coercion may apply when: Evaluating a mixed-type expression Assigning a value double myDub = 3; int myInt = 24.9; double avg = 39/5; Passing a value to a routine Returning a value from a function promoted to 3.0 demoted to 24 promoted to ?? Type Casting Cast : The explicit conversion of a value from one data type to another Syntax of cast operation: T ( expr ) Evaluates expr, converts to type T int(24.9) int(x+0.5) double(39)/5 Explicit cast often recommended for clarity, even when coercion would apply 24 (just takes whole part) rounds to nearest int (if x positive) 7.8
Image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}