- Part One
- What do you need to start?
- The program layout (organisation) and syntax
- And what if there is an error?
- "Hello World"
- Input & Output
- Swapping numbers
- Reading and writing multiple variables
- The IF condition
- The CASE condition
- The FOR loop
- The WHILE loop
- The REPEAT loop
- Checking whether a number is positive or negative
- Writing the first positive N numbers
- Calculating the sum of positive numbers
Nowadays, there are so many programming languages that it is quite hard to decide on which to start with. However, what people usually fail to realise is that it's not all about the language - what use is it if you don't know how to solve a problem? When looking at the problem from that angle, the best choice would be a language with syntax that is easy to understand and learn, while at the same time it is powerful enough for complex operations. Starting out with Java or a C is OK with me, but in my opinion, beginners should stick to something "lighter" - like Pascal.
If you have been convinced by the introduction paragraph, continue reading.
What do you need to start?
Since Pascal is as thoroughly developed as C# for example, you won't have to spend hours downloading the tools. There are several good compilers out there - the most popular ones are Free Pascal and Turbo Pascal. You might find it a bit hard to adjust to using it, but it's one of the best choices, being free. As soon as you acquire a decent compiler, you are ready to go!
The program layout (organisation) and syntax
As this tutorial is for complete beginners, the programs will not be complicated. This taken into consideration, I will only mention three (somewhat) important "blocks" of a program.
At the very beginning of the program there can be a line defining its name. I say can be because this part is not obligatory. It is, however, good practice to use it, since you might find it helpful in the future. The correct way to define the program name is:
program programName;"Program" is a reserved word that must be used this way. The second part, "programName", is obviously the program name. It can contain lowercase and uppercase letters, as well as numbers underscores. If you leave out this line of code, the program will work all fine, but if you decide to use it, be sure the name is descriptive enough - don't just put "myFirstLongProgramOMG".
The next important part is declaring the variables. You will use these in 99 per cent of your programs, but again, it is not a must-have. The variable block starts with "var" (another reserved word) and continues until the compiler bumps into another reserved word - that tells him there are no more variables. You will be able to read more on this topic in the next chapter.
And finally, the main part of the program - the code that will be executed. Simply enough, it should be enclosed between two reserved words - "begin" which marks the start of the code, and "end." (with a full stop) which marks the end.
Now that we have the main block, we can talk a bit about the commands. The most important thing to remember is that almost every command must end with a semi-colon. There are some exceptions that will be mentioned later in the text. By having these semi-colons, you are limiting each command, so you can practically write the whole program in one line. Of course, this is not advisable, as it will make your code extremely hard to read. A rule of the thumb is to use a new line for each command, and indent it if necessary. You might also want to put blank lines between groups of commands. Here is an example of am (aesthetically) well-written code:
program wellWrittenCode; var // declaring the variables begin // command one // command two // command three // command four // command five end.Hang on, hang on! What are those double slashes? Those are one-line comments. When writing a longer program, these can help you find a specific piece of code easily. Use them well, and they will prove to be very handy. Anyway, back to the example. Here we have all the parts that were talked about - program name, variable declaring, and the main program. As you can see, commands in each block are indented only once, and grouped if necessary. Later on, you will see the advantage of multiple indenting, when the examples get more complicated.
Now we shall concentrate on the "var" part of the program. As already, explained, this is the right place to declare each variable used in the code. For every variable you must use a unique, non-reserved name (consisting of letter, numbers and underscores). Furthermore, every declarations consists of two parts - first come the new variables (their names) and then their type. When declaring more variables for one type, these should be separated by commas. Here are several examples:
var var1: type1; var2, var3: type1; var4: type2;"var1" to "var4" are variable names which will be used in the program, and "type1" and "type2" are data types that the variables will be. In the var block, the variables are assigned a data type by using a colon. And don't forget to put a semi-colon after each declaration!
Let's talk a bit about data types, since these are very important. In Pascal there are many data types - however, since we are just starting off, I will only mention two, both for numbers. The first, and probably most important one is integer. An integer is a whole number between -32768 and 32767 (this might vary depending on the compiler, but this is the safest definition). The second one is real, and it can hold decimal values (e.g. 25.2341). Its range is quite big, so you won't have to worry about it.
OK, so now we have some variables. And how do we assign them values? Quite simple. I guess you remember we used a colon for declaring variables? When assigning them values, we do not use an equal (=) sign, but colon equal - :=. The equal sign is used in true/false statements, which is covered in the next part of the tutorial. To make things more clear, here's an example:
program variableValues; var x, y: integer; z: real; begin x := 24; z := 2314.8375; y := 3.1415926; // this line would give you an error, since you are try to give an integer a real value! end.In bigger programs, give your variables better names, so you can easily know which one holds what value.
And what if there is an error?
In the previous example, we've had an invalid line. In such cases, you would get a compiling error and the number of the line where it occurred. This is usually enough to help you set things straight. There errors are mostly easy to fix if you know the correct syntax. Other things that might produce an error include a missing semi-colon, no "end." etc.
On the other hand, an error can occur while the program is being executed. For example, you ask the user to enter an integer, and he/she enters a real number. The program would stop working, all because of the user. That's why, besides being careful about not making any coding mistakes, you must also be careful when dealing with user input.
For those not into programming, Hello World might not mean anything, but it is a rather important part of the programming process. Every beginner programmer is advised to make a Hello World program first, so he can get a glimpse of how the language works. Basically, all that this program does is write the sentence "Hello World!" on the screen. Just copy this code into a program and run it (remember, we don't always need the var part):
program helloWorld; begin writeln('Hello World!'); readln; end.
Short and simple - we only have two commands! The first one, writeln, writes a new line to the screen containing the text specified between the parenthesis. Notice that the text must be limited by apostrophes, and not inverted commas as it is in some other languages. The readln command is there to stop the program from closing itself. If it weren't there, the program would end after writing "Hello World", and the user would see it (it would happen really fast). This way, we tell the program to read one line (the user has to press ENTER), and then close. You can read more about these two commands in the following chapter.
Input & Output
For outputting data you can use two commands - writeln and write. Writeln writes the data and then moves the pointer to a new line. So, several writelns would give you several lines. On the other hand, several writes would print it all out in one line. You can write any variable, as well as custom text (like in the helloWorld program). If you want to write several variables, you can separate them with commas - writeln(a, b, c, 'Text', d);
Sometimes you don't want to assign values to variables yourself, but have the user enter them. There are also two commands, read and readln. When reading data enter from the keyboard, the difference can't be noticed, so you can actually use both; I advise you to use readln whenever possible. Just like for writing data, you can also choose to read multiple variables: readln(a, b, c);
A user is required to enter two integer numbers, which would then be stored in two variables - a and b. The program should then swap the variable values. For example, if user enters 3 and 4, the program would assign a the value 3, and b the value 4, after which it would swap these values, making a contain 4 and b contain 3.
program swappingVariables; var a, b, temp: integer; begin write('Please enter two integer numbers: '); read(a); read(b); temp:=a; a:=b; // we have "temp" so with don't lose the value contained in "a" in this line b:=temp; end.
Reading and writing multiple variables
A user is required to five integer numbers, and the program should write them in the opposite order.
program multipleVariables; var a, b, c, d, e: integer; begin write('Please enter five integer numbers: '); readln(a, b, c, d, e); // reading it all at once writeln(e,' ',d,' ',c,' ',b,' ',a,' '); // we need to have blanks between numbers readln; // so the program doesn't close end.
And that's all you need to know to start learning Pascal!