Basic Math Operations
You already know how to use the print() function to display text. It can also be used to output numbers and perform mathematical calculations. Use print() whenever you want to display data. Do not use quotes (" " or ' '), since this time you’re evaluating a mathematical expression.
main.dart
12345void main() { print('Two'); // String print('2'); // String print(2); // Number }
In Dart, if a number is written in quotes ("123"), it is treated as text (a string) rather than an actual number. This means you can't perform math operations like addition or multiplication on it.
Almost no program can do without mathematical calculations, so Dart supports the following mathematical operations:
main.dart
1234567void main() { print(1+2); // Adding numbers print(9-3); // Subtracting numbers print(4/2); // Division of numbers print(4*2); // Multiplication of numbers print(5%2); // Get the remainder from the whole division of 5 by 2 }
You can also write mathematical expressions. The order of operations follows standard math rules, so 2 + 2 * 2 = 6 because multiplication is performed before addition.
main.dart
123void main() { print(2+2*2); }
The same rule applies to brackets, expressions inside parentheses are calculated first, just like in regular mathematics.
Takk for tilbakemeldingene dine!
Spør AI
Spør AI
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår
Awesome!
Completion rate improved to 4.55
Basic Math Operations
Sveip for å vise menyen
You already know how to use the print() function to display text. It can also be used to output numbers and perform mathematical calculations. Use print() whenever you want to display data. Do not use quotes (" " or ' '), since this time you’re evaluating a mathematical expression.
main.dart
12345void main() { print('Two'); // String print('2'); // String print(2); // Number }
In Dart, if a number is written in quotes ("123"), it is treated as text (a string) rather than an actual number. This means you can't perform math operations like addition or multiplication on it.
Almost no program can do without mathematical calculations, so Dart supports the following mathematical operations:
main.dart
1234567void main() { print(1+2); // Adding numbers print(9-3); // Subtracting numbers print(4/2); // Division of numbers print(4*2); // Multiplication of numbers print(5%2); // Get the remainder from the whole division of 5 by 2 }
You can also write mathematical expressions. The order of operations follows standard math rules, so 2 + 2 * 2 = 6 because multiplication is performed before addition.
main.dart
123void main() { print(2+2*2); }
The same rule applies to brackets, expressions inside parentheses are calculated first, just like in regular mathematics.
Takk for tilbakemeldingene dine!