Learn

Often we need to update a variable in our program. We can do so by modifying that variable using an arithmetic expression, then re-saving it to the same variable name:

int apple = 0; apple = apple + 1; Console.Write(apple); // prints 1

Programmers are always looking for shortcuts. For instance, we can condense the same program above using the shorthand ++. The combined addition signs represent the idea of incrementing by one. We can do the same with the subtraction symbol --.

// a shorter way to do the same thing int apple = 0; apple++; Console.Write(apple); // prints 1

If we want the amount to increment by another value, say 3, we would do the following:

int apple = 0; apple += 3; // is the same as apple = apple + 3 Console.Write(apple); // prints 3

Again, if we want to decrement, you would do -=3.

Ultimately, programmers disagree on this, so everyone does it differently! On Codecademy, we prefer clarity over conciseness, so we’ll use the long form (apple = apple + 3) except for incrementing by one (apple++).

Instructions

1.

Ever heard of the phrase, “two steps forward, one step back?” It means that you can make progress in some task, but also might suffer some setbacks. It’s also a great way to illustrate the concept of incrementing and decrementing!

Start by creating a variable named steps. Set it to 0.

2.

Two steps forward

Next, increment the value of steps by two and resave this new value to steps.

3.

One step back.

Now, decrement the variable steps by 1.

4.

Print the final value of steps to the console.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?