Professional Documents
Culture Documents
Variables Values and Operations
Variables Values and Operations
2
© 2010 Noah Mendelsohn
Storing Information in a
Computer Program
3
© 2010 Noah Mendelsohn
The challenge: so many different types of information!!
As we said on day 1, computers have an amazing ability to run
programs that work on all these kinds of information…and more!!
4
© 2010 Noah Mendelsohn
What’s inside a computer (simplified)!!
5
© 2010 Noah Mendelsohn
What’s inside a computer (simplified)!!
6
© 2010 Noah Mendelsohn
What’s inside a computer (simplified)!!
7
© 2010 Noah Mendelsohn
What’s inside a computer (simplified)!!
8
© 2010 Noah Mendelsohn
The CPU has
instructions to put
The computer also has a "memory" data into and out of
memory.
MEMORY
Central
Processing Unit
CPU
9
© 2010 Noah Mendelsohn
We can store
The computer also has a "memory" numbers
MEMORY
Central
Processing Unit 12
CPU Apples
10
© 2010 Noah Mendelsohn
We can store
The computer also has a "memory" numbers
MEMORY
PROBLEMS:
Central
How can we tell the machine
Processing Unit what 12
to put where?
CPU Apples
11
© 2010 Noah Mendelsohn
We can store
The computer also has a "memory" numbers
MEMORY
PROBLEMS:
Central
How can we tell the machine
Processing Unit what 12
to put where?
CPU Apples
12
© 2010 Noah Mendelsohn
Variables
-
Where your program keeps
the data it’s working on!
13
© 2010 Noah Mendelsohn
What is a variable?
A variable is a named* location in memory that can
store some information
Your program can:
– Set and later change the value of a variable
– Get back and work with the value of the variable at any time
Every variable has a type that specifies the nature of
information that the variable contains:
– Is it a number? A string of text like "A rose by any other name"? A
true/false value? Etc., etc.
Let’s see how C++ can help us to create and use variables…
* Full disclosure: eventually we’ll see variables with no names, but
don’t worry about that for now!
14
© 2010 Noah Mendelsohn
C++ Variables
Find us some space MEMORY
We say that we have
declared the variable for a variable named
named fruit. fruit.
fruit
int main()
{
string fruit;
fruit = "Apples";
cout << fruit;
}
15
© 2010 Noah Mendelsohn
C++ Variables
Find us some space MEMORY
We say that we have
declared the variable for a variable named
named fruit. fruit.
fruit
int main()
{
string fruit;
fruit = "Apples";
cout << fruit;
}
Crucially: C++ will remember for us
where the fruit variable lives…we
don’t have to worry about that!
16
© 2010 Noah Mendelsohn
C++ Variables
Find us some space MEMORY
We say that we have
declared the variable for a variable named
named fruit. fruit.
fruit (string)
int main()
{
string fruit;
fruit = "Apples";
cout << fruit;
}
C++ will also remember that we are storing
strings of text here…
17
© 2010 Noah Mendelsohn
Using our C++ variable to store some information
Tell the CPU to put the MEMORY
word Apples in the
variable named fruit.
Central
Processing Unit
fruit (string)
int main() CPU
{
string fruit;
fruit = "Apples";
cout << fruit;
}
18
© 2010 Noah Mendelsohn
C++ Variables
Tell the CPU to put the MEMORY
word Apples in the
variable named fruit.
Central
Processing Unit
Apples Apples fruit (string)
int main() CPU
{
string fruit;
fruit = "Apples";
cout << fruit;
}
19
© 2010 Noah Mendelsohn
C++ Variables
MEMORY
Central
Processing Unit
20
© 2010 Noah Mendelsohn
C++ Variables
MEMORY
Tell the CPU to send the contents of
the variable fruit to the display.
Central
Processing Unit
Apples fruit (string)
int main() CPU
{
string fruit;
fruit = "Apples";
cout << fruit; Apples
}
21
© 2010 Noah Mendelsohn
Updating the value
in a variable
22
© 2010 Noah Mendelsohn
We can change what’s in a variable over and over again!
MEMORY
Central
Processing Unit
Apples fruit (string)
int main() CPU
{
string fruit;
fruit = "Apples";
cout << fruit; Apples
fruit = "Banana";
cout << fruit;
}
23
© 2010 Noah Mendelsohn
We can change what’s in a variable over and over again!
The fruit variable gets the new MEMORY
value "Banana"
Central
Processing Unit
24
© 2010 Noah Mendelsohn
We can change what’s in a variable over and over again!
The fruit variable gets the new MEMORY
value "Banana"
Central
Processing Unit
Banana Apples
Banana fruit (string)
int main() CPU
{
string fruit;
fruit = "Apples";
cout << fruit; Apples
fruit = "Banana";
cout << fruit;
}
25
© 2010 Noah Mendelsohn
Tell the CPU to send the contents of
C++ Variables: C++the
always
variableuses
fruit tothe last value set!
the display…
exactly the same statement as the
first time!! MEMORY
Central
Processing Unit
26
© 2010 Noah Mendelsohn
C++ Variables: C++ always uses
Read the last value set!
in a string
MEMORY
Central
Processing Unit
Apples fruit (string)
int main() CPU
{
string fruit;
fruit = "Apples";
cout << fruit; Apples
cin >> fruit;
cout << fruit; Whatever
}
27
© 2010 Noah Mendelsohn
C++ Variables: C++ always uses the last value set!
MEMORY
Central
Processing Unit
Whatever fruit (string)
int main() CPU
{
string fruit;
fruit = "Apples";
cout << fruit; Apples
cin >> fruit; Whatever
cout << fruit; Whatever
}
28
© 2010 Noah Mendelsohn
C++ Variables: C++ alwaysImportant!!
uses the last value set!
What would this do?
MEMORY
Central
Processing Unit
29
© 2010 Noah Mendelsohn
With the "" quotes we are referring
C++ Variables: C++ always uses
to the literal the"fruit"…
string last value set!
…without the quotes we are MEMORY
referring to the contents of the
variable named fruit!!
Central
Processing Unit
fruit
Banana fruit (string)
int main() CPU
{
string fruit;
fruit = "Apples";
cout << fruit; Apples
fruit = "Banana"; Banana
cout << fruit; fruit
cout << "fruit";
}
30
© 2010 Noah Mendelsohn
What just happened??
31
© 2010 Noah Mendelsohn
Declare a string variable named fruit
MEMORY
fruit (string)
int main()
{
string fruit;
fruit = "Apples";
cout << fruit;
}
32
© 2010 Noah Mendelsohn
Set the string value "Apple" to the variable named fruit
MEMORY
We say that the variable fruit gets or is assigned the value Apple.
33
© 2010 Noah Mendelsohn
Use the cout stream to sent the contents of fruit
to the display
MEMORY
34
© 2010 Noah Mendelsohn
Initializing variables
35
© 2010 Noah Mendelsohn
Abstraction in Action
36
© 2010 Noah Mendelsohn
On day 1 we talked about four big ideas….
• Modeling
• Abstraction
• Modularity
• Divide-conquer-glue
37
© 2010 Noah Mendelsohn
On day 1 we talked about four big ideas….
• Modeling These are the sorts of
reasons we use languages
• Abstraction like C++ to program our
computers!
• Modularity
• Divide-conquer-glue
int main()
{
string fruit;
fruit = "Apples";
cout << fruit; string
}
Apple
fruit
39
© 2010 Noah Mendelsohn
Types
40
© 2010 Noah Mendelsohn
Types tell us what kind of information a variable holds
41
© 2010 Noah Mendelsohn
Types tell us what kind of information a variable holds
42
© 2010 Noah Mendelsohn
Types tell us what kind of information a variable holds
43
© 2010 Noah Mendelsohn
Types tell us what kind of information a variable holds
44
© 2010 Noah Mendelsohn
Types tell us what kind of information a variable holds
45
© 2010 Noah Mendelsohn
Types tell us what kind of information a variable holds
46
© 2010 Noah Mendelsohn
C++ Fundamental Types (partial list)
Type Name Values Example literals
int integers (may be positive or negative 1, 3, 0, -543, 987654
or zero)
unsigned int A non-negative integer 1, 3 , 0, 987654
string Zero or more characters "A rose by any other name"
float, double Numbers with fractions; numbers in 1.235
scientific notation -355.20
0.000000404
2.0
-135E12 (Same as
135 x 1012..
The "E" stands for
"exponent
bool Results of true/false tests, etc. E.g. 4 true, false
> 3 is true. (must be written lower
case)
47
© 2010 Noah Mendelsohn
Types, Operations and
Expressions
48
© 2010 Noah Mendelsohn
Integer types support arithmetic operations
Integer
division drops
Operator Purpose Example fractions
+ Addition 4 + 2 6
- Subtraction 4 - 2 2
* Multiplication 4 * 2 8 Result of %
/ Division 4 / 2 2 always has sign
of first argument
7 / 4 1
(-5) / 2 -2
% Remainder (mod) 4 % 2 0
5 % 2 1
(-5) % 2 -1
(-5) % (-2) -1
Examples: x = 3 + 5;
y = x % 3;
49
© 2010 Noah Mendelsohn
Dividing two
Float and double operations are similar to int floats can yield
a fractional
Operator Purpose Example result.
+ Addition 4.3 + 2.2 6.5
- Subtraction 4.3 – 2.2 2.1 There is no
remainder
* Multiplication 4.2 * 2.0 8.4 operation for
floats!
/ Division 5.0 / 4.0 1.25
(-5.0) / 4.0 -1.25
% Remainder (mod) 4.0 % 2.0 4.0 is a float,
and if either
but floats and doubles can have fractional parts operand to / is
a float, so is
Float literals must include a decimal point the result
50
© 2010 Noah Mendelsohn
We can combine the operations into expressions
int
main(int argc, char *argv[])
{
int x =3;
int y=4;
int z = 2 * x + y ;
cout << z << endl;
return 0;
}
Prints: 10
51
© 2010 Noah Mendelsohn
We can often use expressions instead of variables
int
main(int argc, char *argv[])
{
int x =3;
int y=4;
cout << 2 * x + y << endl;
return 0;
}
Prints: 10
52
© 2010 Noah Mendelsohn
The = operator does not always imply equality
A mathematician says:
x = x + 1
is always a false statement!
x = x + 1;
is a great way to add one to x!
53
© 2010 Noah Mendelsohn
The = operator does not always imply equality
Important:
x = x + 1;
is a great way to add one to x!
54
© 2010 Noah Mendelsohn
Operator precedence
5+3*2+6/3+4
evaluates as
5+(3*2)+(6/3)-4
There are many more operators and the precedence rules are
tricky…when in doubt write out all the parentheses!
55
© 2010 Noah Mendelsohn
Operator precedence
5+3*2+6/3+4
evaluates as
Greatly reduces
5+(3*2)+(6/3)- 4 the chance of
bugs!!
but you can use parens () to change
that, e.g.
(5+3)* 2 + 6 / (3–4)
Best practice: fully
parenthesize
((5+3)* 2) + (6 / (3–4))
56
© 2010 Noah Mendelsohn
A program that uses strings and integers
int main()
{
string fruit1;
int count1; // int type variables hold integers
string fruit2;
int count2; // int type variables hold integers
int total_items = 0;
//
// Figure out total number of fruit items we will be buying
//
total_items = count1 + count2;
cout << "That's a total of " << total_items << " fruits" << endl;
return 0;
}
57
© 2010 Noah Mendelsohn
A program that uses strings and integers
int main()
{
string fruit1;
int count1; // int type variables hold integers
string fruit2;
int count2; // int type variables hold integers
int total_items = 0;
//
// Figure out total number of fruit items we will be buying
//
total_items = count1 + count2;
cout << "That's a total of " << total_items << " fruits" << endl;
return 0;
}
58
© 2010 Noah Mendelsohn
A program that uses strings and integers
int main()
{
string fruit1;
int count1; // int type variables hold integers
string fruit2;
int count2; // int type variables hold integers
int total_items = 0;
//
// Figure out total number of fruit items we will be buying
//
total_items = count1 + count2;
cout << "That's a total of " << total_items << " fruits" << endl;
return 0;
}
59
© 2010 Noah Mendelsohn
A program that uses strings and integers
$./fruit_shopping
Enter the name of a fruit and the amount you want to buy: apple 5
Enter the name of another fruit and the amount you want to buy: banana 12
int main()
{
You are planning to buy:
string fruit1; 5 apples
int count1; ...and...
// int type variables hold integers
string fruit2;
int count2;
12 bananas
// int type variables hold integers
That's a total of 17 items
int total_items = 0; $
// Get information about first fruit
cout << "Enter the name of a fruit and the amount you want to buy: ";
cin >> fruit1;
cin >> count1;
//
// Figure out total number of fruit items we will be buying
//
total_items = count1 + count2;
cout << "That's a total of " << total_items << " fruits" << endl;
return 0;
}
60
© 2010 Noah Mendelsohn
A program that uses strings and integers
$./fruit_shopping
Enter the name of a fruit and the amount you want to buy: apple 5
Enter the name of another fruit and the amount you want to buy: banana 12
int main()
{
You are planning to buy:
string fruit1; 5 apples
int count1; ...and...
// int type variables hold integers
string fruit2;
int count2;
12 bananas
// int type variables hold integers
That's a total of 17 items
int total_items = 0; $
// Get information about first fruit
cout << "Enter the name of a fruit and the amount you want to buy: ";
cin >> fruit1;
cin >> count1;
// Confirm what we read in so user can verify it's correct We can do either of:
cout << "You are planning to buy:" << endl;
cout << count1 << " " << fruit1 << "s" << endl;
int count =100
cout << "...and..." << endl; string count = "100";
cout << count2 << " " << fruit2 << "s" << endl;
Why not use strings for everything?
//
// Figure out total number of fruit items we will be buying
//
total_items = count1 + count2;
cout << "That's a total of " << total_items << " fruits" << endl;
return 0;
}
61
© 2010 Noah Mendelsohn
An integer is not the same as a string with digits in it
62
© 2010 Noah Mendelsohn
Pause to write some programs
63
© 2010 Noah Mendelsohn
Some other integer types
64
© 2010 Noah Mendelsohn
More advanced integer types – integers come in different sizes
65
© 2010 Noah Mendelsohn
Table of C++ Integer Types on our Halligan Computers
66
© 2010 Noah Mendelsohn
A little more on characters and
strings
67
© 2010 Noah Mendelsohn
Table of C++ Integer Types on our Halligan Computers
Hmm, why is this tiny little
integer type called "char"?
Type Name Space used Values
int 4 bytes -2,147,483,648 2,147,483,647
unsigned int 4 bytes 0 4,294,967,296
char 1 byte -128 127
unsigned char 1 byte 0 256
short 2 bytes -32768 32767
unsigned short 2 bytes 0 65535
long 8 bytes -9,223,372,036,854,780,000
9,223,372,036,854,780,000
unsigned long 8 bytes 0 18,446,744,073,709,600,000
68
© 2010 Noah Mendelsohn
The challenge: so many different types of information!!
As we said on day 1, computers have an amazing ability to run
programs that work on all these kinds of information…and more!!
How do we make computers and programs that can store and manipulate
all of these…?
69
© 2010 Noah Mendelsohn
How does a computer store all these things?
70
© 2010 Noah Mendelsohn
How does a computer store all these things?
71
© 2010 Noah Mendelsohn
Double quotes on a literal
chars and strings makes it a string.
72
© 2010 Noah Mendelsohn
But chars are really numbers!!
The table of ASCII
Mappings from characters to numbers
73
© 2010 Noah Mendelsohn
The challenge: so many different types of information!!
As we said on day 1, computers have an amazing ability to run
programs that work on all these kinds of information…and more!!
IT'S ALL
NUMBERS INSIDE
THE COMPUTER!!
Advanced
How do wetopic:
makeincomputers
almost all and
computers,
programs thethat
numbers
can store
are and
stored manipulate
as binary (base
2)
all rather
of these…?
than decimal (base 10) numbers. In advanced programs you can see the
difference sometimes…and, that's why the limits on the numbers we can store
seem so strange! Flower photo by: Mohamed Abelsadig (https://www.pexels.com/@moab94)
74
© 2010 Noah Mendelsohn
Wrapup
75
© 2010 Noah Mendelsohn
Review: what we learned in this session
Next time: we will learn how to build more complicated programs that
work with large quantities of data, that make decisions based on the
data and the computed results, and that can loop to apply the same
computation repeatedly.
76
© 2010 Noah Mendelsohn