Basic Algorithms and Exercises

In this article, we will go through some basics usage of variables, types, and algorithms to get you comfortable with logic and how programs are written. We will be using c# for the examples and templates that I provide to you, but feel free to use any language you want in the future, it shouldn´t matter at this point. The syntax may vary a little from language to language, but the core ideas we are touching on this article should be transferable.

Lets start by getting to know how to use this dotnetFiddle embeded in the page to run some code. Modify the variable value, hit the blue button that says “Run” and have your name printed out instead of “Matt”:

Let’s ignore, for now, the using system, class, and all that stuff. The only thing you need to know is that Console.WriteLine will output below what you pass there between the (). And we learned in the previous article that a variable is a type, a name, and a value. That’s what we did there, string is the type, myName is the name of the variable and “Matt” was the value before you modified it to have your name printed out.

With that out of the way, let’s get to do some interesting stuff. First, we will do some arithmetic examples of what we can do with code. Follow the comments and modify the example to make it work as expected:

There we have 2 variables, number, and secondNumber. I already did the addition operation for you. Complete the other three lines using the corresponding operator and the two variables. (Hint: operators are: – , * and / in most languages). After you get the correct results printed out below we can move to start using conditionals as we learned in the previous article.

In the following code snippet, you will be introduced to the way of writing if conditionals. In C# this is done with the if reserved keyword and then a condition enclosed in (). If the condition is true, then whatever we put between the { } will be executed. In these cases, we will evaluate which number is greater given two numbers. The idea is for you to modify the values of the numbers and run the app to see what is written in the console and also to complete the last condition to make it work as expected. Quick Note: Pay attention at how are we checking for equality of numbers using ==, that’s because one = is for assigning values.

Now let´s move on to some ifs with strings values and lets introduce the else keyword. You see when we talked about conditionals before we said it was like a path that gets divided into two ways. So far we have only used one way, the one that executes when the condition is true. But what happens when the condition is false. For that we use the if(condition) { doSomethign} else { doOtherThing}. Lets work this example:

In this example, we use a string variable and the “if-else” statement. If you run it like that you will see the else path gets executed. Go ahead and modify the value of the variable currentWeather to be “sunny” and see how the behavior of the program changes.

Great. We covered a lot in this article, basic usage of variables, types int and string (also bools but without declaring them), and the conditionals. We will wrap it up with an exercise for you to try and solve. If you have any doubt feel free to hit me up on Twitter or in the comments below this article. Have fun:

See you in the next article to cover loops and arrays!

Click on the image above so you don’t miss out on future content.