- Published on
A Gentle Introduction to Programming
What's all the buzz about?
- Anirudh Rowjee
::: warning This post has a sizeable amount of content. Feel free to take breaks in between - understanding what i've written is important, not how fast you do it. :::
"The good news about computers is that they do what you tell them to do. The bad news is that they do what you tell them to do." Ted Nelson
What if I told you that within your possession is an object that can do simply incredible things?
This device has simply transformed humanity in a large number of aspects - It's connected the world in more ways than one can imagine, Allowed us to literally reach for the stars, unlock the secrets of our DNA, Accelerated the growth of global economies, and lastly, pushed humankind to solve some of the most challenging problems we've encountered.
Here's a hint - you might be carrying it with you every day, you might have one at home, or you might be using one in a lab.
Well, of course, it's the Computer! Your smartphone is one. So is your laptop. So is the Desktop PC in your lab.
Let me ask you another question. What if I told you that if you put in some time and effort, you will be able to make these beautiful devices do your bidding?
Because you can. It's right here, within your reach. All you have to do is be willing to learn - and given that you're here, reading this, I assume that you are :smile:.
What will I get by learning Programming?
- It's an exercise in logic.
Now, before you reject yourself because you feel you aren't "good" at such things, hear me out.
Programming is NOT math.
(This isn't to say that you can't use programming to do math - you can, and that's a topic for another post.)
The Fact of the matter is, programming is perhaps one of the most fun ways to improve your logical acumen, because it has direct and tangible results. Math tends to be more abstract and has a higher learning curve as opposed to programming. In many ways, it's easier. There is NO hard-and-fast rule that you must be good at math to be good at programming.
Think of it as a muscle. Even the most experienced programmers who have built large-scale systems will have trouble programming if they haven't done it for a long time, which is to say, practice is important.
This means that more people around the world are coming online, as a part of the huge global community that we are. Understanding how the internet works, and the true extent of its capabilities is fundamental in using it to bring about change, and improve people's lives. More than that, it's about putting yourself in a position to take advantage of this connection to deliver value to yourself and the world at large.
If you know how to write code that takes advantage of this connection to make things happen, it is nothing but an advantage.
- You will become much better at problem solving
This holds true not just for programming problems, but for problems in general. Taking a structured approach and looking at a problem from multiple directions are skills that you will pick up ALONG the way. These skills, and the accompanying mental models, will help you no matter what field you choose to make a career in.
This will become more apparent as we use pseudocode to model real-life problems in the sections ahead.
- It's a path to a better, high-paying job.
Compared to many other professions, the bar for entry - i.e. the minimum amount of skill required to get a job in the software engineering field is far lesser than that of any other field. You can use the internet to do this, in many cases, without having to pay for external training. Many of the Industry leaders in Software will hire people without a college degree if they are passionate about what they do, and skilled enough to make the cut - there is no formal restriction.
In any case, Glassdoor1 reports the Average salary for the role of Software Engineer in the USA to be ~$93,000. This is a LOT of money, and can lead to a significantly better life for many people.
::: warning I am formally advising you against dropping out of college. I am not responsible if your parents find out that you are considering such things, or if anything happens to you as a consequence of this choice.
Again, I am advising against you dropping out of college. :::
What can I do with programming?
At the end of the day, no matter how fun the act of doing so may be, Programming is a means to an end. It's a method by which we solve problems - as long as we can define the problem we want to solve.
Some problems are well-defined, and some aren't well defined at all. This goes to say that for some problems, there's an obvious solution, and for some problems, there isn't - but this is no indication that it's something that can't be solved by a computer. Theoretically, if you can define a problem in a mathematical sense, a computer can solve it. It might not be efficient (i.e. takes too much time) or it might not be feasible (i.e. you need more processing power than the energy of the sun) - but it is possible.
::: warning When we talk about problem-solving with programming, it's easy to digress and talk about Computer Science problems - classical problems like the Travelling Salesman Problem and the Knapsack Problem (I highly recommend you read about them when you're more comfortable), whose answer is mathematical, but have programming solutions - are extremely confusing for beginners because of the sheer background that you need to understand the problem as it is. Because of this, we'll talk more about the applications of programming. It's not as glossy and sleek as solving the computer science problems, but are of high utility nonetheless. :::
Let's say you want to get an SMS/Whatsapp Message whenever the price of an item drops below a certain number on Amazon or Flipkart (I see you looking at that smartphone you've been wanting). You can do that with no more than 50 lines of code!
Maybe you've got a bet with a friend about how many times the word "Hello" was mentioned in any book of your choice. That's also extremely easy to do with code, as opposed to spending hours reading the book with a notepad in hand! This is an optimization.
At this point, you may ask - "But wait, Anirudh, these are all silly problems! The first one maybe has some potential, but what about delivering value to people? Can I do something that people would pay me for, or something that would deliver some tangible value?"
And that is an excellent question. Thankfully, we have answers.
Let's say you want to track your habits at the push of a button - that every day you brushed your teeth twice, you would press a button, and at the end of the month, you'd know your longest streak and success percentage. This is also only mildly difficult to implement.
Or what if the vegetable vendor pushing a cart near your house wants a better way to manage his finances, so that he can keep his money safe and track online payments. Not impossibe! Definitely difficult, and definitely not something you can do on your first day (contrary to what some people will tell you), but it is very much possible.
The bottleneck is often the programmer and not the computer - simply put, if YOU can solve a problem, you can theoretically write a program to solve it.
An Introduction to PseudoCode
Okay. Now that we've seen the dreams of how we'll make the world a better place, let's get down to implementation.
Pseudocode can be thought of as programming in English - it does not have a defined syntax (i.e. a set of rules on how everything should be written, what words can and can't be used, etc). It's a simple description of programming constructs. You'll understand more as we move on.
Pseudocode is often used to describe what is known as an Algorithm - a defined set of steps to take to solve a problem. Here's a universal one - making a cup of coffee.
remember ingredients as milk, sugar, coffee powder add sugar to milk turn stove on put milk in pot put sugar in pot boil milk and sugar add coffee powder stir contents of pot take a glass pour from pot to glass drink
Now, we could make it less verbose and simple to read, but we often lose control over the finer details - people will start to assume things that aren't already pretty obvious.
add sugar and milk to pot boil contents of pot pour to glass
What are the assumptions people can make here? Maybe they have questions like - What stove is used? What order do we add the ingredients in? Do we stir? When do we get the glass ready?
There is a similar tradeoff in programming languages, which might influence the way some authors write pseudocode - I'd call this the control-complexity tradeoff. The more "complex" a programming language is, the higher degree of control it gives you. When we move on to the actual programming aspect of this series, it'll be more obvious, but this isn't something that's very obvious or relevant to beginners.
Basic Programming Constructs
Consider the coffee pseudocode. Some things were assumed - the existence of the stove, that we know that boiling means, what stirring means, etc. These are the constructs - pre-existing building blocks of a universe - that exist in the situation.
In that way, when we deal with programming, there are certain constructs - building blocks - that we use to make our programs. These are mostly consistent across different programming languages, which is why this article serves as an introduction to Programming, not to Python or C++.
These constructs can be condensed as follows - Communication, Data, Computation, Repetition, Routines, and Abstractions, and are written in terms of writing programs for computers. If you were writing pseudocode to make coffee, you would have a different set of constructs.
Our program is useless if the program can't communicate with us. A conversation is a two-way street - it involves talking and listening. Similarly, with computers, this is known as Input and Output. Input is what we tell the program, and Output is what the program tells us. In our world of pseudocode, we define these things as
Consider the canonical "hello, world" program2 - in Pseudocode, this would read -
Tell "Hello, World"
Now, in regular cases,
Tell would just communicate with us - the programmer - on our screen, but remember that it can take multiple forms, maybe you want to tell another program, or another computer - or maybe you want to tell Facebook or Twitter. However, for us, the first case would be enough.
Now, how do we tell communicate something to the program? We would use
Ask, right? However, this is useless if we can't remember the answer - if you asked someone what their name was but couldn't remember it, it wouldn't have any utility.
This is the perfect place to introduce us to the next construct, which is Data.
We need some way to remember the information we're remembering, right? Let's do that. Introducing our next construct,
Remember "Anirudh" as Name
Why do we need to know what we are remembering something as? This is because by thinking of a shorter, permanent name for something, it becomes much easier to recall. If you have to remember a number, but you don't know what it's for, recalling it when someone asks you what the number was will be extremely difficult. Computers work somewhat similarly.
Solving our first problem -
Remember the answer of Ask "What is your name?" as Name Tell "Hello" Tell Name
Which would give us the output of
What is your name? <you type "Sam"> Hello Sam
The advantage of data is that we can now change things easily - say I write a program where I have to
Tell a specific value many times.
Tell "Something" ... Tell "Something" ... n times Tell "Something"
As humans do, let's change our mind about what we want to
Tell. Now, you have to go and change every line of
Tell "Something" to
Tell "Something Else", every time it occurs in the pseudocode. This is a very tedious process.
Wouldn't it be easier to lean on our memory?
Remember "Hello" as ImportantThing Tell ImportantThing ... Tell ImportantThing ... n times Tell ImportantThing
It's much cleaner, and if we want to change what we're
Telling, all it takes is a change in one line, as opposed to
Remember "Bye" as ImportantThing Tell ImportantThing ... Tell ImportantThing ... n times Tell ImportantThing
Remember almost anything - numbers, lists, text, tables, and sometimes, even other pseudocode, but we'll get to this later.
As a shorthand for
the answer of <Any Operation>, let's just enclose
<Any Operation> in brackets.
In effect, the pseudocode
the answer of first+second will be
Remember the answer of Ask "What is your name?" as name
Remember (Ask "What is your name?") as name
You can have this construct nested, but that is beyond the scope of our concerns for now. :::
All this is cool, but what about dealing with operations such as addition and subtraction? At some point, we'll need to do this. Thankfully, in both Pseudocode as well as programming, this is pretty close to how the real world works.
Remember 1 as first Remember 2 as second Remember ( first+second ) as sum Remember ( first-second ) as difference Remember ( first*second ) as product Remember ( first/second ) as ratio Tell sum Tell difference Tell product Tell ratio
which would give us the output as
3 -1 2 0.5
Because we used
Remember, we won't have to change every line of pseudocode if we want to use different numbers, or even if we want to
Ask and let the user decide what numbers they want to add.
Lastly, another type of computation we can do is comparison -
Remember 1 as first Remember 2 as second Remember (first = second) as equality Remember (first > second) as first_is_greater Remember (first < second) as first_is_lesser Tell equality Tell first_is_greater Tell first_is_lesser
which will, as you can tell by this point, give us the output as
false true true
You'll notice that the comparison type of computation gives us only True or False answers. This is something we will use to our advantage to control what the program does based on data it has in the next section.
Finally, you can also compare the Comparison results too! We can do this using
Remember 1 as first Remember 3 as second Remember 2 as third Remember (first > second) as compare_one Remember (first < second) as compare_two Remember (second > third) as compare_three Remember (second < third) as compare_four Tell (compare_true And compare_four) Tell (compare_true Or compare_four)
It's a little tricky to understand how
Or work, so I'll leave you with this - for
And to be True, both terms compared have to be True - and for
Or, only one of the terms has to be True. This is the same as the
Or that one encounters in Boolean Algebra.
It is a good idea to use these Truth Tables for reference -
|first||second||first And second|
|first||second||first Or second|
::: tip Pat yourself on the back!! You've made it this far, and that was a mouthful. Feel free to take a coffee break (you already know how to make it :smile: in our pseudocode, so it'll be easy). :::
Great. Time to make another assumption!
oh, no, Anirudh, not again!
Oh, come on. You're having fun (I hope). I swear I'm not trying to control how you feel by injecting humor in my post.
Oh no. Now you know.
You can control your program like this too! Introducing the
If-Then-Else constructs - These will take specific steps only if certain conditions are met.
It's especially useful if you're writing a calculator that hates negative numbers.
Remember (Ask "Enter first Number") as num1 Remember (Ask "Enter Second Number") as num2 If (num1 < num2) is True Then Remember (num2 - num1) as difference Tell "Your answer is" Tell difference Else Tell "Please make the second number bigger than the first, thanks"
First, let's get to the space from the margin after the
If statement - This is our way of telling the computer that we want ONLY some things to happen. Wouldn't it be silly if we did in fact make the second number bigger than the first, and the computer still told us to "Please make the second number bigger than the first"? We call this separated unit a section, or a block. It's annoying, but it's important.
Our Biased calculator should work as follows -
Enter first number <you type 1> Enter second number <you type 2> Your answer is 1
AND, it should work as follows -
Enter first number <you type 3> Enter second number <you type 2> Please make the second number bigger than the first, thanks
This is why the Control constructs are so useful - they offer us an extreme amount of control over the program based on data - and being human, we like being in control.
Another caveat is that for the
If-Then-Else block to work, the statement that we check is true or not has to be one that answers in terms of yes or no - simply put, True or False - and our "Comparison" type of computations fit the bill perfectly, so it is this type of result that is used.
This is also reinforcing what some of you may have already suspected - our Comparison type of Computations have a "Binary" or "Boolean" output, in that they can either be True, or False. :::
Last Part! Are you excited? Are you excited? Are you excited? Are you excited? I'm going to ask you until you keep saying yes.
could you be more annoying, Anirudh?
Maybe. My sister asks me this often, and scientists around the world are still confused. However, the utility of asking a question, or doing an action till you get the right answer or result is appreciated and praised around the world as "Determination" and "Persistence", but it's really just repetition.
Consider our calculator that hates negative numbers. What if we don't want it to stop, but instead keep asking until you give a number greater than the first number. Again, this too relies heavily on the comparison type of computation to make decisions on whether something has happened or not.
Remember (Ask "Enter first Number") as num1 Remember (Ask "Enter Second Number") as num2 Repeat Till (num2 > num1) is True Remember (Ask "Enter Second Number") as num2 Remember (num2 - num1) as difference Tell "Your answer is" Tell difference
which would output
Enter first Number <you type 10> Enter second Number <you type 5> Enter second Number <you type 6> Enter second Number <you type 7> Enter second Number <you type 15> Your answer is 5
Now we don't have to worry about getting a negative number because we've bugged the user enough to make sure that they will give only positive numbers.
If you provide an expression that can NEVER be true in the
Repeat Till () is True, it will keep on repeating forever - or until you can kill the program, or your computer. This is known as a forever-loop. It's not fun. Don't do it.
Finally, say we want to write a program to count down from 10 for a rocket launch. How would we do this? With our newfound powers of repetition, we could do the following -
Remember 10 as count Repeat till (count = 0) is True Tell count Remember (count - 1) as count Tell "Takeoff!"
Which would give us the output as -
10 9 8 7 6 5 4 3 2 1 Takeoff!
yay! that was fun! However, because this is a type of repetition that is used so often, most languages have an inbuilt construct for it, which we will discuss later. In principle, that construct does this as well, but it looks much neater. it's called a
For-In construct and looks something like this -
remember [10, 9, 8, 7, 6, 5, 4, 3, 2, 1] as numbers For number In numbers Tell number
This would give us the same output as above.
This was a lot to understand! What do I do now?
Take some time to digest this, but try and apply this technique to solve as many problems as you can. This is a primer to programming, and problem-solving with computers - this encourages you to think in terms of problems, and not in terms of a language.
Look at some problems all around you in your daily life. Is it possible to frame it in such a way that a computer can solve it? Is it possible to write pseudocode which makes it easy to solve this problem?
Here's a small challenge to get you started - in the comments, the first person to write the pseudocode to calculate the sum of the first N natural numbers (where N is something the user provides) will be formally declared the "Ultimate Baller and Coolest Person in the whole Universe".
(Oh, that's also your first assignment)
Don't worry too much about not writing syntactically perfect pseudocode - by design, pseudocode is supposed to be natural language. Solving the problem using the constructs that we discussed is the priority.
Please feel free to leave all feedback - what you liked, and especially what you didn't like - in the comments below. Join us on discord for more discussion on this.
Thank you for taking the time to read this article! I hope this helped you get a better grip on the ideas and concepts that we're dealing with.