Title: Module 3
1Module 3
- Data Types and Expressions,
- Input and Output
2Recall from Last Module
- Variables represent memory locations with names
that you make up - Three basic data types so far integral, floating
point, and character - Different types can use different amounts of
memory - Well talk some more now about data types
3Floating Point Numbers
- They have both an integer part and a fractional
part, with a decimal in between - They also have an exponent
Sign Exponent Mantissa
4Strings of Characters
- C/C both support a data type called stringa
string is a sequence of characters - While characters use a, strings use hey
- Examples Hello, a, 678
- The empty string has no characters, and is
written as
5Modifiers to Data Types
- Integers will allow the modifiers long and short
- short int is equivalent to short
- long int is equivalent to long
- Floating point double allows the modifier long
- Eg long double static_tension
- The consequence is possibly increased (or
decreased, for short) amount of memory used - This is system dependent!
6Modifiers, Continued
- For all standard data types, one can use the
modifier const will turn the variable into a
constant - When we use this, we can set the value of the
variable exactly once - Thereafter, we can use that value, but can never
change it - EG const char first_letter
7Assigning Values to Variables
- Syntax
- An expression is something that evaluates to a
particular value - It usually is a combination of numbers,
variables, and arithmetic operations - Eg income total_trans busns_cost
- We use the operator, called gets
Variable Expression
8Compilers and Expressions
- When the expression is encountered
- First it is evaluated
- Then the result is stored in the memory location
named on the left - Lets walk through an example or two of this on
the board
9Setting the first value
- When we give a value to a variable that
previously had none, we say we are initializing
the variable - We can do this when we declare the variable if we
know what value we want it to start with - Eg int Joe_age 67
10Mixed types
- In general, it is considered an error to try to
put a result of one type into a variable of
another - Integral types allow type promotion
- Floating point types allow type promotion
- No types allow demotion without help
- Can you think of some examples of each? Why does
this make sense?
Char lt Short lt Int lt Long
Float lt Double lt Long Double
11Example of Promotion
include ltiostream.hgt int main( ) // What gets
printed? int x 3, y 4 char z 'Z' x
z cout ltlt "X is" ltlt x ltlt endl return 0
12Operators Put To
- The put to operator is used with cout to
produce output - The thing to the left must be a streama source
of data - The right hand side can be any simple type or
string
cout ltlt X is ltlt x ltlt endl
13Operators Get From
- Likewise for output there is the gtgt or get
from operator - Here the stream is on the left side getting
something from a stream and saving it into a
variable - To use these, you must use the iostream.h header
file
14Addition, Subtraction, Multiplication
- These operators operate just like you are used
towe use , -, and to symbolize these
operations - Note that the minus sign can be used as a unary
operator to make a single number or variable
negative, EG
x y (-5)
15Division Operator
- The division (/) operator doesnt always operate
the way you are used to - Integer division must give an integer result!
- This is to avoid type mismatches
- So what happens is dependent on the type
16Real Numbers Versus Integers in Division
7/3 2.33333333 in real numbers 7/3 2
remainder 1 in integer math So 7/3 ? 2
for an integer in C And 7 3 ? 1 for an
integer in C This second operator is called
the modulus operator We read this as 7 mod 3
17Increment/Decrement Operators
- The increment () and decrement (--) operators
are unary operatorsthey take only one variable - They can be used on the left or right of a
variable to add or subtract one from it - Which side they appear on may have an effect on
the results!
18Prefix Increment
Code for each int x 3, y 4
int x 3, y 4 x y
x y Results stored in
memory X 5
X 4 Y 5
Y 5 Thus when the value is extracted from Y
is indicated by the position of the operator
19Precedence of Operators
- You need to know the precedence of basic
operators for this course! (Well, not all) - The precedence determines which thing is done
first - For the math operators, you should know
Precedence
Associativity Unary -, , -- right to
left , /, left
to right , -
left to right
right to left
20Associativity
- The associativity of an operator tells us which
direction to combine operators of equal precedence
x y z 4 m done first done
next done last
21Parentheses
- Parentheses can be used to change the usual order
- Anything inside parentheses is done first
- Evaluate the following
- (7 (10 5) 3) 4 9)
- ((- 5 5) 5 10)
22Type Casting
- Occasionally we need to explicitly change from
one type to another - Doing this is called type castingit can only be
done when C actually knows how to do it - Usually done when you are throwing information
away!
23Type Casting Continued
- Our compiler seems to accept either of two
different ways of expressing this - X int(Y)
- X (int) Y
- What are the results here if X is an integer and
Y - is a floating point value?
24Something Funny About ?
- We use the equals sign to mean many things
- We have to restrict meanings in programming
languages - Thus we can not use for comparison of values
(we will use in C) - Likewise, when we talk about functions, we will
have to restrict our meanings there
25Input From the User
int main( ) int x 0 cout ltlt Please input
an integer ltlt endl cin gtgt x cout ltlt Please
input another integer ltlt endl cin gtgt x
26Values Stored in Variables
- What happened to the values stored in x?
- We have a choice of persistence or overwriting
- C overwrites old values
- The mailbox with only one letter
- When an old value is gone, it is really gone!
- There is no way to recover past values of
variables
27Accumulation Operators
- If you want the effect of a running total, we
have special operators , , /, and - - The effect of (for example)
CODE EQUIVALENT Sum
purchase Sum Sum purchase
28Legal Code, Bad Style
- The following is a legal C statement
- What passes through an assignment operator is
what will be passed to the previous - This is a mess to read or documentdont do it!
variance myValue overAll result