We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Math Operators

All the same operators you'd expect exist in C:

x + y;
x - y;
x * y;
x / y;

If you're coming from Python, +=, -=, *=, /= are all the same.

In addition, there are also the ++ and -- operators:

x++; // += 1
x--; // -= 1

The name of C++ is a bit of a joke by the creator, it's meant to be "incremented C" or "better C".

These increment (++) and decrement (--) operators can be used in two forms: postfix and prefix.

Postfix (x++ or x--): The value of x is used in the expression first, and then x is incremented or decremented. For example:

int a = 5;
int b = a++; // b is assigned 5, then a becomes 6

Prefix (++x or --x): x is incremented or decremented first, and then the new value of x is used in the expression. For example:

int a = 5;
int b = ++a; // a becomes 6, then b is assigned 6

I generally avoid prefix operators. If I want to increment a variable but keep the original value, I do that in two steps. Postfix is more common, especially in loops, which we'll get to.

Assignment

Complete the snek_score function in exercise.c. Sneklang is unique™ in that its toolchain gives developers a "project score" that's dependent on how maintainable and "high quality" their codebase is. The larger the score, the harder it is to work in the project. The score is calculated as follows:

Tip

You can convert an integer to a float by casting it:

int x = 5;
float y = (float)x;