Created
July 29, 2025 13:35
-
-
Save mukhtharcm/bd5e4089f68df37718e13cef93801673 to your computer and use it in GitHub Desktop.
Dart basics: Lists & For Loops
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void main() { | |
// A List holds an ordered collection of items. | |
// The type of item is specified in the angle brackets < >. | |
// A List of Strings | |
List<String> shoppingList = ["Apples", "Milk", "Bread"]; | |
print("Shopping List:"); | |
for (String item in shoppingList) { | |
print("- $item"); | |
} | |
print("---"); | |
// A List of integers (whole numbers) | |
List<int> scores = [95, 82, 87, 74]; | |
int totalScore = 0; | |
print("Exam Scores:"); | |
for (int score in scores) { | |
print("Score: $score"); | |
totalScore = totalScore + score; | |
} | |
print("Total score is: $totalScore"); | |
print("---"); | |
// A List of doubles (decimal numbers) | |
List<double> expenses = [150.50, 45.00, 230.75, 80.00]; | |
double totalExpenses = 0; | |
print("Monthly Expenses:"); | |
// A 'for' loop goes through each item in the list one by one. | |
for (double expense in expenses) { | |
print("Processing expense: $expense"); | |
totalExpenses = totalExpenses + expense; // Add the current expense to the total | |
} | |
print("Total expenses for the month: $totalExpenses"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment