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:
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.
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.
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:
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.
The general syntax of a for loop:
for (initial-action; loop-continuation-condition; action-after-each-iteration)
The for statement is evaluated in 3 parts:
- The initial action is evaluated. This is where you usually initialize a variable. The initial action is only evaluated once.
- 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.
- 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.
Example 2: If the body of the for loop contains only 1 statement, the braces can be omitted.
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.
Example 4: Trying to access i outside of the loop produces an error.
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…
… is different than i <= 5, or i is less than or equal to 5.
Example 6: You can initialize more than one variable (separated by commas) and use more complicated conditions.
That concludes Lesson 3. The next lesson is on generating random numbers.