We’ve been using syntax like counter = counter + 1, which works, but as you’ll increasingly find with Ruby, there’s always another way.

A shortcut is to use an assignment operator. You already know one assignment operator: =, which sets a variable. You can update a variable with additional assignment operators, however, which include +=, -=, *=, and /=. For instance, when you type

counter += 1

You’re telling Ruby: “Add 1 to counter, then assign that new value back to counter.” This provides a succinct way of updating variable values in our programs.

Some languages have the increment operators ++ and -- (which also add or subtract 1 from a value), but Ruby does not. You’ll have to make do with += and -=!



The loop from the first exercise is in the editor. Rewrite it using the appropriate assignment operator.

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?