How to Write Turbo Pascal Programs

104 15
    • 1). Open Turbo Pascal. In Windows, this can be done by clicking "Start" and "Run," typing "cmd" to open the DOS command prompt, then typing "tpascal."

    • 2). Type the program definition:

      program helloWorld;

      All programs begin with this line that lets Turbo Pascal know where the program starts.

    • 3). Define your variables:

      var

      name: string;

      Variables are shortcuts to memory locations, and they hold any information that can be altered over the course of your program. To create one, you open the variable section with the word var and then provide the name of the variable and its data type. In this case, the data type is a "string," or a series of letters, numbers and punctuation marks. The name makes it obvious to programmers what the information in the variable will be, but you can choose any name you want.

      Some modern languages skip this step, but it is essential in Turbo Pascal. You can define as many variables as you need under the "var" heading.

    • 4). Define the beginning and end of the program:

      begin

      end.

      All the code for the program will go between these two lines. The "." after end is essential.

    • 5). Paste the following between the begin and end lines:

      Write("What is your name? ");

      ReadLn(name);

      WriteLn("Hello, ", name);

      Going line by line, it firsts asks for a name, reads the user's name from the keyboard and places that information in the "name" variable, then prints a short message back to the user using the user's name. Notice the difference between the first and third lines of this short program. "Write" prints to the screen but leaves the cursor located on the same line. This is the equivalent of writing a sentence in a word processor and not hitting the enter key at the end. "WriteLn" does the same, except it moves to a new line after it is finished printing.

    • 6). Click "Run" from the menu to run your program. It may take a few seconds for your program to start, since the computer must translate your Pascal code into machine code that the computer understands. This process is called compilation. Alternatively, you can click "Compile" from the menu to build an executable for your program but not run it.

Source...
Subscribe to our newsletter
Sign up here to get the latest news, updates and special offers delivered directly to your inbox.
You can unsubscribe at any time

Leave A Reply

Your email address will not be published.