课程: Complete Guide to C++ Programming Foundations
免费学习该课程!
今天就开通帐号,24,100 门业界名师课程任您挑!
Compound assignment operators - C++教程
课程: Complete Guide to C++ Programming Foundations
Compound assignment operators
- [Instructor] Compound assignment operators combine an arithmetic operation with an assignment, making the code more concise. We'll cover a few of them in this example, addition, subtraction, multiplication, division, and remainder. For this example, suppose we are working on a survival horror video game and we need to keep track of the player's ammo and the number of incoming zombies. We'll use compound assignment operators to update these values during gameplay. Let's start with the initial code setup. In this example, we have two integer variables, ammo and incoming zombies, initialized with values 30 and 20 respectively. First, I'm printing the initial values of ammo and incoming zombies in line 11 and 12, and now it's time to fill in the blanks. Let's use the addition assignment operator to increase the ammo count when the player finds some additional ammo. The addition assignment operator is written as += and it adds the value at the right to the current value of the variable…