Java Increment and Decrement Operators Explained

Day 10 - 🚀 Increment and Decrement Operators in Java Understanding increment (++) and decrement (--) operators is essential for every Java programmer. These operators are commonly used in loops, counters, and logic-building. 🔹 What are Increment and Decrement Operators? ✅ Increment (++) → Increases a variable’s value by 1 ✅ Decrement (--) → Decreases a variable’s value by 1 🔹 Types of Increment & Decrement Java provides two ways to use these operators: 1️⃣ Pre-Increment / Pre-Decrement The value changes before it is used in an expression. int a = 5; int b = ++a; // a becomes 6, then b = 6 int x = 5; int y = --x; // x becomes 4, then y = 4 📌 First change happens, then assignment. 2️⃣ Post-Increment / Post-Decrement The value is used first, then it changes. int a = 5; int b = a++; // b = 5, then a becomes 6 int x = 5; int y = x--; // y = 5, then x becomes 4 📌 First assignment happens, then change. 🔹 Key Difference at a Glance Operator Meaning When Value Changes ++a Pre-increment Before use a++ Post-increment After use --a Pre-decrement Before use a-- Post-decrement After use 💡 Where are they used? ✔ Loop counters (for, while) ✔ Array indexing ✔ Game scores, timers, and tracking values Mastering these small operators makes your logic cleaner and your code more efficient! 💻✨ #Java #Programming #CodingBasics #JavaLearning #Developers

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories