Increment and Decrement
Part of the Fundamentals section of Coddy's Dart journey — lesson 19 of 94.
In Dart, increment (++) and decrement (--) operators increase or decrease a variable's value by 1.
void main() {
int count = 5;
count++; // Increases count by 1
print('After increment: $count');
}void main() {
int score = 10;
score--; // Decreases score by 1
print('After decrement: $score');
}void main() {
int a = 5;
int b = 5;
// Prefix increment: increment first, then use the value
int resultA = ++a;
// Postfix increment: use the current value, then increment
int resultB = b++;
print('a: $a, resultA: $resultA');
print('b: $b, resultB: $resultB');
}Challenge
EasyCreate a program that demonstrates the increment (++) and decrement (--) operators in Dart:
- Declare an integer variable named
scorewith an initial value of 10 - Print the initial score with the label:
Initial score: 10 - Use the pre-increment operator (++score) and store the result in a variable called
newScore - Print the new score with the label:
After pre-increment: 11 - Print the current value of the original score variable with the label:
Current score: 11 - Use the post-decrement operator (score--) and store the result in a variable called
postDecrementResult - Print the post-decrement result with the label:
Post-decrement result: 11 - Print the final score with the label:
Final score: 10
Your output should match the expected format exactly.
Cheat sheet
Dart provides increment (++) and decrement (--) operators to increase or decrease a variable's value by 1.
Basic usage:
int count = 5;
count++; // Increases count by 1
count--; // Decreases count by 1Prefix vs Postfix:
int a = 5;
int b = 5;
// Prefix: increment first, then use the value
int resultA = ++a; // a becomes 6, resultA is 6
// Postfix: use current value, then increment
int resultB = b++; // resultB is 5, then b becomes 6Try it yourself
void main() {
// Declare your score variable here
int score = ?;
// Print initial score
print("Initial score: $score");
// Use pre-increment operator
int newScore = ?;
// Print new score and current score
print("After pre-increment: $newScore");
print("Current score: $score");
// Use post-decrement operator
int postDecrementResult = ?;
// Print post-decrement result and final score
print("Post-decrement result: $postDecrementResult");
print("Final score: $score");
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 2
Comparison OperatorsLogical ANDLogical ORLogical NOTType Test OperatorsRecap - Making Comparisons7Working with Strings
String ConcatenationString InterpolationMulti-line StringsString PropertiesBasic String Methods10Collections - Maps Basics
What are Maps?Creating a MapAccessing Values by KeyKey-Value PairsGetting Map SizeGetting KeysGetting ValuesChecking if a Key Exists13Null Safety In Depth
Understanding NullNullable TypesNon-Nullable TypesNull Assertion OperatorLate InitializationRecap - Handling Null Safely16Fundamentals Challenges
Challenge: List of calculationChallenge: Sum of numbersChallenge: Find product2Variables and Basic Data Types
What are Variables?StringsIntegers (int)Doubles (double)Booleans (bool)Type Inference with 'var'Final VariablesConstant VariablesNaming ConventionsBasic Null SafetyRecap - Declaring Variables8Control Flow - Loops
The 'for' LoopThe 'while' LoopThe 'do-while' LoopUsing 'break' in LoopsUsing 'continue' in LoopsRecap - Repeating Code3Operators Part 1
Arithmetic OperatorsInteger DivisionModulo OperatorIncrement and DecrementAssignment ShortcutsRecap - Simple Calculations6Control Flow - Decision Making
The 'if' StatementThe 'else' StatementThe 'else if' StatementRecap - Simple DecisionsNested 'if' StatementsThe 'switch' Statement9Collections - Lists Basics
What are Lists?Creating a ListAccessing by IndexGetting List LengthAdding ElementsRemoving ElementsChecking if a List is EmptyIterating Over a List12Functions Advanced
Optional Positional ParametersNamed ParametersRequired Named ParametersDefault Parameter ValuesRecap - Function Parameters15Project: Simple Calculator
Setting UpDeclaring Number