![]() |
|
[Delphi] Pascal Programming Tutorial - 1 - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Coders Lounge (https://hackforums.net/forumdisplay.php?fid=5) +---- Thread: [Delphi] Pascal Programming Tutorial - 1 (/showthread.php?tid=4697965) |
Pascal Programming Tutorial - 1 - Deadbyte - Feb 21, 2015 Pascal Programming Tutorial - 1 Intro I presume this is the right section for this tutorial. Since Delphi is Object Pascal. Pascal is a programming language. It was created in 1970 by Niklaus Wirth, to help people learn how to make computer programms. Now, there are many different dialects of the language, so a Pascal program written for one compiler can not easily be used with another one. This tutorial has multiple sources myself and other official Pascal websites. [img]http://www-history.mcs.st-and.ac.uk/BigPictures/Pascal_2.jpeg[/img] Blaise Pascal (19 June 1623 – 19 August 1662) Turbo Pascal is a software development system that includes a compiler and an integrated development environment (IDE) for the Pascal programming language.From version 6 both a lower-priced Turbo Pascal and more expensive Borland Pascal were produced; Borland Pascal had more libraries and standard library source code. The name Borland Pascal is also used more generically for Borland's dialect of Pascal. Preview: [img]http://upload.wikimedia.org/wikipedia/commons/f/f5/LagrangePAS.jpg[/img] Programming Basics Lesson 1 In a program, you must always obey the rules of the language, in our case, the Pascal language. A natural language has its own grammar rules, spelling and sentence construction. The Pascal programming language is a high level language that has its own syntax rules and grammar rules. As you go along with the lessons, you must note what you can do and what you cannot do in writing a Pascal program. A very simple program is shown below: PHP Code PHP Code This first program is, what is commonly referred to in programming, as 'indented'. Indentation is a must in writing programs as it aids in the way the code is written ie. neater. Indentation also helps with debugging and code presentation. You will note how I indent programs. String Variables Lesson 2 Soon, you should learn how to input text by the user using 'string variables'. The following program is written showing an example of a string variable, prompting the user to input his name, whatsoever: PHP Code Constants and the Assignment Statement Lesson 3 Apart from variables, there are also items in the program which are referred to as 'constants'. Unlike variables, constants keep their value or string unchanged for the whole program. Here I have made a program, not so much different from the previous one: PHP Code Const age = 15; The constant 'age' is a value that could be used whever it is required in a program. Example: age2 := 15; age2 := age + 15; The above example shows an addition of the value of the variable 'age' with the value 15. The value of the constant 'age' remains 15, but the value of the variable 'age2' becomes 30. The assignment statement is not only used for additions, but is also used to assign a variable: text if it is a string variable or a numeric value if it is an integer variable. Special Reserved Words of the CRT Unit: ClrScr(), GotoXy(), etc... Lesson 4 Before I teach you the if statements and for loops, I would like to give you an idea of some pascal functions which are quite useful. This would require your program to include the library 'crt.tpu'. To include a library in the program, one should use the reserved word 'uses', because it is used to call a library of functions and procedures. Here is the program of lesson 2 (program 3) which is better handled and more user-friendly: PHP Code Program Control Lesson 5 Now, it is time to learn the most important rules of programming: the if statements - decision making, for loops and the repeat-until loop. Almost, these 3 general programming constructs are common in every programming language and you have to make sure that when you have finished reading this lesson, make sure that you have practiced them enough before continuing with learning pascal because they are of outmost importance. If you fall in doubt about these programming constructs, then try to use the forums, describing your problem. The 'if statement' executes a the proceeding statement(s) conditionally. This means that if an action comes to be true, then the statement(s) proceeding the if statement are executed, else these statements are skipped. It works like this: If this happens(action), then do this(reaction, if action is true). OR: If this happens(action), then do this(reaction, if action is true), else do this(reaction, if action is false). In Pascal, the 'if statement' should be written as follows: If conditional expression then code ... ;{if one action} OR: If conditional expression then Begin instructions ... End; {if more than one action is required} Note that you should not use an assignment statement in the 'if' construct, otherwise the compiler will raise a syntax error. I.e.: Wrong: If x := 20 then x := x + 1; {the underlined character must be excluded} Correct: If x = 20 then x := x + 1; {only an equal sign is used for comparison} A program is shown below as an example of how the 'if statement' works: PHP Code If..Then..Else In a normal if statement, the 'reaction' cannot be performed if the condition is not true. But in an if..then..else statement, there is at least one set of statements to be performed. Let's take a look at the example below: writeln('Who has discovered the land of America?'); Readln(ans); If (ans = 'Christopher Colombus') then score := score + 1 {if this is false,} ELSE writeln('sorry, you''ve got it wrong!'); {then this is true} Note that if the 'else' term is included with an if statement, then there should be no semi-colon before the 'else' term; just as seen in the above example. Pascal Programming Tutorial - 2 is coming soon. * - aldypalypso - Feb 23, 2015 Your tutorial is nice, but it should've been bigger in my opinion. Too small. * - Deadbyte - Feb 23, 2015 (Feb 23, 2015 09:46 AM)aldypalypso Wrote: Your tutorial is nice, but it should've been bigger in my opinion. Too small. I'll make part 2 this week I think. * - borisganji1 - Feb 23, 2015 You should really write the pros and cons about Pascal. I know C++ and I don't see any upside learning this, C++ can probably do the same, while have a better documentation - but then again I know nothing about pascal :) Regards * - Deadbyte - Feb 23, 2015 (Feb 23, 2015 09:55 AM)borisganji1 Wrote: You should really write the pros and cons about Pascal. I made the thread for educational purpose only. Pascal is useless now in my opinion, but a good first language for a beginner who has difficulties learning programming. * - Recursive™ - Feb 23, 2015 I find Pascal a language perfectly appropriate for coding learners, allowing users to properly learn programming paradigms and fully experiment with a small extent of the capabilities of third generation languages. This language has really helped me properly understand C++ and Java. The guide could indeed be added to, although on the whole the guide is informative, easily read and laboriously thought through. Good job. I would however recommend not using the free pascal GUI because let's face it, that GUI is disgusting and horrible to use. Possibly encourage using Lazarus as much more user friendly. * - Deadbyte - Feb 23, 2015 (Feb 23, 2015 02:34 PM)Forumnator Wrote: I find Pascal a language perfectly appropriate for coding learners, allowing users to properly learn programming paradigms and fully experiment with a small extent of the capabilities of third generation languages. This language has really helped me properly understand C++ and Java. The guide could indeed be added to, although on the whole the guide is informative, easily read and laboriously thought through. Good job. I would however recommend not using the free pascal GUI because let's face it, that GUI is disgusting and horrible to use. Possibly encourage using Lazarus as much more user friendly. I agree with everything you said. 🤝 * - Cyber_Hox - Feb 23, 2015 Good tutorial (moj prijatelju kada ce sledeci? :D) * - Deadbyte - Feb 23, 2015 (Feb 23, 2015 04:49 PM)Cyber_Hox Wrote: Good tutorial (moj prijatelju kada ce sledeci? :D) This week I hope I will be free to do it 🤝 |