Beginners C++ - Lesson 2
Instructor: Paul C. Benedict,
Jr. (PCC PaulB)
Color coded text:
black
New things to Remember
blue C++
keywords (do, while)
green
C++ preprocessor directives (#include, #define)
Section 2.1
From last time, we learned how to declare numbers and do math with them. For
instance, lets declare an integer variable we call x and set it to five:
Example 2.1.1
int x = 5;
This called initialization! We call it
initialization because x was set to five when it was created (The int
means create an integer variable. It is not an abbrevation for initialization).
Now, if we left off the value (called the inital
value), x would contain junk. What do we mean when we say it contains
junk? When x was allocated (memory that
was set aside for x), whatever value that was in memory previously before
x is now the value of x. We call this value of x undefined
because we did not define the value, and since the value is undefined, the
variable is uninitialized. You cannot do
anything useful with the variable until it was initialized. You do not have to
initialize variables when you declare
them.variable before using it.
Example 2.1.2
Here you declare two variables, then assign them later.
int x;
int y;
x = 5;
y = x * 3;
First x and y is declared (and has an undefined value because
it is uninitalized). Then x is initialized to the value five. They y
is initialized to the value of x times 3, which is fifteen.
Example 2.1.3
What happened if we did this?
int x;
x = x + 1;
What is the value of x? We don't know. x is whatever it was
plus one now. The value is completely meaningless to us. One of the most common bugs
in programming is not initializing variables before you use it. (On a side note,
a bug is an "unlisted feature" in a program that makes the program not
work they way you want it - the last example shows this.).
Section 2.2
These are the current operators we know so far from our last lesson:
Addition
+
Subtraction
-
Multiplication
*
Division
/
Modular Division (Remainder) %
We are now going to learn some more operators to work with numbers:
Increment
++
Decrement
--
Addition Assignment
+=
Subtraction Assignment
-=
Multiplication Assignment
*=
Division Assignment
/=
Remainder Assignment
%=
Example 2.2.1
The increment operator ++ increments a variable by one.
The decrement operator -- decrements a variable by one.
int x = 10;
x++;
x = x + 3;
x--;
First x is initialized to the value ten.
Then it is incremented to the value eleven.
Next we add three to it.
Then we decrement it.
The final value for the variable x is now thirteen.
Example 2.2.2
Now, with the += operator we can simply this even more:
int x = 10;
x++;
x += 3;
x--;
Wow! Look at that!
x += 3;
Is an equivalent shorthand for
x = x + 3;
Example 2.2.3
So, lets see how we can reduce our typing on these next four statements:
x = x + 1;
x = x - 3;
x = x * 2;
x = x / 3;
They reduce to:
x += 1;
x -= 3;
x *= 2;
x /= 3;
The C++ makers were nice enough to save some typing for us. You will always
see these shorthands in professional code, so always keep these in the back of
your mind. Once you begin programming, you will become very fluent in the
language and begin using it naturally.
Section 2.3
Now we are going to learn the all important assignment precedence and
associativity rules. In math, do you remember the "Pretty Please My
Dear Aunt Sally" (PPMDAS) phrase? That was a saying to remember the precedence
rules of our operators:
Parentheses
Highest
Powers
Multiplication, Division
Addition, Subtraction
Lowest
Example 2.3.1
int x = 2 + 5 * 3;
According to precedence, multiplication comes first before addition. So we
multiply five and three, then add two. x now contains seventeen. We could
have thrown in parentheses and got a different answer:
Example 2.3.2
int x = (2 + 5) * 3;
Parentheses has the highest precedence here, so we do the inside of them
first. Two plus five is seven, then we multiply by three and get twenty-one.
In C++, there are many other operators we must deal with. We will introduce
two other operators to you (one which you should already be familiar with). We
have the = operator (which we call the assignment
operator), and we have the == operator (which we call the equality
operator).
The = operator will assign what is on the right-hand side to the left-hand
side of the equal sign.
The == operator will test to see if what is on the right-hand side is equal
to the left-hand side. If they are equal, the == operator returns to use the
value of true (which
is 1). If they are not equal, it returns the value of false
(which is zero).
These two new operators are also on the precedence list, and now we can
expand the list to this:
( )
Highest
==
=
*, /
+, -
Lowest
Example 2.3.3
Which means, if we did a statement like this:
int x = 2;
int y = x == 3;
x would first be assigned two. Then for y, first x would
be compared to three. Since they are not equal, we get a value of false
(which is zero), and assign that to y. y is now zero.
Example 2.3.4
To complicate things a tad-bit further, say that we did this:
int x = 0;
int y = x == 3 == 5;
First x is assigned zero. Then for y, we first compare three
against five. Since they are not equal, we get back the value false
(which is zero). We then compare the value of zero to the value of x.
They are equal so it returns the value of true
(which is one) and assign it to y. y now equals one.
Example 2.3.5
For fast initialization of many variables, we can even do this:
int x = 3;
int y;
int z;
z = y = x;
All variables now equal three.
Section 2.4
Next up in the wonderful world of C++..........
boolean algebra (created by Mr. Boole)
You will now learn how to do more algebra in binary! Remember our great
lesson from last time? We'll for the ones who asked why do I have to know this
stuff, it is because working with bits of a number is more important than the
number itself. I can guarantee that if you can't do boolean algebra, you will
not have a fun time finding a job as a C++ programmer. Boolean algebra is a
fundamental component of programming - rather it is C++, straight C, Visual
Basic, Pascal, etc. This might be a confusing part for non-programmers, so we
are going to spend the rest of the lesson nailing down the basics (What's the
use of a lesson if you left confused?).
The great geniuses of C++ came up with the bool
data type (::ding, ding, ding::: New Data Type!). This is even a brand new type
for fellow C programmers who moved to C++ (Remember C++ is just a superset of C,
which means it adds on to C. And if you looked at the name "C++", it
is C with the increment operator. Pretty cool, huh?).
Now, if you don't know, the bool
data type can hold only two values: true
and false.
If you want numerical value for these constants,
constants are values that never change, use this simple rule:
true = one
false = zero
Example 2.4.1
bool x = true;
bool y = (false == x);
Since == has higher precedence over =, we do test for equality first.
Since false does not equal x, it evaluates the value false which
is set to y.
When we mean "returns", just think of it as another meaning for the
word "equals".
Example 2.4.2
bool x = false;
bool y = true;
bool z = (true == y) ==
true;
First we test true
against y which evaluates to true.
We than compare true against true
which is true.
Example 2.4.3
bool x = false;
bool y = false;
z = (false == false) ==
true;
false is
first compared to false,
which returns true.
The returning true than
is compared against true,
which returns true.
The returning true is
now set to z.
Section 2.5
There are four main operations we use in logic:
NOT !
AND
&&
OR
||
XOR ^
NOT takes the opposite of a boolean value. If a variable is true,
the NOT of it is false.
Think of it in English: if a value is not true,
it is false.
AND takes two variables and determines if both are true.
If both are, AND evaluates to true.
If either one or both are false,
it evaluates to false.
OR takes two variables and determines if any of them are true.
If just one or both are, OR returns true.
Otherwise, it is false.
XOR won't be discussed in-depth. XOR stands for Exclusive-OR. It only returns
true when only one value is true.
If both are the same value (either both true
or both false,
it evaluates to false).
Now we have to add these (yes, once again) to our precedence list:
( )
Highest
!
==
=
&&
||
*, /
+, -
Lowest
Example 2.5.1
bool x = true;
bool y = false;
bool z = x && y;
x and y is ANDed (&&) together. Since y is false,
it makes the entire expression false,
and z is set to false.
Example 2.5.2
bool x = false;
bool y = !true == x
&& !x;
!x equals true.
ANDed with x which evaluates false
And that evaluated false
compared to !true
evaluates to true.
y evaluates true.
New terms to remember
Allocate
And
Boolean
Bugs
Constant
Decrement
false
Initialization
Intial value
increment
Not
Or
Precedence
Undefined
Uninitalized
true
Xor
Review problems
01. What is wrong with this code? (find the bug)
int x;
int y = 2;
int z;
z = y + 1;
y = z + y;
z = z + x;
02. What values do we get for all the variables?
int a = 3;
int b = 45;
int c = 1;
bool d = c == 1;
bool e = false == false == 1;
03. Why won't this code compile?
r = 44;
04. What do we get in this?
bool x = true;
bool y = false;
bool z = ((x == y) == y);
05. What do we get in this?
bool x = false;
bool y = true;
bool z = false == true == false == x;
We will be using these variables in the following examples:
bool p = true;
bool q = false;
bool r = true;
06. bool a = !true;
07. bool b = (false == !false)
&& true;
08. bool c = q && r == false;
09. bool d = (p && q) || r;
11. bool e = (r && r) &&
q;
12. bool f = p && (q || r);
13. bool g = !q && p;
14. bool h = !(q || p);
15. bool k = p && !q || r
|| p && !p
ML>
|