CS 31 Intro To Programming UCLA C++
CS 31 Intro To Programming UCLA C++
Agenda
Learn about the programming
process
Questions about Project #1?
Our first C++ programs:
No
No
Design
Goal: Figure out how your program is going to
work before you write a single line of logic.
#include <iostream>
void main(void)
{
std::cout << GO BRUINS!";
}
#include <iostream>
57119 10 4699 3
void main(void)
{
std::cout << GO BRUINS!";
}
MyProg.exe
GO BRUINS!
(Whats
happening
under the
hood is a bit
more
complicated
than this,
but thats
the general
idea)
Things to do:
Test your program to make sure it works
Make sure your program works properly
even if the user doesnt use it correctly.
Fix any problems you find and then repeat
needed testing.
Hello world
Goodbye!
main
int main(void)
{
std::cout << "Hello ";
std::cout << world...\n ";
std::cout << Goodbye!";
function header
main
function
body
#include <iostream>
int main(void){std::cout<<
"Hello ";std::cout <<
world...\n ";std::cout <<
Goodbye!";return(0);}
int main(void)
{
std::cout << "Hello ";
std::cout << world...\n ";
std::cout << Goodbye!";
<< Goodbye!"; // this is just fine!
}
Hello world
Goodbye!
int main(void)
{
std::cout << "Hello world!\n";
std::cout
<< "Good\nbye!\n";
}
Hello world!
Good
bye!
#include <iostream>
years_old
int main(void)
{
int years_old, num_hairs;
num_hairs
100 - 33
33
67
#include <iostream>
years_old
int main(void)
{
int years_old, num_hairs;
num_hairs
33
67
std::endl are
is another
way to write
\n sure you use the
Variables
case sensitive
too! Make
same case when you declare your variable as when you use it
how you can print multiple items out by using more
inNotice
your program!
than one set of << in a single std::cout command.
#include <iostream>
years_old -7269
?
33
int main(void)
{
int years_old, num_hairs;
num_hairs 479
?
More on Variables
We must explicitly declare variables to hold data for our
program. This reserves memory in the computer for them.
To declare a variable, we first specify the variables
type, and then its name.
And
If we
welike,
can initialize
we can define
variables
multiple
whenvariables
we declare
at the
them
same
if we
time.
like.
int main(void)
{
int boogies;
boogies
???
Variable Names
1. Variable names may have letters, numbers
and underscores _s but no other symbols.
2. Variable names may not begin with a number.
3. Variable names are case sensitive.
Good or bad?
aGoodVariable15
Amount$Made
31CSROSTER
ANICE*VARIABLE
Num Goats
_EarWaxVolume
Types of Variables
In the previous example, we declared two integer variables:
#include <iostream>
int main(void)
{
int years_old, num_hairs;
// two integers
variables we can
use in our programs
Floating-point Variables
A floating
point
(float) variable can hold:
As weve
seen,
integer
variables
positive can
and negativeWhat
real numbers
both
if we want to
only hold whole
any value between 3.4perform
* 10-38 tocalculations
3.4 * 1038
number values.
with real numbers?
If you want to store real values in your program,
you need to use the float variable type.
int main(void)
pi 3.14159
{
float pi; // float variables can hold a real #
pi = 3.14159;
std::cout << PI times 10 is: << pi*10;
}
Unsigned Variables
In some cases, we want an integer variable to hold only nonnegative values.
In this case, we use an unsigned variable.
All you do is place the word unsigned in front of the
standard type name.
int main(void)
{
unsigned int age;
age = 10;
-5;
// ERROR!
Unsigned Variables
An unsigned integer (unsigned int) variable can hold:
both zero or positive whole numbers
any value between 0 and 4294967296
int main(void)
{
unsigned float
int
age;
age;
age = 10;
// ERROR!
long careysEgo;
// same as int careysEgo;
unsigned long smallbergsAge;
char
unsigned char
double
zits = 120;
grits = 254;
big = 3.925e123;
// 3.925123
1 byte
2 bytes
4 bytes
4 bytes
8 bytes
} 1 byte
sh
fl
int main(void)
{
float fl = 3.14159;
char
...
ch = 27;
ch
27
short sh = 1927;
3.14159
1 byte
2 bytes
4 bytes
4 bytes
8 bytes
1927
char
short
int and long
float
double
Order of Operations
1. Parentheses ( and )
2. Unary minus (e.g. 5)
3. *, / and %
4. + and -
gives
you the whole number result.
= 7;
dogs
biscuits = 20;
biscuits_per_dog
20
2
This holds
true for char, int, long and short variables.
std::cout << biscuits_per_dog;
Questions:
What does each line print?
line?
hmm = 7 / 3;
std::cout << "result #1: "<<hmm<< "\n";
hmm = 10 * 5 / 3;
std::cout << "result #2: "<<hmm<< "\n";
hmm = 5 / 6 * 100;
std::cout << "result #3: "<<hmm<< "\n";
// one last thing...
hmm = 0;
b = 10 / hmm;
std::cout << "result #4: "<<b<<"\n";
}
int main(void)
dogs 7
{
C++
allows you to use the modulo operator to
int dogs, biscuits, biscuits_left_over;
biscuits
20
compute
just
the
remainder
part
of
an
integer
dogs = 7;
biscuits_left_over
6
division.
biscuits = 20;
biscuits_left_over = biscuits
dogs;
Instead
of using the / you should use a % sign.
std::cout << biscuits_left_over;
Question:
If we do "N % A", what must
the result be smaller than?
barf = 10 % 3;
std::cout << "barf: "<<barf<< "\n";
barf = 6 % 18;
std::cout << "barf: "<<barf<< "\n";
// be careful!
barf = 6 % 0;
std::cout << "barf: "<<barf<< "\n";
// hmmmmm!?!?
return(0);
}
// careful!!!
#include <iostream>
int main(void)
{
int earWax = 10;
earWax
15
earWax = earWax + 5
5;
cout << You have << earWax
<< gallons of earwax;
}
10
How does
this work?
Next,
C++ stores
the
result
the
variable
First, in
C++
evaluates
specified
toto
the
left
of
everything
the
right
ofthe
theequal
equalsign.
sign.