We wrote variables.cpp,
which shows the basics of using a variable in C++,
including how to declare the variable, set it to a value, and print it out.
Arithmetic with integers
We wrote arithmetic.cpp.
This program prints out tons of text.
We used it as a test, so we could figure out how the arithmetic operators work in C++.
The main takeaways were:
You don't need to know about the lines with "for" for now!
That will show up later in the course;
I just used it so I could quickly create those big lists of printouts.
Addition (+), subtraction (-), and multiplication (*) work just like you'd expect.
Division (/) is a little confusing:
if the answer is a decimal, C++ will ignore anything after the decimal point.
For example, 5/2 becomes 2 and 399/100 becomes 3.
% is called the "modulo operator".
a % b returns the remainder when you divide a by b.
For example, 5%2 becomes 1 and 399%100 becomes 99.
Exponents: the ^ operator is NOT what you'd expect.
You don't need to know anything about it for this class.
If you want to use exponents in your code, you need to do it some other way.
(Lecture will cover this soon enough.)
Here are some Google searches you could try if you're having trouble with today's material.
I'm deliberately trying to choose search terms that you could have thought of yourself!
how to declare variable C++ example or simple example variables C++ -
if you're having trouble writing a basic program that uses variables
C++ division operator - "division is giving weird results sometimes but I don't remember why!"
C++ integer division - followup to previous search if you want more info
C++ percent operator - "what does this one do again?"
C++ exponents or C++ caret operator -
if you want to laugh at some posts written by people who're super confused because ^ does something crazy
Extra stuff to try for fun (not required)
Try to figure out what happens when you use the / or % operators with one/both of the inputs being negative.
You can investigate on your own by writing a program that prints out the results of a bunch of operations;
you could either figure out how to modify my arithmetic.cpp to use negative values
or just write your own program (using a bunch of cout lines instead of the for loops).
Then see if you can figure out the pattern!
You can also read about the answer to the previous question on the internet. The answer would actually be different if you'd looked it up 10 years ago! It seems weird, but the rules of C++ do change (very gradually) over time.