Home
Intro
LC
ACR
About

Day 6 - Comments and Other Neat Stuff

-=-=-=-=-=-=-=-=-=-=-

What you are going to learn today:

  • What comments are and how to use them
  • How to use units (not make them)
  • How to format Writeln text
  • How to get information from the user

-=-=-=-=-=-=-=-=-=-=-

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?

{ Utter Complete Trash } Comments are so darn simple, we wonder why people talk about them at all. Well, simply put, comments are just code the compiler never executes. This lets you type text into your code - because the compiler never executes it. Comments are used to annotate code, or just to take notes. For example:
  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:

  • Crt
  • Dos
  • Graph
  • Graph3
  • Overlay
  • Printer
  • System
  • Turbo3
  • WinDos
  • 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!
      Ideas - You've probably realised something by now, in any Pascal program, besides the program code, there are a few 'sections' before the block. The const, var, and uses sections (that's not all though, get ready for type...) declare variables, constants, or load units. You may be wondering, what order should you put them? Well, it really doesn't matter, but just remember, before you use anything, you must first declare it. Thus, if your var section needs something from the const section, the const section must of course come before var. Here's my suggestion: first should be uses, then const, and finally var.
    Now, you can go on to use the TextColor and GotoXY procedures defined in Crt - just like any other function. (Note that these procedures will not work if Crt is not loaded.) So you can type this:
      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

    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);      6784
    
    As 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.

    Digits/Precision

    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.14159
    
    Notice 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 user

    You 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 Word

    Now 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!

    -=-=-=-=-=-=-=-=-=-=-

    HomeIntroLCACRAbout
    Back to Class B

    -=-=-=-=-=-=-=-=-=-=-

    This page is ThinkQuest entry 11127.
    email: tq97-11127@advanced.org