|
|
What you are going to learn today:
Relax. This is the most mixed tutorial in Class B - that means that you don't need to concentrate on anything! I mean, what could be more fun than a mess of topics to learn about? Comments? What comments? a := w * h; { Width times Height }
The words "Width times Height" don't even get a glance from the compiler! To mark
out a comment, you use the two curly brackets - { and }. Comments can be almost
anywhere, but for goodness sakes, don't put them inside the middle of a statement.
Comments can also be used to temporarily mark out statements.
{ This code is worthless.
a := a;
See what I mean? }
The statement a := a is never executed at all. Aha! Now you see the power involved.
Imagine, with just two characters, you can render an entire sequence of code
useless. Is that power or what?
How do you use units?Pascal has a few built in commands - Writeln, and so on. But to do more complex things, like changing the colour of the text or repositioning the cursor, you need different commands which aren't built-in. This is exactly what units are for. Units are files which actually define new procedures and functions for you to use! Amazing! Pascal generally comes with a few standard units which are listed blow:
Of all those units above, the one you'll probably use most would be Crt. It provides procedures to change the text and background colour and to reposition the cursor - Yes, sounds very well and good, but how do you include a unit in your program? Well, you've learnt a few keywords: const and var, so here's another one: uses! To include a unit in your program, type this before your code block, just like how you would declare variables or constants: uses
Crt;
This way, the Crt unit is loaded and all its procedures and functions will be
available to you! If you want to load many units, separate them with commas.
Just make sure there's no typo, and off you go!
TextColor(Blue);
GotoXY(10, 10);
Writeln('SKCUSTISOCBVETAHI');
Those lines would print out "SKCUSTISOCBVETAHI" in blue at screen location 10, 10.
(Crack the code! Find out what SKCUSTISOCBVETAHI means and win a $10 vouncher!)
Isn't that great! Complete control! For a complete list of Crt Procedures and
Functions, type Crt and press Ctrl-F1. Select "Crt-Procedures" to view a list of
all the procedures and functions in Crt. Nice work.
How do I format Writeln?For many of you who decided to jump the gun and play with non-integer data types (numbers which can have decimal points), you may have noticed that your output was far from what you expected. For example: var
RealNum: real;
begin
RealNum := 7.3;
Writeln(RealNum);
end.
Upon running this program, you find out that your expected 7.3 never appeared the
way you wanted it. It became "7.3000000000E+00" - pure trash except for true
mathematicians like us. (Ahem) Well, what's the problem? Ahh, it's all very simple.
When you print out a real number, its precision is well beyond the
limits of normal human understanding. What it actually tried to print out was
"7.30000000000000000..." until we all fell flat staring at zeros. To solve this
problem, you must understand the concepts of width and digits...
Width is the mininum width the number must take up, and this value is set using a colon next to the value name. Assuming x is 6784, the following statements give the following results: Writeln(x:3); 6784 Writeln(x:4); 6784 Writeln(x:5); 6784 Writeln(x:6); 6784As you can see, whatever value x is, you know that the number will always take up that number of spaces. However, if the width is lower than the width of the number itself, the width is expanded automatically, as shown in the very first example: Because 6784 has 4 digits but we gave a width of 3, the 6784 will still be printed out in full. However, if the width is larger than the length of the number, the number will be aligned to the right, and spaces will be added to the left. However, that's not enough. The result after using Width still gives a number with an 'e' in the middle. Ahhh, what now? Precision! That's it! We tell Writeln how many digits we want after the decimal point! And how do we do this? By adding another colon after the Width, followed by the precision: Writeln(x:5:3); 3.142 Writeln(x:8:3); 3.142 Writeln(x:8:5); 3.14159Notice that the number is not truncated, but rounded off. Ahh, intelligence of Pascal, I believe? (Now you know why I love this language!) With this information, Numbers can be printed out in the exact way you want them to be! How to get info from the userYou might have noticed that we've been making programs which are kinda one-sided. The user has no active participation in the whole thing. Thus, the user feels left out. So, in order to keep your users smiling, use the Readln command, as illustrated below: var
x: integer;
begin
Writeln('Enter Value...');
Readln(x);
end;
Simply put, this program just stores what you typed into x - a very interesting
move. Try it out. It works, right? Well, that's not all. Readln can read more
than one value at a time:
Readln(x, y, z);Used this way, you can either separate each value with an enter or a space. Oh, how wonderful this is! (More of this I'll go singing like The Sound of Music) You've finally finished Class B! Our Final WordNow that was hectic, wasn't it? Don't worry, you've only got two more classes to go. After all, this was the longest class. So go, get your newest video game or something, play it for 36 hours non-stop (I personally play SimCity 2000, but naa, you wouldn't), take a 48-hour sleep, and come back for Class I, ready and going!email: tq97-11127@advanced.org |