Hello, welcome to Part 2 of the series. In this section, we’ll start with some actual code writing. “Code” is a short term to refer to “programming lines” i.e. instructions. So when someone is “coding”, they’re actually “writing programs”. I’ll use that word for short.
We’ll start with writing some instructions in pseudo-code. What is pseudo-code? Does it mean pseudo-programming? Yes, sort of.
You want to make sure you understand how to write and understand pseudo-code because that’s what I’ll be using through the rest of this series. They’re easy, English-like statement so don’t worry too much.
Pseudo Code
Pseudo-code refers to instructions written in a structured language. That is, there are specific words to represent specific things. Like commands for e.g. You’ll understand what I mean soon. The good thing about pseudo code is that it is programming language-independent. You can write instructions in pseudo-code then hand your sheet to any programmer and he or she will be able to write a program based on that. Why? Because pseudo-code represents instructions in an independent way. The programmer will then be able to translate those in the language of his/her choice with appropriate syntax.
In short, pseudo-code are step-by-step instructions to solve problems, written in a “strict” way that can then be used as guideline when writing a program.
Algorithms are usually step-by-step instructions to solve a problem. You could say that pseudo-code is a way to write algorithms. For e.g., some algorithm in mathematics can say, you need to square this, differentiate that. In pseudo-code, you’re just writing the things to do in a structured way. So for the purpose of this article, I’d say they mean the same things. Just step-by-step instructions.
How do programs begin? They start in the mind of a programmer as a series of steps that must be completed to solve a problem. Programmers think in terms of pseudo-code, and then use those to write programming language codes. Sometimes they write these instructions on paper or a whiteboard to make it easier for them, but for simple things, they just do it mentally. It becomes simple enough with some practice.
To get you started, let’s assume you’re telling a friend how to draw a simple house. You need to tell them instructions. What would you be telling him/her? Write down your commands.
If I were to do that, I’d probably write those lines:
Draw a square, each line being 10 cm. (walls) At the top of the square, draw a triangle, touching the square. (roof) Inside the square, draw two smaller squares near the top. (windows) Near the bottom of the square, in the middle, draw a small rectangle. (Doors)
Hopefully, that should give you a decent-looking house drawn like a 5-year old. Congratulations, you just wrote some pseudo-code!
Let’s see another example. We want to have the computer ask the user for two numbers, add those two numbers and then show the result to the user. Simple enough, huh?
This example has 3 sections: an input section (asking the user), a processing section (add numbers) and an output section (show the result). Those 3 steps are the basic things that happen in programs. These 3 things occur in almost all programs, although they may not be very apparent in all programs.
So let’s see how we can write pseudo codes for this problem:
Input 2 numbers, a and b. Result = a + b. Output result.
Simple, yes? This is pseudo-code for addition of two numbers. If you look carefully, you will see I used “a” and “b”. These are called variables and we’ll see about those in the next part. Basically, these act as two generic numbers so whatever 2 numbers the user will tell the computer, they will be represented as a and b.
This is a simple set of instructions and a number of things could go wrong in that. It’s also fairly limited in terms of functions. What if you want something else than addition? What about multiplication? Can’t do. Actually, we can. We will do this in the lesson about conditions. What about adding more than two numbers? Can’t do. Oh yes we can, but in loops lesson! 😀
Normally pseudo-code has a fairly strict syntax so that code written by different people. If a person writes “Input 2 numbers” and another “Enter 2 numbers”, it’s still ok. But if a 3rd writes “Shout 2 numbers”… then no. Thus, pseudo-code uses a restricted number of words in English, normally words which you will normally find in other programming languages. For e.g. “input”, “output”, “if”, “else” etc. You’ll learn about those words as we go along.
Batch Jobs
Sometimes, programs don’t ask the user for any inputs. They just take their inputs from some file or from some other place. Batch jobs, or batch programs, are those programs that run without the user having to supply any data during running. Data, as in numbers, text or other information. They run, do their processing and could do some outputs, although not all of them do.
Let’s see about two examples of batch jobs. First example will read a series of numbers from a file and add them all together. If you don’t understand some parts, don’t worry too much. I’ll cover them in next parts. So, here it is:
Open numbers file. While there are more numbers to read in file Add number just read to Total. End Output Total. Close File
Here, we didn’t ask the user for inputs. We just get numbers from a file and add them all to a total count then show that to the user. The “inputs” are actually read automatically so the program can run fast, i.e. doesn’t have to wait for the user to supply numbers one by one. Thus we had to tell the computer to open the needed “numbers” file, read off it and finally close it when it’s done.
Note one thing: the “Add number just read to Total” is a bit off-sided as compared to the rest. This is called “indentation” and it makes reading code blocks easier. At a glance, we can know that this line falls under the control of the “While block”.
Which do you prefer in terms of clarity:
While there are more numbers to read in file Add number just read to Total. End
Or:
While there are more numbers to read in file
Add number just read to Total.
End
Guess I already know the answer.
Code blocks are important in programming since they normally do different things as the program is running. So seeing what code is running under the control of which blocks is very important and I encourage you to properly indent your code when you are writing inside blocks. You will learn what this particular block of code does if you read the “Loops” part of this series.
Let’s see another example. This time, no inputs, no outputs. Just processing. The “output” is actually writing to a file. We’ll do the same thing as above: read numbers in a file and find their total. But we’ll then write this number in another file.
Open numbers file While there are more numbers to read in file Add number just read to Total End Open results file Write Total to results file Close results file Close numbers file
This time, we don’t show the user anything. We just read from one file and write to another. Basically, if this program was ran, the user would just see the program start and exit, and nothing else happen. They will have to open the results file to see something had happened.
That’s it for this part. I hope you enjoyed it and learnt something interesting. If you have questions, just ask in the comments below. Again, I ask programmers out there to please provide suggestions and comments. Thank you for reading and see you in Part 3.
[seriesposts title=”In the same series:” titletag=”h3″ listtype=ul show_date=0]