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.

OperatorMeaning
==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;
}

FAQ: C++ Operators

Common questions about this page.

What is the StudyGrid C++ tutorial?

The StudyGrid C++ tutorial is a full beginner-to-advanced track: syntax, types, input, loops, functions, classes, the STL, templates, maps, and lambdas. Each chapter has copy-and-run examples.

Should I run c++ operators examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn c++ operators in this C++ C++ lesson (C++ Operators).

Is the C++ editor the same as Try Python or Try HTML?

No. Try C++ compiles with g++ at /cpp/try and shows stdout plus compiler messages. Try Python stays at /try. Try HTML stays at /html/try. C++ lessons never open those editors.

Do I need to install a compiler to learn C++?

No. Open a chapter, click Try it in C++, and compile in the browser. You can also download a .cpp file and compile locally with g++.

Where should I start the C++ tutorial?

Start at C++ Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After classes, open C++ Examples, then templates, map, and lambdas. Use Next at the bottom of each chapter.

Is the C++ tutorial free?

Yes. The C++ workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.