This content originally appeared on DEV Community and was authored by Munin Borah
What I Learned
1. Float Data Type
-
float
is used for decimal numbers (like 3.14, 0.99, -12.5). - Compared to
int
, floats allow precision, but not always exact precision (because computers store them in binary).
Example:
float pi = 3.14;
cout << pi << endl; // prints 3.14
Key insight: Use
float
(or even better, double
) when you need fractional values. Use int
when you only need whole numbers.
2. Increment (++
) and Decrement (--
) Operators
These operators let you quickly increase or decrease a variable’s value by 1.
-
Increment:
x++
is the same asx = x + 1
-
Decrement:
x--
is the same asx = x - 1
There are two types:
-
Post-increment (
x++
) → use the current value, then increase. -
Pre-increment (
++x
) → increase first, then use the new value.
Example:
int x = 5;
cout << x++ << endl; // prints 5, then x becomes 6
cout << ++x << endl; // x becomes 7 first, then prints 7
Key insight: If you just want to add/subtract, both work. But if you’re using it in a more complex expression, pre vs post matters.
Example Code
#include <iostream>
using namespace std;
int main() {
// Working with float
float price = 9.99;
cout << "Price: " << price << endl;
// Increment & Decrement
int counter = 10;
cout << "Counter: " << counter << endl; // 10
counter++; // increase by 1
cout << "After counter++: " << counter << endl; // 11
counter--; // decrease by 1
cout << "After counter--: " << counter << endl; // 10
// Pre vs Post increment
cout << "Post-increment: " << counter++ << endl; // prints 10, then counter = 11
cout << "Pre-increment: " << ++counter << endl; // counter = 12, then prints 12
return 0;
}
Takeaway
- Float vs Int: Use float for decimals, int for whole numbers.
- Increment & Decrement: Shortcuts for adding/subtracting 1.
- Pre vs Post increment: Order matters only when used inside expressions.
At the start, it felt tough, but it’s getting smoother now. Learning may be slow, but consistency wins the game.
#100DaysOfCode #LearnInPublic #CodeNewbie #C++ #CodingChallenge #Programming
This content originally appeared on DEV Community and was authored by Munin Borah