How are you guys? After several updates to some of the functionality of this web, I decided that the conversation of the first party becomes me, and the conversation of the second party is you, hehe, it's not good because it's impolite. Okay, let's continue to the topic.
Understanding
Surely the readers here know, if the basic arithmetic operations are addition, subtraction, multiplication and division, and there are also more advanced ones, such as percentage manipulation , square root s, exponents, logarithmic functions, and so on.
When we code using a programming language that is static in data type, we cannot just give random division/multiplication. Because it will generate error later. So, pay close attention to the following operations. And if you want to practice, go straight to dartpad yes
Cheat sheet
Operator | Explanation | + | Plus (plus) |
- | Minus (less) |
/ | Divide |
* | Times |
~/ | Division yields integer |
% | modulus |
++ | increment (+1) |
-- | decrement (- 1) |
Discussion
Add
Addition with operator (+). Maybe you will immediately understand.
example 5+5 = ? the answer must be 10.
Let's go to code:
void main() {
vara = 5;
varb = 5;
varc = a+b;
print(c);
}
Result
10
Less
Subtraction with operator (-). Maybe you will immediately understand too.
example 5-5 = ? the answer is definitely 0.
Sample code
void main() {
vara = 5;
varb = 5;
var c = a-b;
print(c);
}
The result is
0
Divide
Divide with the operator (/).
example 20/10 = ? the answer is definitely 2
sample code
void main() {
vara = 20;
varb = 10;
varc = a/b;
print(c);
}
The result
2
Please be careful with data types, case examples
void main() {
vara = 20;
varb = 9;
int c = a/b;
print(c);
}
The result is error
Multiplication
Multiply with operator (*).
example 5*5 = ? The result is 25
sample code
void main() {
vara = 5;
varb = 5;
varc = a*b;
print(c);
}
Result
25
Division returns an integer
operator (~/)
example 29/5 = ? The result is 5. Why? Because of rounding down, or you could say clearing decimal
example code
void main() {
vara = 29;
varb = 5;
int c = a~/b;
print(c);
}
The result is
5
Modulus
operator (%)
Modulus is the remainder of the quotient.
example 25 % 5 = ? the result is 0
example code
void main() {
vara = 25;
varb = 5;
int c = a%b;
print(c);
}
Result is
0
Increment
operator (++) p>
It is added sequentially.
example:
void main() {
int i = 1;
i++;
print(i);
}
The result is
2
why 2? because (++) adds one number. So basically i above is 1 + 1
2nd example
void main() {
int i = 1;
i++;
i++;
print(i);
}
Well, now there are 2 i++, what's the result? yep.. the result is
3
essentially 1+1+1 = 3
decrement
operator (--)
Represents subtraction sequentially.
example:
void main() {
int i = 1;
i--;
print(i);
}
The result is
0
why 0? because (--) subtracts one number. So the point i above is 1 - 1
example 2nd
void main() {
int i = 1;
i--;
i--;
print(i);
}
Well, now there are 2 i--, what's the result? yep.. the result is
-1
essentially 1-1-1 = -1
Okay, thanks. Next we will discuss other operations. Keep here dude!