Increment/Decrement
Part of the Fundamentals section of Coddy's Java journey — lesson 17 of 73.
Increment and decrement operators are used to increase or decrease the value of a variable by 1. These operators are widely used in programming, especially in loops and counters.
The increment operator is represented by two plus signs ++, and the decrement operator is represented by two minus signs --.
For example, to increment a variable named count, you can use the increment operator like this:
int count = 5;
count++; // count is now 6Similarly, to decrement a variable named value, you can use the decrement operator like this:
int value = 10;
value--; // value is now 9This is equivalent to this:
count = count + 1 // increment by 1
count = count - 1 // decrement by 1Challenge
BeginnerYou are given a code with initialization of count. (Don't delete this line!)
Your task is to add the following operations, in this order:
- Use the increment operator (
++) four times to add 4 to count - Use the multiplication operator (
*) to multiply count by 2 - Use the decrement operator (
--) once to subtract 1 from count
Cheat sheet
Increment and decrement operators are used to increase or decrease the value of a variable by 1.
The increment operator ++ increases a variable by 1:
int count = 5;
count++; // count is now 6The decrement operator -- decreases a variable by 1:
int value = 10;
value--; // value is now 9This is equivalent to this:
count = count + 1 // increment by 1
count = count - 1 // decrement by 1Try it yourself
public class Main {
public static void main(String[] args) {
int count = 0;
// Type your code below
// Don't change the line below
System.out.println("count = " + count);
}
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsModulo OperatorIncrement/DecrementPost Increment/DecrementArithmetic ShortcutsComparison OperatorsString Comparison5Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Logical Operators Part 43Variables Part 2
ConstantsNaming ConventionsRecap - Initialize VariablesType Casting Part 1Type Casting Part 26Decision Making
If StatementIf - ElseSwitch StatementTernary OperatorRecap - If ElseNested If - Else