C++ Tutorial
C++ Operators
Arithmetic, comparison, and assignment operators work on values. Precedence decides what runs first.
Arithmetic
+, -, *, and / add, subtract, multiply, and divide.% is remainder after integer division. Both sides of % should be integers in this tutorial.
Example
#include <iostream>
using namespace std;
int main() {
cout << 10 + 3 << endl;
cout << 10 - 3 << endl;
cout << 10 * 3 << endl;
cout << 10 / 3 << endl;
cout << 10 % 3 << endl;
return 0;
}10 / 3 is 3 because both operands are int. The fractional part is dropped, not rounded. 10 % 3 is 1: three goes into ten three times, remainder one.
Run the listings in /cpp/try. g++ compiles them there. Do not use the Python editor at/try or the HTML preview at /html/try.
Increment and decrement
++ adds one. -- subtracts one. Written as a statement, n++ and++n both change n by one. This tutorial uses them on their own line so prefix and postfix do not matter yet.
After int n = 5;, the statement n++; leaves n equal to 6.n--; would take it back to 5. The compound-assignment example at the end of this page usesn++ together with +=.
Comparison
Comparisons produce a bool. cout prints that as 1 (true) or0 (false). Use == to test equality. A single = assigns; it does not compare.
| Operator | Meaning |
|---|---|
== | equal |
!= | not equal |
< | less than |
> | greater than |
<= | less than or equal |
>= | greater than or equal |
Example
#include <iostream>
using namespace std;
int main() {
int a = 7;
int b = 4;
cout << (a == b) << endl;
cout << (a != b) << endl;
cout << (a > b) << endl;
cout << (a < b) << endl;
return 0;
}Parentheses around a comparison are required when you send it to cout. Without them,<< and < fight over precedence and g++ will refuse the line.
Logic
&& is and: both sides must be true. || is or: at least one side is true.! is not: it flips true and false. Use parentheses when you mix them so the intended grouping is obvious.
Example
#include <iostream>
using namespace std;
int main() {
int n = 8;
cout << (n > 0 && n < 10) << endl;
cout << (n < 0 || n > 100) << endl;
cout << !(n == 8) << endl;
return 0;
}Compound assignment
+= adds and stores. -=, *=, /=, and %= work the same way. n += 2 means n = n + 2.
Example
#include <iostream>
using namespace std;
int main() {
int n = 10;
n++;
n += 5;
n *= 2;
cout << n << endl;
return 0;
}Precedence
Multiplication and division run before addition and subtraction. 2 + 3 * 4 is 14, not20. Parentheses override the default: (2 + 3) * 4 is 20. When a line looks busy, add parentheses even if the default would match what you meant.
Next: string, where + concatenates text instead of adding numbers.
Worked examples
The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.
Open them in the C++ editor at /cpp/try. Change one measurement and check whether the result still has the right unit.
Physics
Ohm’s law
For many metals at constant temperature, current is proportional to potential difference. V = I R. 20 mA through 220 Ω is 4.4 V.
Keep I in amps, not milliamps, unless you convert. 20 times 220 is 4400, which looks like a voltage until you remember the 20 was milliamps.
V = I R
Ohm’s law is a model for ohmic components. A filament lamp’s resistance rises as it heats.
Example
#include <iostream>
using namespace std;
int main() {
double I = 0.020;
double R = 220.0;
double V = I * R;
cout << "V = " << V << " V" << endl;
return 0;
}Maths
Hours and leftover minutes
Integer division throws away the remainder. 125 / 60 is 2 hours. The leftover 5 minutes is 125 % 60. Together they reconstruct 125.
h = total / 60, min = total % 60
Example
#include <iostream>
using namespace std;
int main() {
int total = 125;
cout << total / 60 << " h " << total % 60 << " min" << endl;
return 0;
}