Increment and Decrement
Increment (++) and decrement (--) operators are commonly used to increase or decrease a variable's value by 1. They are especially useful in loops for managing counters.
Increment Operators
- Pre-increment (
++variable): increases the value before usage; - Post-increment (
variable++): increases the value after usage.
Usage in a loop:
main.dart
12345void main() { for (int i = 0; i < 5; ++i) { print(i); // Outputs: 0, 1, 2, 3, 4 } }
Decrement Operators
- Pre-decrement (
--variable): decreases the value before usage. - Post-decrement (
variable--): decreases the value after usage.
Usage in a loop:
main.dart
12345void main() { for (int i = 5; i > 0; --i) { print(i); // Outputs: 5, 4, 3, 2, 1 } }
Task
Complete the code so that it prints all numbers from 2 to 10 inclusive.
main.dart
12345void main() { for (int i = 2; i <= 10; ___) { print(i); } }
Use an increment operator.
main.dart
12345void main() { for (int i = 2; i <= 10; i++) { print(i); } }
¡Gracias por tus comentarios!
Pregunte a AI
Pregunte a AI
Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla
What would the code look like to print numbers from 2 to 10 using an increment operator?
Can you explain the difference between pre-increment and post-increment in this context?
Could you provide a hint for how to start the loop?
Awesome!
Completion rate improved to 4.55
Increment and Decrement
Desliza para mostrar el menú
Increment (++) and decrement (--) operators are commonly used to increase or decrease a variable's value by 1. They are especially useful in loops for managing counters.
Increment Operators
- Pre-increment (
++variable): increases the value before usage; - Post-increment (
variable++): increases the value after usage.
Usage in a loop:
main.dart
12345void main() { for (int i = 0; i < 5; ++i) { print(i); // Outputs: 0, 1, 2, 3, 4 } }
Decrement Operators
- Pre-decrement (
--variable): decreases the value before usage. - Post-decrement (
variable--): decreases the value after usage.
Usage in a loop:
main.dart
12345void main() { for (int i = 5; i > 0; --i) { print(i); // Outputs: 5, 4, 3, 2, 1 } }
Task
Complete the code so that it prints all numbers from 2 to 10 inclusive.
main.dart
12345void main() { for (int i = 2; i <= 10; ___) { print(i); } }
Use an increment operator.
main.dart
12345void main() { for (int i = 2; i <= 10; i++) { print(i); } }
¡Gracias por tus comentarios!