• $19 Game Dev' Courses - Use code "FEBUDEMY"

    Video courses

The Unreal Engine Developer Course – Learn C++ & Make Games

Learn and Understand C++

Over 260 video lectures in 44 hours of video!

Learn C++: Beginner to Advanced

Join thousands of successful students!

Having just learnt what C++ game variables are and that they store values that represent the objects in our games it is probably obvious that these values held by our variables will need to change as the game progresses. We can achieve this with C++ operators. Operators in C++ are just like mathematical operators and many of them are the same symbol. So if you coped with junior school math you will have no problem with this tutorial. Let’s start by looking at a list of some of the frequently used C++ operators.

About this tutorial

Skill level 1
Time to read: 10 minutes

New Concepts:

  1. C++ operators
  2. assignment
  3. increment
  4. decrement

C++ operators

Here is a list of some of the most useful C++ operators that allow us to do things with our game’s variables. The assignment operator which we have already seen ( =): This makes the variable to the left of the operator the same as the value to the right. For example look at other ways we might use the C++ assignment operator:

// Player gets a new high score
hiScore = score;


// Set the score to 100
score = 100;

The addition operator ( +): This adds the values on either side of the operator. It is usually used in conjunction with the assignment operator, such as:

// Add to the score when an alien is shot
score = aliensShot + wavesCleared;


// Add 100 to whatever the score currently is
score = score + 100;
  • Beginning C++ Game Programming

    Beginning C++ Game Programming

    This book is all about offering you a very fast and fun introduction to the world of game programming and C++. It will begin by teaching you the programming basics such as variables, loops, and conditions. In this book there is very little theorizing. If you want a deep discussion of all the ins and outs of C++ concepts look elsewhere. If you are starting from a knowledge of zero C++ and want to build great games- fast, take a look at this book.
    • Setup a C++/SFML/Visual Studio development environment
    • Build a fast, fun Timber clone after learning basic C++
    • Discover the fundamentals of C++ object oriented programming and build a multi-level, top-down 2D, scrolling zombie survival/shooter
    • Explore some advanced C++ concepts and make a side scrolling, co-op split-screen multiplayer platform game with directional sound, particle effects and your very own level designs!
    Amazon USA
    Amazon UK
    Amazon CA
    Our Store (US only)
    C++ for Game programmers book

    C++ For Game Programmers

    Already know the basics of C++ but want to dig much deeper in an entirely game-focussed context? C++ for Game Programmers is that book. This best selling title supplements your knowledge by putting everything into perspective from a game-development point of view. The book points out the most effective C++ practices and steers you away from the potentially dangerous ones. It describes common C++ techniques to solve specific problems most game developers face.
    • From a gaming perspective, learn about inheritance, performance, memory management, and STL to object creation, object serialization, and scripting languages
    • Explore C++ Design Patterns and high-level problem-solving constructs that are most commonly found in games
    • Discover custom Structures and algorithms, which provides a detailed hands-on discussion of using C++ to create efficient solutions to difficult problems in a highly structured manner
    Amazon USA
    Amazon UK
    Amazon CA
    Game Programming Patterns book

    Game Programming Patterns

    If you are serious about writing high quality, reusable, efficient code using C++ or indeed any other language, then inevitably you will need to seriously hone your knowledge of game programming patterns. If you don't know what game programming patterns are they are simply the solutions to design problems. If you have programmed even a basic game you will have noticed that the code gets long and hard to manage, very quickly. The more features you add the more sprawling and messy your code gets. In addition to usability/reuse-ability, our game code needs to be efficient enough for the game to run smoothly. That's what this book will do for you.
    • Learn to write a robust game loop, how to organize your entities using components, and take advantage of the CPUs cache to improve your performance
    • Discover how to organize your entities using components, and take advantage of the CPUs cache to improve your performance.
    • Never again wonder what is the "best" way to organize your game code
    Amazon USA
    Amazon UK
    Amazon CA
    Our Store (US only)

Notice that it is perfectly acceptable to use the same variable on both sides of an operator.
The subtraction operator ( -): This subtracts the value on the right side of the operator from the value on the left. It is usually used in conjunction with the assignment operator, perhaps:

// Uh oh lost a life
lives = lives - 1;


// How many aliens left at end of game
aliensRemaining = aliensRemaining - numberAliesDestroyed;

The division operator ( /): This divides the number on the left by the number on the right. Again, it is usually used with the assignment operator, like this:

// Make the remaining hit points lower based on the value in swordLevel
hitPoints = hitPoints / swordLevel;


// Give player something back for recycling a block
recycledValueOfBlock = originalValue / .9;

Obviously, in the previous example, the variable recycledValueOfBlock will need to be of the type float to be multiplied by the value .9.
The multiplication operator ( *): Perhaps unsurprisingly, multiplies variables and numbers, such as:

// answer is equal to 100 - of course
answer = 10 * 10;


// biggerAnswer = 1000 - duh
biggerAnswer = 10 * 10 * 10;

As an aside have you ever wondered how C++ got its name? C++ is an extension of the C language. It’s inventor Bjarne Stroustrup originally called it “C with classes” but the name evolved. If you are interested read the C++ story.

The increment operator ( ++): This is a really neat way to add 1 to the value of one of our game’s variables.


// Add one to myVariable
myVariable = myVariable + 1;

is the same as this:

// Much neater and quicker
myVariable ++;

The decrement operator ( --): You guessed it, a really neat way to subtract 1 from something.

playerHealth = playerHealth -1;

is the same as:

playerHealth --;

What next

Perhaps surprisingly we know enough C++ to actually get some game-like action in a real C++ program. If you would like to practice what you have learned in this and the previous tutorial about game variables try the mini-project C++ game variables demo which uses the SFML C++ game library. In the next tutorial, Checking for conditions in our game we will try using some declarations, assignments, and operators. When we bundle these elements together into some meaningful syntax, we call it an expression.