![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
Home | |||||||||
Pascal Home | |||||||||
Pascal Tutorial Part I - The Basics | |||||||||
Note to everyone: Instead of typing all these programs in, remember that you can copy them to clipboard, paste them into notepad, and save in double quotes "fname.PAS" where fname is the file name. Remember that any questions should go to qbspot@zwallet.com Pascal tutorial I: First of all, in every Pascal program, you should start with a "uses crt;" Crt is, as advanced programmers might geuss, an external library - that is, file, that can be brought in. There are other such files, but let's start out with that one. So here's your first program: uses crt; Blahhh.... Does nothing... Now you need a begin. Now here's where it gets fun. Let's clear the screen and have a line written to the screen! Here's your program: uses crt; begin clrscr; {Clear Screen} writeln('Hello, World!'); {Display text} end. Now we're going somewhere... In order to clrscr;, you need to include crt. Then You need to "begin," as in every program. Notice the indentations and the comments. When making a program, these are very important. But notice the ";" at the end of each line (other than begin and end.) This is very important - one of the most common and fatal mistakes in Pascal is not putting a ";" at the end of most lines. Don't make that mistake! As for the "end.", this is very important. That must always be the very last line of the file. Make sure you never type anything after that. If you know QBASIC, be sure to be here in a couple days for my BAS-2-PAS conversion chart, but first you need to understand program structure in Pascal. This won't work: read(s); writeln('You typed, ', s); You need to: uses crt; var s : string; begin read(s); writeln('You typed, ', s); end. So first finish this Tutorial! Now to run the programs, just go to the menu and Run. Problem is, you'll have trouble seeing it. So go back to the run menu and choose User Screen. This is our temporary solution. Now let's make a basic calculator: uses crt; var x, y : integer; begin writeln('Enter num1: '); read(x); writeln('Enter num2: '); read(y); writeln('Num1 + Num2 = ', x+y); read(x); end. there's your calculator! First the user is prompted to enter a number, the number is read off the keyboard, the number is saved as x, the process is repeated and the number is saved this time as y. Then the Sum is displayed. Note the last line of the program - read(x); - now why do that again? What's the use? You'll notice, if you run this example, that you cannot leave the screen until you press Enter. - The read(x); waits until Enter is pressed and the value is read into x, but unfortunately, the program is over! This is our other solution - no more Run menu.. User Screen! Now just include that in your program and you'll have to press enter for everything to dissappear! But here's the easier way: a char variable: uses crt; var c : char; : : : c:=readkey; end. This makes it so you can press any key to exit. Any key! Much better! But you have to declare c as a char. And readkey is contained in crt, so you have to "uses crt;" in the beginning. Next you're going to learn about procedures and functions. Tell me which makes more sense and is easier to type and less file size: uses crt; var c:char; procedure wait; begin writeln('Strike any key to continue...'); c:=readkey; end; begin writeln('Pausing...'); wait; writeln('One more...'); wait; end. Or: uses crt; var c:char; begin writeln('Pausing...'); writeln('Strike any key to continue...'); c:=readkey; writeln('One more...'); writeln('Strike any key to continue...'); c:=readkey; end. That's what a procedure is! A list of instructions to be executed more than once! And now you know how to do that in Pascal! Now you need to know what a function is. It's like a procedure, but it also acts like a variable - you specify what variable type that is. Here's an example: uses crt; var c:char; x:boolean; function yesno : boolean; begin writeln('(Y/N) ?'); repeat c:=upcase(readkey); until c:='Y' or c:='N' writeln(c); if c='Y' then yesno:=true else yesno:=false; end; begin writeln('Are you sure you want to test this function '); x:=yesno; if x then writeln('Yes Yes Yes!'); if x=false then writeln('Awww, mannn, no no no...'); end. See? You could just do it this way, too: if yesno then writeln('Yes!'); if yesno=false then writeln('No.'); A function is as simple as that! You just have to get it's value through itself or another variable and it is automatically executed. For those of you n00bs there scratching your heads at "Boolean," simply put, it means either 1 or 0. True or False. So to check if it's true, we don't have to see if x=true. Just if x. Cool, huh? Doing it that way will make you seem a little less n00bish. OK? So do it that way! Note that x had to be boolean, too. Yep. You can't set a string to a boolean function. Hahahahaha, of course you can't! OK, so now you understand functions and procedures. OK, maybe you don't... But you know a little more... Now we have to explain the function a little more, though. Readkey is in CRT, so "use" crt! But what if the user pressed a lower case 'n' or 'y'? We didn't check for that; only for 'Y' or 'N'! So why not? Because it doesn't matter what they pressed, the upcase char function converts chars to uppercase, so it's alright! No matter what, if you pressed 'n' or 'y', c is always uppercase, so it'll be 'N' or 'Y'! And what's the repeat? It's a loop. For QB programmers, it's the same as DO...LOOP. For anyone else, we'll explain later. For now, what it does is keeps prompting "(Y/N) ?" until a "y" or "n" or "Y" or "N" is pressed! Then why does it display c with writeln(c);? Because when you get a keypress from readkey, it doesn't actually display the key! So we have to do it manually. You've learned a lot now. You know what 2+2 is... No, seriously, you can begin making a program modeled after these ones. Type them in, check for mistakes, and run! Now the next Tutorial, Part II, is all about codes. I have it written, I just need to type it in, which takes some time. For you pros, it's all about sound, ASCII, Color, Special Text sizes, and how to choose where you display the text - where the cursor goes. Click here to go to the unfinished Tutorial Part II. Review and Test ---Write a program that tests a persons knowledge of basic Pascal - how to do this, how to do that, multiple choice questions incorporating a similar concept as yesno, only an integer function so you can have multiple choice questions. ---That's enough for now. Send you program(s) to qbspot@zwallet.com. If I like your program, I'll put it up for download here. Remember, send only the .PAS file. |
|||||||||
Home | |||||||||
Pascal Home |