Introduction to Programming

Spread the love

One might be looking at moving towards computing at a later stage in one’s life, what I’ve noticed is that usually the tutorials that are found can be a little bit troublesome. As someone that came into computing at a young age, I understand the way the computer works more than most of your typical people.

In 2016 I gave introductory lectures for students in Computer Science Major at a few universities here in South Africa. I learned right then and there: while something in terms of what computers do may make sense to me, it could be a “little too deep” for others, so to speak.

Please note: Code snippets will be shared at the end.

Plan of Action

Please, take note, this is intended for a general audience that has no experience in writing code. Never mind that, I figured I should lay it out for it to be easy to get some experience in.

Imagine you look at Joe Soap’s tutorial and he goes “So, open your console after installing Visual Studio, the tun ‘dotnet –version’ to see if it is installed correctly…” As someone who has been in computing for so many years, I wouldn’t have any trouble with that. I have the necessary background in computers. For someone my age that wants to get into programming they might have a little less experience with computers, so it wouldn’t make sense to jump in to make them use absolutely everything for their first programming experience.

Around midyear 2014 as a student that assisted in the labs, we needed to introduce high school students in grade 9 to the concept of programming. We went step-by-step through a program called [scratch I think]. Sure, we got them to make a little game in it, we also introduced them to more of the other uses for a computer. How to use Word, how to use Excel, and so on. After around 30 minutes of sharing, and describing, how they can use a computer they were given time to try it for roughly 15 minutes. Then they were taken to the next department.

The concept was to show the Gr. 9 students which subjects they should choose for what they wanted to study after matric. In the morning we went though a class at a time, if I remember correctly, we did ‘a school a day’ for a few days during that years holiday. Every afternoon the student would be allowed to go back for a while, usually an hour or more, to the department they were interested in. You can understand that several came back to the Computer Science labs to ‘see more.’

Some came to try out Word and Excel, their interest seemed to show it came from data in general. Some others of them came to play, so to speak, with [scratch] to have more fun with games. Sure, we also have game development courses, and degrees, here in South Africa, it just seemed they wanted to ‘play around’ with the computers on their school trip. Every day a few would come forward to us tutors and ask to see more in their visiting time to see departments.

We would usually sit down, as tutors, and open Visual Studio to show them it goes further than [scratch]. We would open our university, or also personal, projects and describe the ideas behind what we made. For a few they were immediately interested and wanted to learn more, so we suggested they take IT as one of their subject choices for the next year. On the opposite side, it also worried a few students.

Not that I remember the exact words the students used, on one of the days a shy lady came following her friends. The boys were quite loud, and boisterous, they asked a few simple questions about the programming. Questions such as “What is a language?” You can no doubt tell, high school students will rarely have interest until they see something they actually like.

They weren’t sure if they wanted to program at all, they were mostly just curious. They stayed at the lab for around 5 minutes, then shared they were going to the next department. As soon as they shared that they wanted to move on to the next department I noticed, at a glance, the shy lady immediately became worried, she hadn’t said anything yet in that lab visit. So, I asked her, she roughly pointed out “I have never used a computer, but I loved using it today. Can we do maths with things like [scratch]?” The group fell immediately silent and listened, they closely watched while I showed off a little mathematics through C#.

Sure, it could be future mathematicians I was having conversations with, the point should easily come across. Looking at programming in a different way can help you learn more about programming.

It is unfortunate that I don’t remember the exact words of that day, but you can see the point. The aim of this tutorial is to take a ‘different perspective’ in teaching someone to code.

  • Step 1: Get Visual Studio 2019 Community
  • Step 2: Minimal Mathematics
  • Step 3: Introducing types and functions
  • Thoughts

Take note, with this you might be further than the simplified introductions in Step 1 or 2, it can just help to know the terminology, if you aren’t completely familiar with it, and what we are talking about afterwards. Hence, it is slightly simplified, it is just to cover ‘all bases’ just in case.

Step 1: Get Visual Studio 2019 Community

If you don’t already have it I would suggest you first go and get Visual Studio 2019 Community installed. For simplicity I chose to stick to broad areas of development.

Select the platforms you feel like, could just be .NET desktop development. If it doesn’t get installed, grab the .NET Core 3.1 SDK here.

We can do tons more in VS2019, however this time we will focus on using a .NET Core C# console application.

Project Type we use initially.

Step 2: Minimal Mathematics

You should hopefully have Visual Studio installed for yourself by now. When you open it up and it shows you can create a solution you can choose ‘Console Application (.NET Core)’. I called it ‘Tutorial1’, let me just describe the view since we don’t need to care about too much in this step.

For someone without any coding experience this can be overwhelming at a glance, for this ‘Step 2’ we will pay attention to only where the line ‘Console.WriteLine’ is on.

For your own safety, always remember to save when you write any code at all. Saving often can save you in the future of your application programming life. For all the lines of code shown in the image above we only need to pay attention to where the line is highlighted for now.

Don’t trouble yourself with what ‘static void Main(string[] args)’ means, too much, for starting out this is just ‘the main code that will run when we click play’. The ‘args’ we may use in the future, but we don’t need to worry about them today. Inside the brackets, ‘{‘ and ‘}’, below ‘Main’ is where we will do everything in ‘Step 2’.

So, as you see the brackets in the image, we need to keep every bracket. There will be more on this in a little bit. We will rather step into understanding what code is.

In programming we have variables, these are values stored in memory that we name for ourselves to understand in code.

int var1 = 13;

We can put the above into perspective. The first ‘type’ of variable we will use, what that shares is we want an ‘int’ saved as a variable. So, ‘int’ is its type, and it means ‘integer’. This is a positive, or negative, whole number. We wont delve into things such as sizes currently, just mention that it has an upper limit for the number it can hold, and a lower limit.

You will see I’ve called it ‘var1’, that is the name we can use for it anywhere in these brackets. The ‘= 13’ is saying in the code to set it to 13, and the ‘;’ lets the code know that the line is finished. You will note if you don’t say that the line ends it can give an ‘error’.

I simply made sure there was an error to show it off. When you don’t understand why it wont work always just double check that there aren’t any errors. Some can be easy to understand, like the one above, others may be a bit more difficult.

Console.WriteLine("Answer: " + var1);

We adjust the Console.WriteLine above, this will allow us to just see the value in the variable.

It is very simple to see we gave it in the code, ‘13’, it stores it in memory for us, then it prints it out to console. This is to share that code will run line, by line. First we have ‘make an integer called var1 and give it the value 13,’ then we more onto ‘on the console, write the line Answer: with the value inside var1.’

There are deeper things in terms of having more things run at once, and so one, we just wont go that far today.

Lets just discuss the mathematics for a little.

Please, don’t trouble yourself with the answer there being wrong. The answer should actually be ’15,7368…’ instead of just 15 here. This is to point out that the ‘data type’ is important, this one is an ‘integer’ so it rounds off to a whole number as you can see above.

Similarly, you will notice the mathematical operations:

  • () : shares what must be executed before it is used, as in executes ‘var1 * 23’ as its own thing, so it becomes  ‘299’
  • * : multiplication
  • / : division, left is above, right is below. You can’t use ‘\’.
  • + : addition
  • – : subtraction

Take note, this just touches the mathematics for starting out. There is also a System.Math class that has several functions, and as noted before the data type can impact the answer. An ‘int’ will give an ‘int’ answer.

Don’t worry too much further, but just to show it off:

You will notice the answer is immediately more accurate for us, this will be what we look into at a later stage. For now, we can just put it in an ‘int’ variable. As you will note, until now we have had the single variable. We could just make a ‘var2’ just below it with another number we want.

In essence, as you will notice above, we just set ‘var1’ to the mathematics we have on the right hand side. The maths can interchangeably be fully mathematical terms, like before, and variables.

The usual question happens to be “can we make a double work with an int, they’re both numbers?” You can see above the answer there is ‘no’. We can convert between different number systems, so ‘int’ being a whole number, ‘double’ having a decimal, and so on. We won’t delve too deep into that today.

Don’t be intimidated with the image above. Remember we mentioned ‘Main’ before? Well, now you will understand more.

string[] args’ stands for an ‘array’ of strings, that is, it can be given no strings, or it could get 37 strings given to it when it is run. We aren’t running it with these ‘arguments’ today, which is why we didn’t worry too much about it.

Underlined in pink is ‘int in1, int in2’ – these are arguments. Essentially when we make a function, we specify the ‘type’ of what we want to send into it, ‘int’ here of course, and we give them variables which are ‘in1’ and ‘in2’. So we didn’t need to create what we want to use in this function, we just specified what the function needs.

In the red square we have ‘static int’, this is just what we call ‘accessibility’ and ‘return value’. You can no doubt tell it will return an ‘int’. While there are tons of things we can do with ‘static’ in the future, don’t worry about it today. We only use ‘static’ this time for the function since ‘Main’ is ‘static’. The general sense, which ignores parts of it, is this can adjust ways we want to use code.

So, on the yellow line you will see that we call this function ‘Multiply’ when we create it in our code, where on the light blue line we call it. Simply put, when we run this program, we go line by line.

  • Make var1 = 13
  • Make var2 = 23
  • Do calculation so that we have a value to put into var1
  • Brackets first
  • Function sending var1 and var2

Pause there for a moment, we haven’t used a function here before. Typically, we can just take a moment to look at order of operations. We know the order of the lines happens to go from top to bottom. In code, we also know the mathematics follows exponentiation, multiplication/division, then addition/subtraction. The brackets work the same as they do in mathematics. So, what about functions?

It would follow the rules. It sees ‘(Multiply(var1, var2)) / 19’ as an entire mathematical operation. It sees the division first, then goes ‘what is: (Multiply(var1, var2))” as it looks to see what it is trying to divide. The it goes ‘in the brackets is: Multiply(var1, var2)’. It, technically, then goes ‘it’s a function, run it to get the return value.’ This boils back to ‘we got 299 inside the brackets’, which boils down to ‘299 divided by 19’. It does the simple mathematics, and uses that as the ‘int’ we want to set ‘var1’ to.

This can add a little trouble, you will note. “1 + 3 / 2 = 2.5” whereas “(1 + 3) / 2 = 2”. Just take care to group your mathematics together correctly with brackets. Lets continue along the steps.

  • Function returns ‘in1 * in2’, gives ‘299
  • We have above division, now get below division
  • It is an integer, ‘19’
  • Calculate ‘299 / 19’

This isn’t exactly the order these things might be in, it’s just to understand the concept of what we are writing. We can always follow the logic of what will happen in our code. Sure, it could have many deep operations that will occur, however we can make functions to simplify it a bit.

Hopefully you have, having completed Step 2:

  • More understanding of how to easily make “simple mathematics-based console applications”
  • Understanding what can happen line-by-line
  • Spotting “errors” in code

Step 3: Introducing types and functions

We’ve touched a little on ‘data types’ now, please note that there is way more than we share here today. We will focus mostly on 4 of them.

  • int : positive or negative whole number
  • double : positive or negative number with decimals
  • string : a line of text
  • bool : technically a boolean, just consider it easily like “yes” or “no”

For starting out we should introduce a few more functions, unfortunately. Before we used:

Console.WriteLine("Answer: " + var1);

Which is all good and dandy, but take note, again, for seeing more you can just type the start of a name class or variable and Visual Studio will show it to you.

Then, when you add ‘.’ It will show you its ‘public’ variables, and functions.

Note right there, ConsoleColor is a class, and its name should be understandable for what it is. ‘Beep’ shows what the functions look like, you will note that when you mouse over it you get to see the use of the function described, and in some cases possibly there are overloads. This means there are several ways you can use it, we just wont completely delve into that today.

What we want is the ReadLine() for typing things and getting it into our code when we press enter. You will, no doubt, note immediately that we cant just convert a ‘string’ to an ‘int’ like the ‘double’ trouble we had before.

            Console.Write("Please type an integer: ");
            int var1 = int.Parse(Console.ReadLine());
            Console.WriteLine("Answer: " + var1);

There is more depth we can go into if we want, just note that double.Parse usually takes a “,” for decimals, bool.Parse takes true or false ignoring capitals, and I figure you can understand we don’t need to string.Parse because we actually cant. So, you will note we have a quite simple way to input any data type we want now on our console application.

This way we can delve into the first ‘small thing’ we can introduce. ‘if statements.

Take note first, I show how to make comments. Adding comments can help you remember what the code is supposed to be doing for you. Just note, like this comment says, the last ‘else’ wont run.

We have several logic operators available; it is easiest to think of it as if you’re talking. ‘If var1’s value is true we must make var2 have “True!”, or else if we have var1’s value as false we must make var2 have “False…”, or else no matter what we have nothing since there’s only a comment.

These are boolean operations. There are several we can use with simple logic:

  • == equal to
  • != not equal to
  • >= greater than or equal to
  • <= less than or equal to
  • > greater than
  • < less than
  • && and, e.g. a<b && a>c; a is less than b and a is greater than c
  • ! not, e.g. a<b && !(a>c); a is less than be and a is NOT greater than c

You can understand we would use that usually with an int or double interchangeably for the most part.

        static void Main(string[] args)
        {
            Console.Write("Please type an integer: ");
            int var1 = int.Parse(Console.ReadLine());
            while (var1 != 0.0)
            {
                if (var1 > 0)
                {
                    Console.WriteLine("    GGRREEAATTEERR TTHHAANN");
                }
                else
                {
                    Console.WriteLine("    less than");
                }

                Console.Write("Please type an integer: ");
                var1 = int.Parse(Console.ReadLine());
            }
        }

The important part here is take note, ‘var1’ is an int, whereas 0.0 isn’t. The condition here doesn’t care about it being an int versus a double. Certain cases may have trouble, but we aren’t going that deep into it today. Its just to introduce the starting concepts for programming in general.

The code can make sense for you logically, I hope. We can look at it directly. ‘While’ some condition is met, inside the brackets there of course, check if what we put in was ‘greater’ or if it was ‘less than’ zero.

We set that condition to be that it continues until we input 0 since it is int.Parse we’re using. Take note, we aren’t touching errors and things yet today. This is just a supplementary programming helper tutorial.

Hence, we finish off by allowing a new Console.ReadLine after we do our little calculation. Well, that is if we don’t try start with ‘0’ since it uses the loop only if it isn’t 0.

Take note, we can continue to nest code inside other code. We’ve touched functions a little, and we got the idea of logic. Sure, we know how to make repetitive loops using ‘while’, but we might need to swap over.

                    for (int i = 0; i < 6; i++)
                    {
                        Console.WriteLine(" - " + i + " gives " + (i * var1));
                    }

Following how we’ve looked at it before we can do that here again. ‘For some int I which we set to 0’ is where we begin. That is the first ‘condition’ so to speak. You will notice that we use the ‘;’ delimiter to group sections that it needs in how we define it. The logic then shares ‘While i is less than 6.’ So starting at 0, while its less than 6, We then go on to say ‘i++’ which means ‘increase i by 1’. The full way to look at this is:

int i = 0;
while (i < 6) { 
  /* some code */
  /* last line */ i++; 
}

You can, no doubt, tell that we might not even NEED the ‘for’ loop. We could totally have done that with a ‘while’ loop in the beginning. This essentially just boils down to simplification for us to read and understand our code. It is easier to talk about things in the way ‘for every number in this range’ rather than ‘while the number we use is less than this’ at times. So, you can use them interchangeably.

Take note, finally, that we have experienced sharing of the 3 data types, int, double, and bool. The question can be ‘Where is string in all of this?’ Our main use of string is for input from console, whatever we type there comes in for us as a string, that is why we dropped a little into using int.Parse, bool.Parse and double.Parse. Similarly, what we write to console needs to be string, in the distant past, but now we can just use the variables with their data types.

If you ever have trouble and need to convert any of them to the actual string you don’t need to worry at all. i.ToString() would work perfectly as well for the last code snippet shared above.

What we’ve found in step 3:

  • A few of the data types we can use, and how to use them for our introductory console applications
  • Code logic, “if statements”
  • Loops, “for loops”, and “while loops”
  • The idea that we can put sections of code within other sections

Thoughts for Today

I still have to drop into making the content I asked about on Make Games SA, you will note I did share my wrapping of MonoGame for ease in my uses. This is intended as an explanation of the broad things you will need to begin programming from scratch. If we want to delve deeper into this I’d love to hear from people about what they need.

Don’t get me wrong, we can move into encryption and SQL stored procedures, I just prefer to make the content help people with whatever they want. I’ve finally been asked to help with an intro to programming. It is, technically, meant to help someone who, like me, is far gone out of the school ages. If you want to start programming for the first time in so many years? This is meant to be an easy resource for you to use.

Code Snippets from above