Skip to main content

Section 8.2 Increment and Decrement

Here is another while loop example; this one displays the numbers 1 to 5:
#include <iostream>
using namespace std;

int main() {
    int i = 1;
    while (i <= 5) {
        cout << i << endl;
        ++i;
    }
}
Assignments like i = i + 1 donโ€™t often appear in loops, because C++ provides a more concise way to add and subtract by one. Recall that, ++ is the increment operator; it has the same effect as i = i + 1. And -- is the decrement operator; it has the same effect as i = i - 1.
We introduced these โ€œshortcut operatorsโ€ back in Sectionย 3.12. There, we said there is no reason you have to use them, but you should learn to recognize them as they are commonly used. Loops are the most important example of this. Programmers will almost always write i++ or ++i

Note 8.2.1.

In C++, there are some differences between i++ and ++i that can make the choice of which to use important. If you are using them as part of a larger expression, the placement of the ++ changes when the increment happens. If you are using ++ on more complex structures, the preincrment version - ++i - can be more efficient than the post increment version. When used on their own with integer variables it does not matter which you use. But it is not a bad habit to get used to writing ++i by default.
If you want to increment or decrement a variable by an amount other than 1, you can use += and -=. For example, i += 2 increments i by 2:
#include <iostream>
using namespace std;

int main() {
    int i = 2;
    while (i <= 8) {
        cout << i;
        i += 2;
    }
    cout << "Who do we appreciate?";
}
And the output is as follows:
2, 4, 6, 8, Who do we appreciate?

Checkpoint 8.2.1.

The super evil villian RePete wants to annoy the city by hacking into the cityโ€™s helper robots and making them repeat everything they say 5 times. However, thereโ€™s an error in his code and now the robots wonโ€™t stop repeating! Can you find the error?
void repeatBot() {
  int n = 0;
  while (n < 5) {
    cout << "Hello, how may I help you? ";
    --n;
  }
}

int main() {
  repeatBot();
}
  • repeatBot can only take one word as an argument.
  • A string is any number of characters or words surrounded by double quotes, not just one word.
  • n is declared to be 0 and 0 is always less than 5.
  • The code doesnโ€™t loop infinitely because of the value n was declared to be.
  • Every time the while loop runs, n is reset to 0, so it will always be less than 5.
  • The initialization of n occurs outside the while loop, so the value of n does not get reset to 0.
  • n is declared to be 0 and we continuously decrement n so it will always be less than 5.
  • Since n starts at 0 and gets smaller, the conditional for the while loop will always be true, and thus the code runs forever.

Checkpoint 8.2.2.

After making some changes to his code, RePete tries again. This time, however, the robots donโ€™t repeat anything! Can you find the new error?
void repeatBot() {
  int n = 0;
  while (n > 5) {
    cout << "Hello, how may I help you? ";
    ++n;
  }
}

int main() {
  repeatBot();
}
  • n > 5 is not a valid conditional, so the while loop doesnโ€™t execute.
  • n > 5 is a boolean statement and thus is a valid conditional.
  • The value of n never gets modified in the while loop.
  • n is incremented in the while loop after the cout statement.
  • In the cout statement, only spaces are printed.
  • input is also printed.
  • The conditional for the while loop is not met.
  • Since n is declared to be 0, 0 is not greater than 5, so the while loop does not execute.
You have attempted of activities on this page.