Welcome to Programming
If you are an absolute newcomer to computer programming, this is the place for you. I’m going to give you some tips to help you get started and hopefully make the challenges not seem overwhelming. I naturally cannot give you a full software development education within a website page. We’re going to focus on the simplest concepts and learn the basic building blocks of C# to get you up and running. Then start watching the videos and you’ll pick the rest up. My disclaimer here is that I’m not going deep dive into language features. I feel like it would only serve to confuse you at this point. So the things that follow may be oversimplified and you may come to realize there are exceptions, omissions, and alternatives. My intentions are good and I think this is the best way for absolute beginners.
C#
We are going to be learning the C# programming language here. C# (pronounced “C sharp”) is an object oriented programming language with lots of similarities to other popular programming languages. Let’s focus on the basic building blocks that will allow you to express yourself in code. First you should know that programs generally run by executing lines of code from top to bottom on your screen- much like the way you read a book. For now you can think of a program as a vertical sequence of statements/commands to execute. So in general you are going to set data and then use computer logic to manipulate that data to get a result you want based on conditions you encounter. I don’t mean to sound complex here. So imagine you have a store. Your data could be your products and their characteristics (price, quantity, etc.). Your logic to manipulate that data could be calculating a final price by including some sale condition, like it being a Tuesday or Black Friday. Another data manipulation could be to update your quantity after making a sale, removing the number sold from the number you previously had in stock. Hopefully you’re getting a feel that there can be different kinds of data (numbers, words, true/false, etc.) and that you need to be able to make decisions based on the current conditions. Let’s see how you can do that.
Program Control Flow
So we said programs run from top to bottom, in a linear way. Well what happens when we need to proceed in different directions based on a decision we must make? For example let’s say you need to make sure a user is a member in order to provide information. A user’s credentials would be checked. If the credentials are valid, you would proceed as normal. But if the credentials are wrong you don’t want to just give out information! You can see we hit a fork in the road and program flow isn’t simply top to bottom any longer. Enter C#’s conditional statement:
I want you to focus on the “if” and “else” keywords here. You’ll see that “if” is followed by parentheses that contain a condition, which is something that evaluates to true or false. We call this a “Boolean” in programming. The program execution is NOT straight from top to bottom here. It will either match the “if” condition (a + b is greater than 10) or it won’t. If it does, you’ll see that the text “The answer is greater than 10” is printed to the screen. Otherwise the text “The answer is not greater than 10” gets displayed. So one statement is getting executed while the other is getting skipped. We’ll talk more about printing text to the computer screen (Console.WriteLine here) later. Here I just want to make the point that we now have the ability to skip some logic instead of proceeding strictly from top to bottom like I initially told you. This is a nice feature for us to have, to be able to make decisions.
If you were wondering if you are limited to having only 2 ways of branching, you are not! So you can insert as many “else if” keywords with their conditions between the “if” and “else” clauses, giving you unlimited ways to branch if you really want to. You also aren’t limited to having only one condition- you can chain these with logical operators. So you could check if a value was greater than 10 AND less than 50 for example. Or you could check if at least one of a list of conditions were true by using a logical OR between each condition. The symbols for logical AND and OR in C# are “&&” and “||” respectively.
There are other ways to control the flow as well. You probably realize that computers do highly repetitive tasks. Sometimes you want to repeat things. We have looping structures for this.
So this code will print the “Hello world…” line 10 times, with each printed line containing the actual index value at the time of printing. It will be 0 for the first execution, 1 for the second, 2 for the third, etc. You can see the three parts of the “for” loop inside the parentheses following that keyword. They allow you to set a variable to use for indexing, then you specify a condition for when the looping should be allowed (index < 10), and finally you specify how the index should update after each time through the loop. Most of the time you’ll see loops like this- where the index starts at 0 and is increased by one each time through the loop. But you are in no way constrained to this. You could double the index each time if you wanted or you could decrement it. You don’t have to start at 0 either.
There are also “while” loops for when you don’t want to specify the exact number of loops that should occur. For these loops you provide a Boolean condition like you did for the “if/else” statement. Your loop will terminate when your condition is met. Beginners often get in trouble with these because they forget to update the values referred to in the condition, and so end up in an infinite loop. It’s not actually infinite- your program will crash out relatively quickly. The takeaway is to remember to update your variables, like how the “for” loop had you do before. In this case, it is the “counter” variable, which starts low and is incremented (counter++) to meet the terminating condition:
There are variations of “for” and “while” loops that you’ll encounter in my videos. You can also look them up in the Microsoft C# documentation.
Data and Variables
So you’ve got some decent power now with the program control statements you learned. Let’s move on to talk about data and how to work with it. You know what data is but the way we work with it in programs is by loading it into variables. If you’ve had math and science classes, the concept of a variable should be familiar to you. It is a value that can change. In programming it additionally refers to naming a location in memory, of suitable size, where you can store and retrieve the data. It’s nice that we get to give variables human friendly names of our choosing instead of referring to places within billions of memory addresses. If you look at the previous image, you’ll note that “counter” is a variable. In C# you can make variables by listing their type, a unique name of your choosing, and then assigning an appropriately matched value. Notice how that code example then later refers to the counter variable, first to access its value and secondly to modify its value. Let’s talk about the most common built in variable types, but know this isn’t an exhaustive list. Also know that you can make your own custom data types with classes, which are compromised of these built in types or even other custom types.
bool: A Boolean value that can have one of two possible values, either “true” or “false”.
int: A 32-bit integer value. No fractional parts allowed here. There are other kinds of integer types too, bigger and smaller, but you’ll overwhelming encounter int.
string: A piece of text consisting of letters, numbers, symbols, whitespace, or punctuation marks.
char: Similar to string above, but this is a single text character. Strings can be thought of as collections of chars.
double: Another numerical value, but this type allows you to have fractional parts as well. You aren’t limited to integers and can have a value like 2.8752. Double is short for double precision if you want to look up floating point numbers.
Functions
Functions are also referred to as methods. I will use “function” here, but note that software developers often use these terms interchangeably. Functions allow you to package up pieces of code that you can reuse throughout your program. So imagine you work with temperature conversions often. Would you really want to specify how to convert between degrees Fahrenheit and Celsius everywhere you made a conversion (pretend very often)? Of course not! With functions we can define how to make that conversion ONCE and then just refer to the function by name any time we want that result. In this case you would have to pass a parameter to get the result. You tell the function what temperature value you want to convert and it returns you the result. Maybe you need to tell the function whether your current temp is in Fahrenheit or Celsius too.
Let’s talk about this critical aspect of functions: parameters. Functions can take 0 or more parameters. It’s considered a best practice to limit the number of parameters a function takes. If you find yourself having too many, it’s usually a sign your function is doing too much and you should break it up into smaller functions. Remember, your functions can call other functions! So keep them small and easy to understand.
Parameters are part of a function’s signature. The signature is like the ID for a function, what allows your computer to uniquely identify it. Imagine if you were in charge of running a computer program and you found functions with the same name AND the same number of parameters, with the same types, in the same order. How would you know which function to call?! So you are forced to keep your functions unique. There is an slightly more advanced technique called function overloading that allows you to reuse the same function name, but your parameter lists have to be different.
The other aspect of functions is that return value I mentioned before. When you create your function you also specify a return type, which could even be nothing (often called “void”). Let me give you an example of a simple function:
This function is named “DisplayWeatherReport”. Now there are other keywords that may appear with function definitions, but remember, we are keeping things simple here. The first “void” keyword is the return type. So this function returns nothing- it only prints messages to the computer screen. Void could also be replaced with int, bool, or a class name- whatever you need to return. Then comes the function name. Finally you have parentheses that enclose the parameter list, the input to the function. Again, you can have an empty parameter list or as many as you reasonably need. This function has one parameter called “tempInCelsius” of type double. This allows you give more precise values than an “int” parameter would. When there is more than one parameter in the list, they are separated by commas. What follows the function declaration is a pair of curly braces that define the scope of the function. Everything within these closing braces is what executes when the function is called. So the parameter list is the input to the function and the return type specifies the output of the function.
I like to tell people to think of functions as a contract, and to think of yourself as the person who is obligated to provide a service. When someone calls your function, they must give you the parameters you set in the list. You cannot do your job without this information. Once the caller gives you the info you need, it is your duty to run your service and give the caller information of the type specified in the return type. They give you input, you give them output. A word of caution with input- it’s often the result of a previous calculation and may not always make sense for your “service”. Imagine if you had to divide by a parameter value and the caller gave you a zero. Ouch! You’ll often see function definitions do input validation first to prevent these disasters. And sometimes you will act like the “client” where you provide input values to someone else’s function in order to get a result from them. Often you act in both roles- function implementer and caller.
Classes
You won’t use classes as much in the absolute beginner series, but you need to know of them. They are how you get the objects for object oriented programming (OOP)! You can think of classes as blueprints for creating software objects of your own design. You write a code file that contains a class and all the fields and behaviors that class needs to interact in the way you want it to. These fields and behaviors correspond directly to the data and functions we discussed previously. You can think of that as what classes consist of. Sure, you could not make any classes in your program and just use strings, bools, and integers all over the place. But the classes allow you to package data up into units that makes sense, in a small size that we can easily think about. You won’t fully appreciate this when you are doing the beginner series because the challenges are so small. But as you grow to handle larger sized projects, you will see it is absolutely necessary to organize your code and data. OOP is really good for beginners. Instead of having to think of how you can model your problems with mathematics, you can model objects you are familiar with in real life, where you know how they should interact with each other. OOP makes programming accessible to more people.
Staying with the store example I used in the opening of this page, suppose you wanted to create a program for it. You could make objects in software that correspond to all of the things you encounter in your brick and mortar store. You could make objects for products, services, customers, sales, orders, inventory, etc. You give each of theses objects the fields and behaviors they need to interact with each other like they do in real life. Your Product objects for example would have data for a price, a description, and maybe how much you have in stock. Your Product objects could have behaviors (functions) to modify their price or notify some other software object when their stock is low. An Order object could contain a list of Products and how many were purchased. It could have an additional field (bool) denoting whether that Order has been fulfilled or not. Your Customer objects could have an Order history and a discount associated with them if they meet your criteria. I think you get the idea. Make your software easy to think about and work with.