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
|
Decrement a variable | variable = variable - change |
timeRemaining = timeRemaining - 1
|
true
or false
.
Using Scratch
To increment or decrement a variable in Scratch, follow these steps:- Go to the Data blocks code tile category.
- Select the
change <em>variable name</em> <em>by </em><em>value</em>
code tile and drag it into your program. - Press the tab beside the variable name and select the variable whose value you want to change.
- 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.
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>