pyost 0 Report post Posted March 24, 2007 This is the first part of my Pascal tutorial for beginners. Here is what the complete tutorial contains, and it might get expanded (some parts are not written yet): Part OneIntroductionWhat do you need to start?The program layout (organisation) and syntaxVariablesAnd what if there is an error?"Hello World"Input & OutputExamplesSwapping numbersReading and writing multiple variables Part TwoConditionsThe IF conditionThe CASE condition LoopsThe FOR loopThe WHILE loopThe REPEAT loop ExamplesChecking whether a number is positive or negativeWriting the first positive N numbersCalculating the sum of positive numbers Introduction 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 variablesbegin // command one // command two // command three // command four // command fiveend.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. Variables 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. "Hello World" 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 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); Input 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); Examples Swapping numbers 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 closeend. And that's all you need to know to start learning Pascal! Share this post Link to post Share on other sites
bluefish1405241537 0 Report post Posted March 24, 2007 Nice tutorial. Very easy to understand, and very simple, which I suppose was your goal. I now know how to program Pascal without ever writing a single line of code. That's kind of cool. I've never used Pascal before, but it seems like a pretty good language. Personally, I prefer to stick with the more complex stuff (C#/Java, depending on the project), but I think I'll have a go at Pascal now that I've read this tutorial. It seems more like VB than anything else I've used (actually, I think it's most like Delphi, but I haven't used that so I can't be sure). It's nice to have some built-in stuff instead of having to import it all (includes in C/++, using in C#, import in Java). It would certainly be a good building block to base learning programming on - functional, but without unneeded complexity. I have yet to see how it stands up for really big projects, or with graphics, but from first glance it seems to be a good one. Share this post Link to post Share on other sites
pyost 0 Report post Posted March 24, 2007 Personally, I prefer to stick with the more complex stuff (C#/Java, depending on the project)Of course, you should do so if you can because Pascal can't really be compared to C#. If you, however, don't know these "complex" languages, starting with Pascal is a good idea. actually, I think it's most like Delphi, but I haven't used that so I can't be surePractically, Delphi is Pascal, just object oriented. Once you've learned Pascal, you can easily transfer that knowledge to Delphi and create serious Windows applications. Share this post Link to post Share on other sites
WeaponX 0 Report post Posted March 25, 2007 The tutorial was definitely easy to follow. For those that don't have any programming experience, you should have the Pascal compiler installed to see what these code will do.I started with learning the C language and dove into some Perl, Java and then Visual Basic. Never got a chance to learn the even older and more basic ones like Pascal. I actually started to learn ForTran lately to help my cousin in her assignments Picking up from these older languages will be easier to learn once you know another programming language Share this post Link to post Share on other sites
Quatrux 4 Report post Posted March 25, 2007 My first language was ARexx, it is a scripting language, even though I was a kid at that time and it was quite hard for me, then I tried basic, programming on Amiga computer with blitz basic, but after some time I quit. Anyway, I think that Pascal is a very good language for educational stuff, it is easy to understand and it can give you the first programming knowledge.. Pascal was my first serious programming language, we needed to learn it in school and were using custom GUI Free Pascal, in fact it is very good for doing mathematical stuff, I needed to learn it for my 12th form IT exam, but never moved to OOP, I mean Delphi. But at that time I also had a lot of knowledge about PHP and currently in the University we started to learn simple C and when knowing Pascal and PHP, C is really very easy, even though some people say that C is a language for beginners, but it is quite low level programming language and serious things can be done with it, so I disagree.. But of course, if you want to you can start from anything.Nice tutorial, will try to see the part 2 about for and while loops, because usually that is the main thing, most of beginners doesn't understand loops or how they work. Share this post Link to post Share on other sites
yordan 10 Report post Posted March 25, 2007 Nice Tutorial, Pyost, thanks a lot. I found it really simple, clear and understandable. Waiting unpatiently for part 2.Concernint the C comments, I heard that Pascal is more useful if you want to do hardware-oriented things (acting on switches or on relays, measuring external temperature or watching for intruders in a home and reacting when necessary) and C is more useful for big calculations. Share this post Link to post Share on other sites
Bảo Tuấn 0 Report post Posted March 26, 2007 I really to learn pascal but do not have any books. Now, I read your post and like it. Can you share All to tutorial about Pascal? Thanks Share this post Link to post Share on other sites
pyost 0 Report post Posted March 26, 2007 I think I'll be able to write Part Two during the forthcoming weekend, since I'm too busy over the week. Share this post Link to post Share on other sites
iGuest 3 Report post Posted May 1, 2009 hi there I'm using pascal at college and I just dnt understand it at all. But I must admit I'm not very good at the whole code thing such as html I hate it with a passion. Any way I have been asked to create a program which converts pounds into euros which I have just manage to use also it works which is a + lol. Anyway I have now been asked to put a box around it and this is where I am lost I have read though this and am very impressed and have learnt new things that I did not know before so thankyou I was wondering if you could help me in any way due to my box problem???? mez =] Share this post Link to post Share on other sites
iGuest 3 Report post Posted June 3, 2010 Pascal is a teaching languagePascal For Beginners - Part OneI know this is an old topic, but I wanted to toss in my couple of pennies: Pascal was written with the intention of being a teaching / learning aid. When I was in high-school, (late '80s), we used Pascal in our AP Computer science class. I don't remember my pascal syntax, but I use what I learned in that class every day at work. Pascal is a great starter language. (A little better than the old B.A.S.I.C.) If you can find it, QBASIC and QUICKBASIC (the compiled version) are also good learning langauges, even if they are outdated.-reply by Corry Share this post Link to post Share on other sites
iGuest 3 Report post Posted July 12, 2010 Thank you very much!Pascal For Beginners - Part OneHi admin, your work is great. Its so easy and understanding. Very nicely described and easy to learn. And feel love to learn. I have a small question from everyone.. do you guys know how to make a SELF working output ? Think I have wrote a program. But the computer I am going to run it does not have pascal. Is there anyway to run it from it self. like we do for Flash. We output the whole program as a SWF, or EXE.. like that ? I need one self working output for the program to run on any computer. hope you undertoood what I try to explain. Regards!Dilantha. Share this post Link to post Share on other sites