Basic Math Operations
Numbers
You already know how to use the print()
function. We used to output text, but what about numbers and mathematical calculations?
Remember to use print()
if you want to display your data. Also, there is no need to use quotes (" "
or ' '
) since we are evaluating a mathematical expression this time.
main.dart
9
1
2
3
4
5
void main() {
print('Two'); // String
print('2'); // String
print(2); // Number
}
12345void main() { print('Two'); // String print('2'); // String print(2); // Number }
Math Operations
Almost no program can do without mathematical calculations, so Dart supports the following mathematical operations:
main.dart
9
1
2
3
4
5
6
7
void 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
}
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 }
Math Expressions
We also can write math expressions.
The sequence of actions will work the same as in mathematics, where 2+2*2 = 6
, because the multiplication is done before the addition.
main.dart
9
1
2
3
void main() {
print(2+2*2); // Output 6
}
123void main() { print(2+2*2); // Output 6 }
Var allt tydligt?
Tack för dina kommentarer!
Avsnitt 1. Kapitel 5
Fråga AI
Fråga AI
Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal