Helping Kids with Coding For Dummies
Book image
Explore Book Buy On Amazon
Your young coder can write code that changes the value of a variable under certain conditions as the program executes. A common way to change a variable value is to increment it. To increment a variable means to increase it by the same amount at each change. For example, your coder may increment a scoring variable by +2 each time a basketball goal is made. Decreasing a variable in this way is known as decrementing the variable value. Your coder may decrement a timer by –1 each time one second on the clock elapses.

Using pseudocode

The general format for incrementing or decrementing a variable value is as follows:
Action Pseudocode Examples
Increment a variable variable = variable + change score = score + 1

numTwins = numTwins + 2

Decrement a variable variable = variable - change timeRemaining = timeRemaining - 1

cost = cost - 5

Boolean variables aren't incremented nor decremented. A Boolean variable exists only in one of two states: true or false.

Using Scratch

To increment or decrement a variable in Scratch, follow these steps:
  1. Go to the Data blocks code tile category.
  2. Select the change <em>variable name</em> <em>by </em><em>value</em> code tile and drag it into your program.
  3. Press the tab beside the variable name and select the variable whose value you want to change.
  4. Type a number for the increment or decrement in the value field. Use a positive number to increment the value. Use a negative number to decrement the value.
variable value coding Changing a variable value in Scratch.

Using Python

To increment a variable value in Python, type the code

<em>variableName = variableName + value</em>

at the location in your code where you want to change the value.

To decrement a variable value Python, type the code

<em>variableName = variableName – value</em>

at the location in your code where you want to change the value.

Using JavaScript

To increment a variable value in JavaScript, type the code

<em>variableName = variableName + value;</em>

at the location in your code where you want to change the value.

To decrement a variable value in JavaScript, type the code

<em>variableName = variableName – value;</em>

Using Java

To increment a variable value in a Java program, type the code

<em>variableName = variableName + value;</em>

at the location in your code where you want to change the value.

To decrement a variable value in a Java program, type the code

<em>variableName = variableName – value;</em>

About This Article

This article is from the book:

About the book authors:

Camille McCue, PhD, is Director of Curriculum Innovations at the Adelson Educational Campus in Las Vegas where she leads the Startup Incubator, teaches STEM, and kickstarts K-12 learning initiatives. Sarah Guthals, PhD, co-founded an ed-tech company and now continues to build technology for kids to learn, create, and share safely online. She loves to teach teachers how to teach coding in the classroom.

This article can be found in the category: