Menu
Coddy logo textTech

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 icon

Challenge

Easy

Create a program that demonstrates the increment (++) and decrement (--) operators in Dart:

  1. Declare an integer variable named score with an initial value of 10
  2. Print the initial score with the label: Initial score: 10
  3. Use the pre-increment operator (++score) and store the result in a variable called newScore
  4. Print the new score with the label: After pre-increment: 11
  5. Print the current value of the original score variable with the label: Current score: 11
  6. Use the post-decrement operator (score--) and store the result in a variable called postDecrementResult
  7. Print the post-decrement result with the label: Post-decrement result: 11
  8. 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 1

Prefix 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 6

Try 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");
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals