Programming Diaries — Day 4 —Getting into Practice

배우는 자(Learner Of Life)
6 min readJun 12, 2022

--

Finally writing some code!

#Code, #C#, #Error, #Exception, #Local, #Variable

Finally got to actually writing some code. Woohoo! (1)

What did I do today?

Today was the day that I finally got to practice some coding. I expected that I would jump into it right away, but there are things to go over before that, just as you stretch yourself to make sure that you are not strained before jumping into the pool.

I have covered very basic concepts and ideas of programming such as variables, attributes, namespaces, methods, classes, etc. However, of course, there were also some interesting facts to be found in today’s learning.

Initialization of the variable

What’s wrong with this program?

I had written the program above and compiled it, and then I got a fail message. What would be the error? Why is this code erroneous?

In the previous post of day 2, I had mentioned that a local variable must be initialized to a certain value, to be able to be used. In the code above, the variable “number” isn’t assigned to any value. Therefore, the next line of code cannot print it since it does not have an actual value.

It had also been mentioned that “variable” is a name given to an “address” or a “location” in a certain memory. This means that a variable is only the name of the location, and NOT content.

For instance, say that you want to know what product is sitting in a warehouse in a certain location. The place systematically sorted the things, by giving the name of each location. For example, there are shelves A, B, C, and D, and each of them has 12 slots to fill in the products. Each slot is named according to its sequence, with the leftmost one being 1 and the rightest one being 12, which is followed by the alphabet of the shelf with which slots belong. You would like to know the product that is sitting in A-12, which is the name of the location in the 12th slot of shelf A. The product sitting in this location is a set of Bosch 12-inch bearings. For convenience, this product is named “Bosch 12-inch Bearings”, and entered into the system. The system, therefore, returns “Bosch 12-inch Bearings” as its response. The name “A-12” is considered a variable in this case, and “Bosch 12-inch Bearings” is the value in this location. If there is no product, then A-12 is considered “empty”, as “A-12” itself is not the content of the location but only its name.

Therefore, if the variable “number” is assigned to a value, then the code would finally compile.

The problem was that the byte type data “number” was not assigned to a value.

Let’s do some more exploration. Let’s add some more data types. I added an int variable and a float variable and declared each with a value. However, I had encountered another error.

What is wrong again with this new code?

What would be the error? What might have caused an error in this case?

It has also been mentioned that by default, C# treats real numbers as “double” type. This means that unless a variable assigned with a real number is declared to be otherwise, the compiler assumes that it is double-type data. Therefore, “totalPrice” cannot be a “float” type unless we add “f” at the end of the value.

To assign a variable a float type number, the value must be appended with the right suffix.

Let’s add some more variables. This time, those with characters. “String” is used for a demonstrative purpose, but will be covered later in concepts. Note that the string value is enclosed with a double quote. This is an important distinction between char type data and string type data. Let’s also add a bool type data, which only has values of true or false. Let’s print them all out with new “WriteLine()” commands.

Now the code looks a bit longer

There is also interesting magic that C# has. Instead of designating types for each variable, you can type “var” to let the compiler decide what each of the variable types would be. If you had set it properly, then the compiler would automatically detect the appropriate values based on your declaration. How convenient!

If you had set each variable type as desired properly, “var” can do the dirty work for you.

In reality, it is safe to use the “int” type most of the time. This is because in the classes in the .NET framework, the int data type is mostly used to represent integral numbers most of the time.

Let’s do something more interesting. Previously, we had only declared and printed each value one at a time. However, we can declare and print the values simultaneously in a single row, by adapting a placeholder format. The “{0}” represents the first byte value(MinValue) and “{1}” denotes the second byte value(MaxValue). We are assigning values of 0 and 1 simultaneously to these two variables and printing them at the same time.

This is how we print multiple values at once.

Let’s declare some constants. We had previously learned that we cannot change a constant. This means that a constant cannot be re-declared once it has been fixed. As it was mentioned, this is to avoid the risk of accidentally redefining the value, which needs to stay consistent.

You cannot re-declare a constant once it is fixed!

What is expected of the future?

It has been such a valuable time applying the knowledge that I had gained previously to the actual programming domain. It is always the best way to put into practice the knowledge that had been gained, to fully make them part of me.

It is quite amazing how we can test our knowledge instantly with programming. The way we right, the keywords that we use, or even a single character of a line tells a lot about our understanding of the subject. You can never lie with programming, as it fully reveals about who you are and what you are capable of as a programmer.

Programming seems to teach me how to stay humble, as I can make a very trivial mistake that would result in the failure of the program running. I should be able to accept that I am not perfect and that I can make a syntactical mistake somewhere, but should be able to face the fact that there are much more things that I do not than what I do know. Since, this world is ever-changing, at a faster rate than any other industrial sector, it is very essential to always stay humble and ready to learn new things.

Therefore, I am thankful for the programming, for reminding me of the importance of humility. Though it is the machine that I work with, most of the time, it encourages me to remember myself as a trialing human.

P.S(Please See(if you would like))

  • Short key for compiling: ctrl + shift + B
  • Running the application: ctrl + F5
  • To see the data structure within .NET framework, put the cursor on the desired data type, and press ctlr + click
  • Note that all the blue-highlighted ones are the keywords
  • In the Visual Studio, type “cw” and press “tab” to automatically convert it into a “Console.WriteLine()” code

Reference

(1) https://pixabay.com/vectors/programmer-programming-code-work-1653351/

--

--

배우는 자(Learner Of Life)
배우는 자(Learner Of Life)

Written by 배우는 자(Learner Of Life)

배움은 죽을 때까지 끝이 없다. 어쩌면 그게 우리가 살아있다는 증거일지도 모른다. 배움을 멈추는 순간, 혹은 배움의 기회가 더 이상 존재하지 않는 순간, 우리의 삶은 어쩌면 거기서 끝나는 것은 아닐까? 나는 배운다 그러므로 나는 존재한다. 배울 수 있음에, 그래서 살아 있음에 감사한다.

No responses yet