Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 76

COMP 11: Introduction to Computer Science

Variables, Values and Operations


———
Storing and Manipulating Data
in your Programs

Mark Sheldon Noah Mendelsohn


Tufts University Tufts University
Email: msheldon@cs.tufts.edu Email: noah@cs.tufts.edu
Web: http://www.cs.tufts.edu/~msheldon Web: http://www.cs.tufts.edu/~noah

Copyright 2021– Noah Mendelsohn and Mark Sheldon


Goals for this session

 Understand what computers need to be able to do with


information
 Learn how computers and programming languages work together
to create variables that hold the program’s data while the program
runs
 Learn how operations are used to compute with the values in
variables and with fixed "literal" values

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!!

How do we make programs that can store and manipulate


all of these…?

Flower photo by: Mohamed Abelsadig (https://www.pexels.com/@moab94)

4
© 2010 Noah Mendelsohn
What’s inside a computer (simplified)!!

int main() Central


{ Processing Unit
cout << "Hello World";
cout << "Hello Again";
return 0;
CPU
}

5
© 2010 Noah Mendelsohn
What’s inside a computer (simplified)!!

int main() Central


{ Processing Unit
cout << "Hello World";
cout << "Hello Again";
return 0;
CPU
Hello World
}

6
© 2010 Noah Mendelsohn
What’s inside a computer (simplified)!!

int main() Central


{ Processing Unit
cout << "Hello World";
cout << "Hello Again";
return 0;
CPU
Hello World
}
Hello Again

7
© 2010 Noah Mendelsohn
What’s inside a computer (simplified)!!

The CPU in the computer goes step by step,


int picking up each statement in the program,
main() Central
{ and doing what the statements
Processing Unit say to do.
cout << "Hello World";
cout << "Hello Again";
return 0;
CPU
Hello World
}
Hello Again

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

And text information


that we call "strings" …and lots of other
things!

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

How can we remember what kind


of information is at each location?

And text information


that we call "strings"

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

How can we remember what kind


of information is at each location?

Programming languagesAnd like


text information
C++ help us do that! that we call "strings"

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…

…not numbers, or other types of information.

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

Apples fruit (string)


int main() CPU
{
string fruit;
fruit = "Apples";
cout << fruit; We say:
}
The variable fruit gets the value
"Apples"

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

Apples fruit (string)


int main() CPU
{
string fruit;
fruit = "Apples";
cout << fruit; Apples
fruit = "Banana";
cout << fruit;
}

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

Banana fruit (string)


int main() CPU
{
string fruit;
fruit = "Apples";
cout << fruit; Apples
fruit = "Banana"; Banana
cout << fruit;
}

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

Banana fruit (string)


int main() CPU
{
string fruit;
fruit = "Apples";
cout << fruit; Apples
fruit = "Banana"; Banana
cout << fruit;
cout << "fruit";
}

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??

Review of declaring and using


a variable

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

Apples fruit (string)


int main()
{
string fruit;
fruit = "Apples";
cout << fruit;
}

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

Apples fruit (string)


int main()
{
string fruit;
fruit = "Apples";
cout << fruit; Apples
}

34
© 2010 Noah Mendelsohn
Initializing variables

C++ let’s us initialize a variable when we declare it


(these two programs do the same thing)

int main() int main()


{ {
string fruit; string fruit = "Apples";
fruit = "Apples"; cout << fruit;
cout << fruit; fruit = "Banana";
fruit = "Banana"; cout << fruit;
cout << fruit; }
}

Initializing variables is typically a good thing to do, otherwise we might


accidentally wind up using whatever was lying around in that place in memory!

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

C++ hides from you all the messy details of how a


CPU works, where variables are in memory, etc.

Indeed, having named and typed variables gives you a


beautiful, clean abstraction for using the computer’s
memory.
38
© 2010 Noah Mendelsohn
Indeed: we can simplify our pictures from now on

A variable named fruit of type string


holding the value "Apples"

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

Variable student_name holds a string


of text.

string student_name = "Mary Smith";


int number_of_courses = 4;
float gpa = 3.75;

41
© 2010 Noah Mendelsohn
Types tell us what kind of information a variable holds

Variable number_of_courses holds an


integer
(I.e. a number with no fraction.)

string student_name = "Mary Smith";


int number_of_courses = 4;
float gpa = 3.75;

42
© 2010 Noah Mendelsohn
Types tell us what kind of information a variable holds

Variable gpa holds a float


(I.e. a number that may have a
fraction.)

string student_name = "Mary Smith";


int number_of_courses = 4;
float gpa = 3.75;

43
© 2010 Noah Mendelsohn
Types tell us what kind of information a variable holds

string student_name = "Mary Smith";


int number_of_courses = 4;
float gpa = 3.75;

IMPORTANT: the type of a variable determines:

• The values it can hold


• The operations we can do on it

44
© 2010 Noah Mendelsohn
Types tell us what kind of information a variable holds

string student_name = "Mary Smith";


int number_of_coursesWe=can't
4; put text in a value that
float gpa = 3.75; holds only numbers!

IMPORTANT: the type of a variable determines:

• The values it can hold


• The operations we can do on it

student_name = "Bob Jones"; // OK


gpa = "Bob Jones"; // NOT OK !!

45
© 2010 Noah Mendelsohn
Types tell us what kind of information a variable holds

string student_name = "Mary Smith";


int number_of_courses = 4;
float gpa = 3.75;
We can add 1 to an integer, but not
to the student's name!
IMPORTANT: the type of a variable determines:

• The values it can hold


• The operations we can do on it

cout = << (number_of_courses + 1) // OK


cout = << (student_name + 1) // NOT OK

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

sum = 123; // assign the integer 123 to variable sum


area = 37.25; // assign the float literal 37.25 to area
gpa = 4.0; // assign the float (yes float!) literal 4.0

7/41 7 / 4.0  1.75

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!

A C++ programmer says:

x = x + 1;
is a great way to add one to x!

53
© 2010 Noah Mendelsohn
The = operator does not always imply equality
Important:

What the assignment operator = does is:


A mathematician says:
That's why we say, x gets 1. Evaluate the expression on the right
x+1. 2. Assign the computed value to the
x = x + 1variable on the left
(Of course, x never equals
x+1!) is always a false statement!
If the same variable occurs on both sides, the
new value depends on the old.

A C++ programmer says:

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

We say * and / have higher precedence than + and -.

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;

// Get information about first fruit


cout << "Enter the name of a fruit and the amount you want to buy: ";
cin >> fruit1;
cin >> count1;

// Get information about second fruit


cout << "Enter the name of another fruit and the amount you want to buy: ";
cin >> fruit2;
cin >> count2;

// Confirm what we read in so user can verify it's correct


cout << "You are planning to buy:" << endl;
cout << count1 << " " << fruit1 << "s" << endl;

cout << "...and..." << endl;


cout << count2 << " " << fruit2 << "s" << endl;

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

// Get information about first fruit


cout << "Enter the name of a fruit and the amount you want to buy: ";
cin >> fruit1;
cin >> count1;

// Get information about second fruit


cout << "Enter the name of another fruit and the amount you want to buy: ";
cin >> fruit2;
cin >> count2;

// Confirm what we read in so user can verify it's correct


cout << "You are planning to buy:" << endl;
cout << count1 << " " << fruit1 << "s" << endl;

cout << "...and..." << endl;


cout << count2 << " " << fruit2 << "s" << endl;

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

// Get information about first fruit


cout << "Enter the name of a fruit and the amount you want to buy: ";
cin >> fruit1;
cin >> count1;

// Get information about second fruit


cout << "Enter the name of another fruit and the amount you want to buy: ";
cin >> fruit2;
cin >> count2;

// Confirm what we read in so user can verify it's correct


cout << "You are planning to buy:" << endl;
cout << count1 << " " << fruit1 << "s" << endl;

cout << "...and..." << endl;


cout << count2 << " " << fruit2 << "s" << endl;

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

// Get information about second fruit


cout << "Enter the name of another fruit and the amount you want to buy: ";
cin >> fruit2;
cin >> count2;

// Confirm what we read in so user can verify it's correct


cout << "You are planning to buy:" << endl;
cout << count1 << " " << fruit1 << "s" << endl;

cout << "...and..." << endl;


cout << count2 << " " << fruit2 << "s" << endl;

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

// Get information about second fruit


cout << "Enter the name of another fruit and the amount you want to buy: ";
cin >> fruit2;
cin >> count2; Question:

// 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

 We can add and subtract from the integer


 Later we will learn how to do operations on strings, for
example:
– We can combine two strings to make a longer one
– We can search a string to see if contains some particular word\
– We can find out how many characters are in the string (interestingly,
this operation takes in a string and gives back an integer)

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

Smaller integer types take less space in the


computer, but they can't hold big numbers!

• For most of the integers we use in COMP 11, the int


type will work fine

• Use the others if you are writing more complicated


programs, particularly if you are storing thousands
or millions of numbers, or if the values you are
storing might be very large or small

65
© 2010 Noah Mendelsohn
Table of C++ Integer Types on our Halligan Computers

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

Be careful: for numbers over approximately 2 Billion (or 4 Billion


for unsigned) you may need to use the long types!

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…?

Flower photo by: Mohamed Abelsadig (https://www.pexels.com/@moab94)

69
© 2010 Noah Mendelsohn
How does a computer store all these things?

 The amazing trick: the memory itself just stores numbers!


 We encode or represent everything else as numbers
 Examples:
– The brightness of a pixel in an image (typically 0-255)
– The shape of an audio wave, e.g. in a .wav file (typically a sequence of 16 or 24 bit
numbers, sampled 44,100 times each second)
– The colors of each pixel in your display or encoded as numbers. Black is usually 0.
– There is a standard table called ASCII that assigns a number code to each character
on your keyboard, including the letters, the symbols like #,@().!, etc. and the digits
– There is fancier and more complicated table called UNICODE that extends this "trick"
to most all the characters you need for all the widely used languages in the world.

70
© 2010 Noah Mendelsohn
How does a computer store all these things?

 The amazing trick: the memory itself just stores numbers!


 We encode or represent everything else as numbers
 Examples:
– The brightness of a pixel in an image (typically 0-255)
– The shape of an audio wave, e.g. in a .wav file (typically a sequence of 16 or 24 bit
numbers, sampled 44,100 times each second)
– The colors of each pixel in your display or encoded as numbers. Black is usually 0.
– There is a standard table called ASCII that assigns a number code to each character
on your keyboard, including the letters, the symbols like #,@().!, etc. and the digits
– There is fancier and more complicated table called UNICODE that extends this "trick"
to most all the characters you need for all the widely used languages in the world.

71
© 2010 Noah Mendelsohn
Double quotes on a literal
chars and strings makes it a string.

 // a string is zero or more characters, in order


string fruit = "apple";
Single quotes are for
individual character literals
 // a char is a single character of type char.
char c='a';

72
© 2010 Noah Mendelsohn
But chars are really numbers!!
The table of ASCII
Mappings from characters to numbers

What does this program print?


int main()
{
char some_letter='A';
char other_letter = some_letter + 1;
cout << "some_letter=" << some_letter << endl;
cout << "other_letter=" << other_letter << endl;
return 0;
}
WOW!!

The computer is using the same some_letter=A


operations it uses on numbers to other_letter=B Source: http://www.lookuptables.com:
manipulate letters

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

 Understand what computers need to be able to do with


information
 Learn how computers and programming languages work together
to create variables that hold the program’s data while the program
runs
 Learn how operations are used to compute with the values in
variables and with fixed "literal" values

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

You might also like