How to build logic for programming? Zeroth law of coding. — WithShivam
Thinking of how everyone writes such awesome codes, and you can’t? You try a lot but you cannot build your skills. Looking for an answer to how to build logic for programming? Don’t worry. You are at the right place. Read the whole post and you won’t regret.
Prerequisites.
Before you start, I want you to ask these questions to yourself.
- Do you know what a computer program is?
- Why do you want to code? Because Computer Science has a good scope or because you want to build something?
- Do you find computers interesting? After seeing Google Search Page do you feel how can there be my name instead of Google’s logo?
- Do you feel you can break things up while doing random things on a computer?
I hope you have all the answers. I want you to keep these in mind until the end of this post.
What a computer is and how it works?
A short answer: Computer is your that friend who can’t do anything on his own. You have to give instructions to him every time you want him to do your work. (This will be the base for rest of this post.)
Now suppose you want him to help you in making noodles for you. Guess what you would say to him? Think of a conversation between you and him right now. Once done that read the below snippet:
Me: Hey friend, make Maggi for me.
He: Wait. I don’t know what a Maggi is.
Me: Ohh! Here are the instructions:
1. Put water to boil.
2. Pour the Tastemaker in the water.
3. Pour the noodles.
4. Wait for 5 minutes.
5. Tell me when it is done.
He:
Wait. I don't know what a put is.
Wait. I don't know what a water is.
Wait. I don't know what a boil is.
Wait. I don't know what a Pour is.
Wait. I don't know what a Tastemaker is.
Wait. I don't know what a noodle is.
Wait. I don't know what a wait is.
Wait. I don't know what a minute is.
Wait. I don't know what a tell is.
Wait. I don't know what a when is.
Me: 😳😳😳
(Goes to the library and gives him a book on all these things.)
He: Maggi is ready.
2 days later.
Me.
Thinks: Hell. I want him to make Maggi again. But he is dumb.
Hey Friend, take this page and do what is written on it every time I ask you to make Maggi.
Content of the page:
1. Read the books on water, boil, pour, put, Tastemaker, water, noodles, wait, minutes, tell, when, done.
2. Put water to boil.
3. Pour the Tastemaker in the water.
4. Pour the noodles.
5. Wait for 5 minutes.
6. Tell me when it is done.
He: Okay
Me: Make Maggi for me
He: Done.
Before you continue. I want to clarify this guide covers all audience, from absolute beginners. So if you are pro, knows a lot of computer basics and have a shortage of time, you can directly jump to How to start writing code section below.
If you didn’t skip it.
Now, if I tell there is not even a single difference between this ‘friend’ and an actual computer? Yes. A computer is exactly what this ‘friend’ is. Soon you will build logic for programming, so I want you to think about it yourself once. Now I will explain to you the whole snippet above in terms of a computer. The first thing we saw that your Friend cannot do anything on its own. Same as for a computer.
Until you ask a computer to do something, he won’t do anything. Keep this in mind because this is the first thing to start programming. Now since your friend didn’t understand when you said ‘Make Maggi’, you had to give him exact commands. The computer runs on commands. But wait. Even when I gave commands, he wasn’t able to make Maggi. Because he didn’t know the basics too. You gave him a book from which he got to know the basics. Do you know what these books are in terms of the computer? They are dependencies. These dependencies help the computer to know the basics.
But I love Maggi 🙁
Now, You had to repeat the same procedure to make him make Maggi for you, so you found another solution. You gave him a piece of paper on which there were instructions on how to make a Maggi. What if there were a computer? Well, that piece of paper in terms of a computer is known as a file. But don’t you think a piece of paper can have anything written on it? It can have some story, it can have some pictures, it can even have some raw data about India’s population (Remember that word, data). So what we will be the name for specifically that file which has instructions on it? It is an executable.
Files.
If you have ever used a computer you must have seen files. When you see a picture that is an image file, similarly there are video files and remember, when you have to play a game, you double click on a file? That is an executable file. Means it has instructions in it. One more thing. See the first line of the executable file above. You asked your friend to first read some books on the basics of what he will be going to encounter later in the set of instructions. This same is called importing of the module when writing actual code. We import the library for whatever basic things we will need to use in writing instructions.
But wait. How did you interact with your friend? Obliviously by talking. But how did he talked? From where he got the command required to talk with someone, to understand what is written on that page? Well, that is the Operating System. The operating system is a set of instructions which help a normal user to interact with a computer. So I think that is enough basics to get ahead with some programming. So get ready to build some logic for programming.
What is a logic?
There are two types of people in this world. One who can make good tea, others who cannot. The category who cannot make good tea is can be further classified into one who doesn’t want to make tea, others who try but they fail to make a tasty tea.
If the making of tea meant writing code, the one who can make good tea would be a good programmer (or coder). The one who doesn’t want to make a tea would be the one who doesn’t have any interest in coding (or even tech). But for the third one, we would say his logical skill is not good. Which means he tries but he is unable to write the correct set of instructions on how to do some task. In other words, He fails to tell ‘ His Friend ‘ how to make Maggi. But don’t worry. If you are in the third category, read it to the last, and I will try a lot to make you enter the first category.
Writing the first piece of code.
So here we are. We will be writing a page which has instructions on it, hence that page will then be an executable page (file). But wait. There is a lot before that. First, we will dive into what a program is and how it works. We will look into how to write good code and how to build logic for programming.
What is a program and how it works.
Do you remember that Friend? Remember we had to give him a page on which we had written set of instruction. And that page was called an executable file. Also then you must be remembering dependencies. Yes, the books he needed to perform basic operations. Now if I say someone stole those books. Do you think he will be able to make Maggi now? Absolutely not. He will be stuck searching for dependencies. Then he will throw an error, Missing dependencies.
Suppose you have also given him a page with instructions for making tea in it. He will not be able to execute this file too, because it will also be depended on the ‘pour’, ‘boil’, ‘wait’ and other modules. But he doesn’t have the books from where he could get to know how to perform these basic steps. So if any of the required dependency is not available, the program cannot run. So what can I do? My code won’t run if any of the dependencies are missing, true?
Ah! Dependencies are necessary.
What if I attach all the dependencies with the executable file? By linking all the libraries with the page with commands, what I get is an executable file which doesn’t need any external dependency to run. And what we get is a standalone executable. Feel this word. Stand Alone. Someone is standing alone. He doesn’t need anyone. He is on his own.
But his mother tongue is Hindi 🙁
But wait, there is one more problem. The page you gave to him is in English. Don’t worry, he knows English very well. He can read the command line by line, convert it into the language he thinks in (Hindi), and does the job. Again, He reads the commands line by line, executes them line by line. Have you heard this line before? Yes. It is an interpreter. In this method, let’s suppose you have made an error in the 4th line of code. What will your friend do? He will execute line 1, 2, then 3 and then he will get to know that Ohhh! You have made a mistake on line 4. Which means you will get to know about errors only at the runtime (When your code will be executed).
But the other thing is. Well, you might have guessed. What if he converts the whole page into Hindi once just once, and guess what. He will get to know all the errors at the time of converting it itself, i.e. even before running the program. This method is called compiling. We convert the whole code at once and store the converted version. We don’t need to convert it again and again. There will be one more advantage. Don’t you think the second one will be faster? He has already converted whole English into Hindi, and now every time he is running this task what he gets is directly the thing he understands. Definitely, it will be fast. So that was the discussion about compilers and interpreters. So here comes the end of the basic discussion about computing. Now we will dive deep directly into how to build logic for programming.
How to start writing code.
Disclaimer: Everything below is purely my imagination and based on my experience. Someone else may have his own theory.
This is the part where most of us does make a mistake. I don’t think starting directly with some coding language is a good thing to do. I would rather suggest that start by exploring your computer. Start by exploring some cool things, explore some websites and make an interest. Think how great it will be to make a tech.withyourname.com. Think how is it done. How are websites made? Google it and collect some interesting things to do.
Now I will tell you a key. A key to master logical skills.
See, everything in this world is based on two conditions. First one is if-else, and the second one is goto (or jump). Ohh, wait. You might want to ask what a jump statement is? Don’t worry I will explain. Remember your Friend? You had given him a piece of paper on which you have written instructions to make Maggi. But suppose you want him to execute line 5 directly after line 3. So you can put a command like: Jump to 5 directly after the 3rd command. These type of commands are called Jump Statements.
I bet everything is based on this. See around you. Think of everything. How everything is done. I bet you can break every single task your mom ever gave you into these two things. For example, your mom asked you to go to market, bring some ladies finger. If the cost is too high, instead bring some cauliflower.
Think, Think, and Think.
Now I want you to think, how is this related to if-else and jump statements. Build the whole scenario in your mind. Think of how your mind would have done this. Pause reading and give it some time. In the meanwhile, if you are enjoying the article, it would be awesome to tell your friends that you are reading such an awesome article. Let them know how to build logic for programming. Also if you aren’t enjoying the article, please leave suggestion/feedback in the comments section down below. Oh! did you notice something? Were you able to see the if-else condition in the above line 😉? Okay, so you continue. Pause the reading and start thinking right now.
Done that? Okay so think with me this time. Go through the below snippet once.
Suppose the grocery store is on the first left turn from your house.
1. You receive a command to ‘bring ladies finger if in the budget, else bring cauliflower.’
2. Take a bag.
3. Start moving.
4. Move 1 meter.
5. If there is not a left turn, jump to 4.
6. Turn left.
7. Move 1 meter.
8. If there is not a grocery store, jump to 7.
9. Ask him what is the price of ladies finger.
10. If the price is less than the threshold value (budget prize), jump to 14
11. Purchase cauliflower.
12. Put it in the bag.
13. Jump to 16
14. Purchase ladies finger.
15. Put it in the bag.
16. Turn about and start walking towards home.
17. ………… ( this much is enough for some basic understanding)
Boom. If you were able to write that yourself, congratulations. You have written your first code. Yes, that was a program in itself? But how? Don’t you think it is just a set of instructions? And the set of instructions is called a program. I won’t explain you the above code. But I want you to understand it yourself. I want you to focus on line 11 and 14. Think about if they both can execute in one run or not.
Variables, Memory and Files.
This is another major concept in developing a program. If you want to build logic for programming, you must master it. Don’t worry. Our Friend will help. Let’s come back to the if-else problem. Remember you had gone to the market to buy ladies finger. Now suppose you are midway, and a friend of yours calls you. You have some great talks. And at last, you reach the store. But hey. What was I supposed to purchase? I forgot everything. Have you ever experienced such a situation? If yes, great. So let’s extract some knowledge out of this.
Why I forgot?
Before asking the question that why you forgot, I want you to ask yourself, why was I remembering it? I didn’t write it anywhere. So how was it even in my mind. How was I remembering it? Vo to dimag m h yrr. (Obviously, It is in the mind). When your mom asked you to buy ladies finger. You mind stored it into his memory. Your mind stored that “I have to buy ladies finger”. But not only that. Your mind also stored the whole procedure, like how would you reach there, the code we built above using if-then and jump, is also stored in your mind. This one is out of the scope of this post, but I will surely cover this too in some next posts. Till then if someone knows anything about this, I want you to describe in comments.
Okay, so lets come back to topic.
Don’t you think whatever we have to remember is written somewhere in our mind? This is the memory. Suppose you want your Friend (or Computer) to add 3 into 5 and tell you the answer, what will be the first step? Obviously, the first step will be to store the two numbers in memory. Also, you might ask since our Friend is dumb, and he cannot do anything on his own, they how he manages to store the number in memory? We have not given any such command on where in the memory should he store the numbers. So how he does it? Do you remember I had told you something called an Operating System? Memory is managed by the operating system. His operating system is responsible for managing how to store something in his mind.
But when my friend had a great chat with me, I forgot the older things.
Before continuing, Don’t you think each human has different memory strength? Some can remember a lot of things and some forget things easily? Some have high memory capacity, and some have low? Keep that in mind for the rest of this part. So, when my friend had a chat with me, think about what would have happened… Some if-else and some jump statements would have been executed. True? Try to build this whole scenario using if-else and jump.
Now, to deal with friend’s call, a lot of scripts would have executed in our mind? Don’t you think so? Don’t you think it would need some memory? If not, try one thing. Recall the last interaction between you and someone, and just list some things you had to keep in mind while talking with him. The list will not be small. So what will eventually happen is, we would require some free space in our mind where we can store this new list? But suppose your memory goes full. You don’t have any more free space available? What will your operating system do? He will erase some older data from the memory and will use it for the new data? Isn’t it? This is called memory management. And this is done by the operating system.
One more thing.
So we have seen that any data in this type of memory can be lost anytime. What if we want to store permanent data? We don’t want to lose it anytime. Suppose we have to store the password of our email account. Obviously, the mind is not a very good place to store permanent data. Where would you store such data? Obviously on a paper. Wait. I had told you something. What is a paper? It is File. A paper is a file. So we store permanent data in files. And file can store anything. From executable (Remember the Maggi script for your Friend?) to pictures and text. Everything written on a paper is permanent. So that means if you have to store anything permanently, you would use files. And to build logic for programming, you must know the basics of files too.
Next, suppose you want to store some phone numbers. Obliviously you will store them in a file (paper). But wait. Have you ever noticed there is some special type of notebook for them? A diary. It has tables, and we store only a special type of data there (Phone numbers). This diary is represented by a database in terms of a computer. This one is also out of the scope of this post, but we will look into it later in some more posts.
Let’s come to the finale. Lets write a code.
But in what language? The answer is in ‘no-language’. Yes in a fictional language. I would rather name it the zeroth language of code. Since we just want to build logic for programming, we don’t want to turn it to any program. So we will start it in a fictional language. The zeroth language of code. Read the conditions below before starting anything.
Conditions for Zeroth language of code:
- It won’t have any predefined format. Feel free to write statements as you wish.
- The code will be executed line by line.
- You can keep any data in memory.
- Fundamental operators like + , — , * , / , % (reminder) can be used.
- You can use if-else.
- You can use Jump.
- Every statement has to start with the line number.
Where to find problems?
One word answer: Around you. Anything you do, try to convert it into some script on how to do that work, using the Zeroth language of code.
The first problem.
You have paper and a pen. You have to ask for a number from your friend, and you have to draw the following pattern on the paper based on the number:
*
* *
* * *
* * * *
* * * * *
.... upto the number
Some of you might have seen this before. True? Okay, let’s solve it. The first thing you can see is you have to ask for a number. and thus you will need to store that number in the memory too. Now think once. How would you have done it actually? Means if right now I ask you to draw this pattern up to 7 lines, what would be your approach? Ask yourself. You will start line by line, true? You will start with the first line and print stars equal to the line number in each line, up to 7 lines, true? So why can’t you use the same logic in your program? You can. Definitely, you can. Keep this logic in mind till the rest of this part.
So let’s start:
What was the first thing? We had to ask our friend for the number.
1. Ask friend for the number.
Is that enough? Do you think it is enough? No. It is not. We have just asked our friend for the number, but we didn’t store it anywhere. If we don’t store it, how would we use it later? One more thing. We will store it somewhere in memory, but how would we later know where we had stored it in memory. A solution is to let us give it a name. We can then reference this memory using that name later.
1. Ask friend for the number and store it at memory named NUM.
That looks cool, right?
Okay, lets now start printing the stars. Wait, what else I need for stars? I need to print them line by line. And on each line, I will print starts equal to the line number. So I will have to remember the line number. So I will have to remember the line number too. I must know on which line I am to print the same number of stars in that line. Also, the line number will start with 1. So we should create a memory for that too. And for the starting, we must set its value to 1 too.
One more thing is, can you see, something is being repeated. Does the pattern have a rule? Print stars equal to the number of lines. Whenever something is repeated, we have to do some set of instruction repeatedly. Which means we will have to use jump. we will need to jump to previous statements. (If you already know some language, I recommend don’t ever use jump statements, instead use loops.) . How will we print exactly as much number of lines as our friend told us? Since we have the current line number with us, we can always check if the current line number is greater than the number of lines we were provided with, then we have to end the program.
One more thing. After each line print, we will need to increase the current line number by one, because the line number is maintained by us we only are responsible for increasing its value by one after each line so that it always gives us the current line number.
1. Ask friend for the number and store it at memory named NUMOFLINE.
2. Store 1 at memory named THISLINE
3. If THISLINE is greater than NUMOFLINE, jump to 8
4. Print stars THISLINE times.
5. Move the pen to the start of next line.
6. Set THISLINE equal to THISLINE + 1
7. Jump to 3
8. End
That looks awesome man!
I want you to go through this code yourself. And check if it is nice. And after that I want you to give this code to your Friend. Will he able to run this? Think. Can he execute it or are you missing something? Okay for some hint I want you to put your attention on line number 4. Print stars THISLINE times.
Do you think he can do that? How will he know how to print something a number of times? He will through an error. 'I don't know how to print something something times'. So what should we do now? I will have to add another logic to print something some number of times.
A way of doing this is by putting the logic for this in the code itself, at the place of that line. But again, this code looks beautiful to me and I don’t want to make it even more complex. I don’t want to put its logic in place of that line. I like this way of telling him to print something 3 times. Why not we tell him how to print something some number of times? Kuch yaad aaya? (Remembered something?) Our Friend didn’t know some basic things so we gave him some books from which he can know the basics. Why not we write a script to print something some number of times tell our main code to read it before executing the main program? By this way, he will get to know how to print something a number of times and our code will still be as beautiful as it was.
Write your own module (Or function).
So let’s think about how to write a module. How to tell our program how he can print something a number of times. Well, there is a method. Put yourself in place of the module you want to make, and one of your friend in place of the main program. Now think your friend called you and said He wants you to print something repeatedly. What will be your first response? Obviously, you will ask him what to print and how much times it has to be printed. That means your friend would need to pass you 2 things, one what to print, second how much times. Okay, he passed these things to you. What will you do? Obliviously, store both things in memory. And then to print something repeatedly, you must be tracking how much have you already printed. And once you have printed that the required number of times you would want to say your friend that you have done that work. In other words, you would want to return the further task of program execution to the main program (your friend).
#printrepeatedly
1. Pass me 'what to print repeatedly' and store it at memory named WHATTOPRINT.
2. Pass me 'how many times to repeat it' and store it at memory named TIMES.
3. Store 0 at memory named CURRENTNUM.
4. If CURRENTNUM is equal to TIMES, jump to 8
5. print WHATTOPRINT.
6. Set CURRENTNUM = CURRENTNUM + 1.
7. Jump to 4
8. Return to whoever called me, and say 'I have done your work successfully'.
Read that? I want you to go through these very deeply. Try to understand it. Once you got that, think is it enough? No. Still, a very important thing is missing. We didn’t yet tell out our main program to read this page. He still doesn’t know how to print something repeatedly. So, let’s make some changes to that too.
#mainprogram
1. Read the page #printrepeatedly
2. Ask friend for the number and store it at memory named NUMOFLINE.
3. Store 1 at memory named THISLINE
4. If THISLINE is greater than NUMOFLINE, jump to 9
5. Call printrepeatedly and pass '*' and THISLINE to it.
6. Move the pen to the start of next line.
7. Set THISLINE equal to THISLINE + 1
8. Jump to 4
9. End
Read it again. Boom. Your logic is ready. Try to execute it in your mind. Take some random number and try to draw the pattern.
What next?
The post has already gone very long. But I think this much is enough for today. I want you to focus on this zeroth-language, and try to convert everything around you, everything your mom asks you to do, into if-else and jump statements. Believe me, if you mastered these two things, your logical skills will become a lot better. You will be able to write fluent code. But for that, you must do some practice. Below I have left some more patterns, and I want you to make a script of these patterns in the Zeroth language and submit your code in the comments below. For any queries, feel free to contact me using the contact us box below.
Very soon, I will publish the second post on this channel, and believe me. That will also be as awesome as this one. So make sure to subscribe to this blog so that you won’t miss anything important.
Who am I?
I think this is the best place to tell you who I am. If you found the post interesting, and want to know who I am, head on to My Portfolio, https://me.withshivam.com. Want to read more of my posts? Head over to my blog, code.withshivam.com
A Small Request.
If you found this post helpful, it would be awesome to share it with your friends on Facebook, Instagram, Whatsapp, Twitter, and wherever you can. It would mean the world to me. Also while sharing, don’t forget to use the hashtag #WithShivam. It will help me find the post. I would love to see some shares.
Some more questions to do.
Try drawing these patterns:
* * * * * * .......... n times horizontally
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
. . . . n times vertically* * * * * * * ..... n times
* * * * * * ..... (n-1) times
* * * * * ..... (n-2) times
* * * * ..... (n-3) times
* * * ..... (n-4) times
* * ..... (n-5) times
* ..... (n-6) times, until only one is left.1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
.
.
.
.
n lines1 2 3 4 5 6 7 ..... n times
1 2 3 4 5 6 ..... (n-1) times
1 2 3 4 5 ..... (n-2) times
1 2 3 4 ..... (n-3) times
1 2 3 ..... (n-4) times
1 2 ..... (n-5) times
1 ..... (n-6) times, until only one is left.1
2 3
4 5 6
7 8 9 10
11 12 13 14
.
.
.
.
n lines* * * * * * *
* *
* *
* *
* *
* *
* * * * * * *
//Square of side n - The tie breaker question.
Thanks.
Originally published at https://code.withshivam.com on May 12, 2020.