Зміст курсу
Introduction to Dart
Introduction to Dart
Increment and Decrement
Increment
Increment operators are used to increase the value of a variable by 1.
Pre-increment
operator: ++variable
.
The pre-increment operator increments the value of the variable before it is used:
main
void main() { int counter = 0; // Increases the value of `counter` by 1 print(++counter); // 1 }
Post-increment
operator: variable++
.
The post-increment operator increments the value of the variable after it is used:
main
void main() { int counter = 0; // Increases the value of `counter` by 1 print(counter++); // 0 print(counter); // 1 }
Decrement
Decrement operators are used to decrease the value of a variable by 1.
Pre-decrement operator: --variable
:
main
void main() { int counter = 10; // Decrease the value of `counter` by 1 print(--counter); // 9 }
Post-decrement operator: variable--
.
The post-decrement operator decrements the value of the variable after it is used:
main
void main() { int counter = 10; // Decrease the value of `counter` by 1 print(counter--); // 10 print(counter); // 9 }
Increment and decrement operators are often used in loops.
This approach maintains the same functionality while reducing the amount of code we need to write.
Дякуємо за ваш відгук!