Menu
Coddy logo textTech

Integers (int)

Part of the Fundamentals section of Coddy's Dart journey — lesson 7 of 94.

Integers (int type) are whole numbers without decimals. Create them with:

void main() {
  int age = 25;
  print(age);
}

Integers don't need quotation marks and can be used in calculations:

void main() {
  int score = 10;
  print(score);
  
  // Increase the score
  score = 15;
  print(score);
  
  // Create another integer
  int bonus = 5;
  
  // Combine integers
  int totalScore = score + bonus;
  print(totalScore);
}
challenge icon

Challenge

Beginner

Create a program that demonstrates the use of integer variables in Dart:

  1. Create an integer variable named age and assign it the value 25
  2. Create another integer variable named yearOfBirth and assign it the value 1998
  3. Create a third integer variable named currentYear and assign it the value 2023
  4. Print all three variables on separate lines with descriptive labels

Cheat sheet

Integers (int type) are whole numbers without decimals:

int age = 25;

Integers can be used in calculations and reassigned:

int score = 10;
score = 15;  // Reassign value

int bonus = 5;
int totalScore = score + bonus;  // Combine integers

Try it yourself

void main() {
  // Declare your integer variables here
  int age = ?;
  int yearOfBirth = ?;
  int currentYear = ?;
  
  // Print the variables with labels
  print("Age: $age");
  print("Year of birth: $yearOfBirth");
  print("Current year: $currentYear");
}
quiz iconTest yourself

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

All lessons in Fundamentals