Learn to think like a computer, to code like a guru [Part 3]

19 May

The last time I ended my post with two simple questions, and some of you have provided perfect answers, and I was really happy about that. But I still need to make my promise and answer them for you. For the first question which was “What Jazari was going to do if he can’t find resident evil 5 disk on the table?” The answer is that “Jazari was going to hang, since Jazari was not going to be able to execute the command as the disk is not on the table, and as we said before if a computer or a robot failed to execute a command it will hang”

Now for the second question which was “what can we do in this case?” The answer is that we are going to add an extra if command to check if “resident evil 5” is on the table or not, and accordingly Jazari will pick up the disk or not. We will also add an if command at the end of the sequence to check if Jazari is holding a disk in his hand or not and accordingly un-grasp the disk or just say in a metallic voice “Sorry no games found on the table”.

To make it easier to understand I have included the below two figures. The first figure is the command sequence (which we will refer to from now on as pseudo-code, and the second figure illustrates the pseudo-code (this type of diagrams is called flow-chart, so from now on we will use the term flow chart).

Click to enlarge

Let’s explain the pseudo-code, as you can see it looks a bit similar to the pseudo-code we have written in the previous post, As Jazari will move 10 meters, turn left, and then will move 5 meters to reach the table has the games on its top. By this point Jazari will take a look at the table to see if killzone 3 disk is on the table or not, and since it’s not on the table Jazari will execute the commands between “begin else” and “end else”. Which contains another if command that asks Jazari to check if the “resident evil 5” disk is on the table or not. Since the “resident evil 5” disk is not on the table “Jazari” will execute the commands between the “Begin else” and “End else” (line 4.b.1.b.1) which is simply nothing. Finally Jazari will return back to me by moving 5 meters back, turning right, and then moving 10 meters back. At this time Jazari will check if he is holding a disk in his right hand or not. In our case he is not holding any disks so it will execute the commands between “Begin else” and “End else” which is saying using his robotic metal voice “Sorry sir, No games found on the table”.

Robots and computers can execute nothing or simply the “empty command sequence”, the execution of nothing is simply moving on to the next command.

Pretty simple isn’t it? I know that things are becoming a bit more complex now, but the key to understand this is to remember the two rules we have explained so far “Sequence” and “Branching”. Till now we are using them like Mechano pieces, we gather them together to build something useful. So whenever you think its complex look at each mechano piece separately and make sure you understand it very well and move on to the next piece. So take your time to understand each and single command separately, the move to the next command, and don’t forget to use the flow chart to relate commands to each others. keep trying until your mind accepts the way computers and robots think.

Click to enlarge

I think in this example all the pseudo-code is simple and clear except command number four, it looks a little complex. We will need to break it apart to make it easier to explain and understand. Take a look at it in the flow chart you can see that it’s a normal if command with the normal left and right branches (the Begin if, and Begin else). Now look down left branch, as you can see it hold a single “if” command which has its own left and right branches. This was possible because as we said before the “if” command is treated by Jazari as a single command, and since we can put any command we want between “Begin If”, “End if” , “Begin else” and “End else” we can normally put an “if” command without any problems.

When an if command is put between “Begin if”, “end if”, “begin else”, or “end else” of another “if” command, it is called a nested if. Nesting can be up to any level which means that you can have an “if” inside an “if”, inside another “if” which is inside another “if”, etc…

Now as you understand the nested if, let’s agree on a numbering system we will use in our pseudo-code. We will start the count with one, and for each following command we will increase the count by one. This means that if we have four commands we will start by one and end at four. Let’s apply this on our pseudo-code. Please take a look at the diagram and notice that we have eight commands. For the “If” command left and right branches, For the right branch (“begin if” and “end if”) we will add an “a” after the number given to the “if” command and start counting from one for each following command between “begin if” and “end if”. Please take your time to look at the below diagrams to keep following up with me.

Now for any command written between “Begin else” and “End else” (left branch) we will add a “b” after the number give to the “if” command and start counting from one for each following command between “begin else” and “end else”. Again take a look at the diagram to understand what I mean.

Now take a look at the nested if, and make sure you understand its number properly, try to apply the numbering rules mentioned and the common sense. I hope it is simple enough.

What have learned during this post

  1.  A new computer vocabulary: pseudo-code, and flow-chart
  2.  The fact that computers can execute empty commands by doing nothing and moving on to the next command
  3. Nesting concept for the “if” command
  4. How to number the commands in a pseudo-code

 Your assignment
Now as an assignment I want you to write Pseudo-code and draw the flow chart for the following case. I want Jazari to bring me a cold beverage from the fridge, if there was no cold beverage in the fridge Jazari can bring me my favorite “Baklava” which it is in the fridge too, and if was no “Baklava” in my fridge I want Jazari to return back to me and say “Sorry sir, You don’t have any food in your fridge. You might consider to ask for a pizza delivery”.

I can’t stress how important it is to make this assignment, as it will help your mind think like a computer which is needed if you want to be an excellent coder one day. Keep trying an don’t give up easily, even if you think you can’t do it correct, continuous trials will challenge your mind which is really desirable in any learning process. If you faced any trouble with this assignment feel free to contact me.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: