Day 4 / 100 (C++)

🖥️ 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:

flo…


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 as x = x + 1
  • Decrement: x-- is the same as x = 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


Print Share Comment Cite Upload Translate Updates
APA

Munin Borah | Sciencx (2025-08-19T01:50:33+00:00) Day 4 / 100 (C++). Retrieved from https://www.scien.cx/2025/08/19/day-4-100-c/

MLA
" » Day 4 / 100 (C++)." Munin Borah | Sciencx - Tuesday August 19, 2025, https://www.scien.cx/2025/08/19/day-4-100-c/
HARVARD
Munin Borah | Sciencx Tuesday August 19, 2025 » Day 4 / 100 (C++)., viewed ,<https://www.scien.cx/2025/08/19/day-4-100-c/>
VANCOUVER
Munin Borah | Sciencx - » Day 4 / 100 (C++). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/08/19/day-4-100-c/
CHICAGO
" » Day 4 / 100 (C++)." Munin Borah | Sciencx - Accessed . https://www.scien.cx/2025/08/19/day-4-100-c/
IEEE
" » Day 4 / 100 (C++)." Munin Borah | Sciencx [Online]. Available: https://www.scien.cx/2025/08/19/day-4-100-c/. [Accessed: ]
rf:citation
» Day 4 / 100 (C++) | Munin Borah | Sciencx | https://www.scien.cx/2025/08/19/day-4-100-c/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.