C++ Lesson 4: Increment and Decrement Operators and For Statements

Hello World! It’s been awhile since I’ve put together a programming tutorial but I’m hoping to write several over the summer. With Spring Term coming to an end, and a free summer, I should have time for writing once again.

Previous C++ programming lessons:

Lesson 1: Hello World! Programming with C++ for Beginners

Lesson 1.5: Continuing with Hello World! Programming with C++ for Beginners

C++ Lesson 2: Data Types, Arithmetic Operators, Comparison Operators, and Introduction to If Statements.

C++ Lesson 3: Continuing with If Statements and Introducing Logical Operators

If you’re new to programming, make sure you’re up to speed with previous lessons. Anything going forward assumes you know your way around anything I’ve covered.

Today’s tutorial covers increment and decrement operators, which is just a fancy way of saying a variable plus or minus 1, and the for statement (or for loop).

Increment and Decrement Operators

We use these operators as a simple way to add (increment) or subtract (decrement) 1 to or from a variable. They’re useful because in many programming tasks, changing a variable by 1 is all the “math” you need.

Wiki link for more information

Each operator has 2 ways of being used: prefix or postfix

Example: int number = 0;

  • The preincrement operator looks like ++number
  • The predecrement operator looks like –number
  • The postincrement operator looks like number++
  • The postdecrement operator looks like number–

Prefix changes the value before the variable is used and postfix changes the value after the variable is used.

This process is best explained with visual examples. The following code uses the postincrement operator. I’ve added comments to explain the code.

Screenshot (4)

The results:

Screenshot (5)

Notice when j is initialized to i++, the value at j is 0 which is the current value at i.  The code works from left to right, initializing j to the value currently at i, then adding 1 to i. That is why printing i again now gives us 1 instead of 0. Swapping the plusses for minuses simply produces similar results but with i ending at negative 1.

Below is the same code using the preincrement operator:

Screenshot (10)

Screenshot (9)

Notice the value at j is 1 instead of 0. Working from left to right, j is initialized, 1 is added to i which was 0, and that is the value stored in j.

That’s all there is to it! These come in handy when you start working with loops.

The For Statement (Or For Loop)

By far, the most utilized looping statement in C++ is the for statement. The for statement (also called a for loop) is ideal when we know exactly how many times we need to iterate, because it lets us easily define, initialize, and change the value of loop variables after each iteration.

Learncpp.com link for more information

The general syntax of a for loop:

for (initial-action; loop-continuation-condition; action-after-each-iteration)
//loop body

The for statement is evaluated in 3 parts:

  1. The initial action is evaluated. This is where you usually initialize a variable. The initial action is only evaluated once.
  2. The loop continuation condition is evaluated. If it evaluates to false, the loop is terminated. If it evaluates to true, the statement(s) in the body are executed.
  3. The action after each iteration is evaluated. This is where you use the increment or decrement operators we covered above.


Below are some examples that don’t do anything particularly exciting, but should give you a good idea how for statements operate. The for statement is a powerful tool to have in your toolbelt and will be of great use when working with arrays, sorts, trees, and many programming concepts.

Example 1: Prints the value of i then the sum of i plus 5.

Screenshot (11)

Screenshot (12)

Example 2: If the body of the for loop contains only 1 statement, the braces can be omitted.

Screenshot (13)

Screenshot (14)

Example 3: Variables initialized in the for statement have no value outside the loop. If you want to use a variable outside the loop, initialize it before the for statement. 

Screenshot (15)

Screenshot (16)

Example 4: Trying to access i outside of the loop produces an error.

Screenshot (17)

Screenshot (18)

Example 5: Beware of “off by 1” errors.

You’ve probably noticed I initialize every variable at 0. It’s a common starting point in programming. For example, an array begins at 0. So an array that holds 5 values has values starting at location 0 through location 4. If you were looping through an array with a for statement, you need to stay within those bounds. Off by 1 errors can occur with the wrong comparison operators.

This: i < 5, or i is less than 5…

Screenshot (19)

Screenshot (20)

… is different than i <= 5, or i is less than or equal to 5.

Screenshot (23)

Screenshot (22)

Example 6: You can initialize more than one variable (separated by commas) and use more complicated conditions.

Screenshot (24)

Screenshot (25)

More on statements and flow control at cplusplus.com

That concludes Lesson 3. The next lesson is on generating random numbers.




One comment on “C++ Lesson 4: Increment and Decrement Operators and For Statements”
  1. Dodie Siegel says:

    This is very interesting, You’re a very skilled blogger. I’ve joined your feed and look forward to seeking more of your wonderful post. Also, I’ve shared your web site in my social networks!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s