Hello everyone! Today, we will dive into one of the fundamental building blocks of programming, namely variables and data types. You know, like when you play a game and your score is saved by the system, that score is stored in a variable. Or when we take a user’s name and surname, they are stored in different variables. Of course, it’s not that simple; we need to go a bit deeper.
Programming is essentially telling the computer step-by-step what to do. And while doing this, the computer needs to keep track of some things. That’s where variables come into play. Imagine you’re not writing the same information repeatedly but giving it a name. Then you can access that information using the name. This makes our job easier and makes the code more understandable. Isn’t that great?
By the way, every variable has an identity called a data type. It determines what kind of information the variable can hold. For example, a variable storing a number cannot be the same as one storing text; it wouldn’t make sense. Otherwise, things would get messy, and the computer might get confused.
Some of the most common data types are. First, ‘int’, which stands for integers. You know, numbers like 1, 2, 3, -5, etc. For example, a variable that stores your age could be an ‘int’. Or it can store the quantity of a product. Then, there is ‘string’, which is used for texts. Your name, surname, product description, email content… All can be stored as ‘string’. Sometimes in programs, you see a warning like, ‘Please enter a valid email address’, which is usually stored in a ‘string’ variable.
There’s also ‘bool’, which is quite straightforward. It can only have two values: ‘true’ (correct) or ‘false’ (incorrect). For example, to check if a user is logged in or not. ‘true’ or ‘false’. Is a setting on or off? Again, ‘true’ or ‘false’. My own program might be struggling here 🙂
And of course, there is ‘var’. This provides a more flexible structure. You can think of telling the compiler, ‘You understand what it is now.’ So, you don’t specify the type of the variable; the compiler understands it on its own. Sometimes this makes things easier, but overusing it can reduce code readability, so be careful.
Now let’s move on to practical application. We will make a simple example with C#. We will define a few variables and assign values to them. By the way, to try these examples on your own computer, you’ll need to set up an environment like Visual Studio, but you can also find online C# compilers on the internet, if you’re curious .
Let’s start with integers. I’ll define an ‘age’ variable. I am 42 years old, so I will assign 42 to my age. And let’s say we have a variable for the play time in hours, maybe 3 hours. I don’t remember exactly, but I guess it’s around 3-4 hours.
<pre><code> // Integers (int)</code></pre>
<pre><code>int myAge = 42;</code></pre>&p>
<pre><code>int gameDuration = 3; // In hours</code></pre>
Now, let’s move on to text. Let’s create variables to store my name and surname. Of course, this is just an example; you can use your own information.
<pre><code> // Texts (string)</code></pre>
<pre><code>string name = “[Your Name]”; // Replace with your name</code></pre>
<pre><code>string surname = “[Your Surname]”; // Replace with your surname</code></pre>
<pre><code>string fullName = name + ” ” + surname; // Combining name and surname</code></pre>
This ‘fullName’ variable joins ‘name’ and ‘surname’ to create a single text. In the programming world, this process is called ‘concatenation’. Sometimes in programs, you see separate entries for your first name and surname, then they are combined into a single name. That’s what happens here. Isn’t that nice?
Next, let’s consider ‘bool’. Let’s imagine checking if a user has logged in. Initially, set it to ‘false’. When the user logs in, you change it to ‘true’. In more complex scenarios, these ‘bool’ variables are very useful for controlling the flow of the program.
<pre><code> // Correct/Incorrect (bool)</code></pre>
<pre><code>bool isLoggedIn = false;</code></pre>
<pre><code>bool isActiveSetting = true;</code></pre>
Now, let’s see the ‘var’ variable. It offers a more dynamic structure. For example, you can define a number with ‘var’.
<pre><code> // Automatically detected type (var)</code></pre>
<pre><code>var number = 100;</code></pre>
<pre><code>var text = “Hello World!”;</code></pre>
<pre><code>var isTrue = true;></code></pre>
As you can see, when using ‘var’, the compiler detects the type: ‘int’ for ‘number’, ‘string’ for ‘text’, and ‘bool’ for ‘isTrue’. This can speed up coding, though, some old-school programmers might find it a bit confusing.
Why do we learn about these variables and data types? Because programming is built on these building blocks. The data you see on a website, the information you use in a mobile app, the logic of a game—all are based on these fundamental data types and variables. For example, in an e-commerce site, product prices are stored as ‘int’ or ‘decimal’, names as ‘string’, stock levels as ‘int’, and whether a discount is active as ‘bool’. That’s why understanding these topics thoroughly is essential for learning more complex subjects later.
Let’s also see an example of incorrect usage and correct usage to better understand the difference. Suppose you try to store a number as text. This will cause an error because when doing numerical operations, you cannot use text-formatted numbers. For example, a sum.
<pre><code> // WRONG usage</code></pre>
<pre><code>string productPriceText = “50”;</code></pre>
<pre><code> // int totalPrice = productPriceText + 20; // ERROR! because string and int can’t be added directly.</code></pre>
Here, we get an error because ‘productPriceText’ is a string, and you can’t directly add a string and a number. It’s like the computer is going crazy 🙂
The correct way is to store the price in the proper data type or convert when necessary. If the price is a number, it should be stored as ‘int’ or ‘decimal’ (for floating-point numbers).
<pre><code> // CORRECT usage</code></pre>
<pre><code>int productPrice = 50;</code></pre>
<pre><code>int totalPrice = productPrice + 20;</code></pre>
<pre><code> // If it were a fractional price</code></pre>
<pre><code>decimal discountedPrice = 45.99m; // ‘m’ indicates a decimal number</code></pre>
As you can see, defining ‘productPrice’ as ‘int’ allows us to comfortably add 20 to it and store the result in ‘totalPrice’. This small difference can be the line between your code running successfully or not. Finally, choosing the correct data type is crucial for the reliability and performance of your kod.
By the way, practicing these basic concepts is very important. It’s like learning the basic knots before climbing more challenging routes in mountaineering. These variables and data types are the first and most solid steps of your coding journey. If you master these fundamentals, it will be much easier when developing things like REST APIs in C#, SQL databases, or embedded systems. Well then, see you in the next article!