Experimenting with Lazarus

You must have Lazarus installed on your PC to carry out this exercise.

Also, you should have written the "Hello World" program, using Lazarus.

The following steps describe experiments you can do to learn more about Pascal and Lazarus.

  1. If it is not already running, start Lazarus by double-clicking on its Launch Icon You should be able to find a launch icon like this on your desktop, or by using the Start button.
  2. If your "Hello World" program is not already loaded in the editor, open it by using the menu option File > Open ... and navigate to the folder you saved it in.
  3. Use the mouse to select HelloWorld.lpi (note that the extension is .lpi) and click on the button Open.
  4. Click OK to confirm that you want to open the project.

    You should see the HelloWorld program in the editor.

  5. Press the Run icon Active Green Run icon near the top left of the Lazarus menu bar, OR press the F9 key to run the program.

    After a small delay, you should see a mainly black window appear and at the top, it should say...


    Hello World!

  6. Take a look at the Run icon, and you will see that it is no longer green:
    The triangle is now grey Run icon, now grey. This signifies that the program is running.
  7. Press the little red square Row of icons, including red square (with tooltip Stop) to stop the program running.
    This is very useful if a program gets stuck in a loop for some unknown reason.
    The familiar Execution stopped dialogue appears. Press the OK button to continue.
  8. There are several ways to stop a running program:
    • Press the little red square Row of icons, including red square (with tooltip Stop), as we have just seen.

    • Click the Lazarus toolbar to ensure it has focus, then press Ctrl+F2.

    • On the console window, click the X at the top right.

    • On the console window, right click the icon at the top left, and select menu item Close.

    Click the green triangle to run the program again and experiment with the various ways of stopping the program.
  9. The purpose of the readln; line at the end of the program is to prevent the black window from disappearing as soon as the rest of the program has finished. Try commenting out the readln; statement by typing // at the start of the line, so the line becomes //readln;. Then click on the green Run icon.
    Anything on a line after // is treated as a comment to help programmers and is not converted to machine code.
    The black window will disappear before you can see what has been output to it.
  10. In the editor, remove the //.
    Notice that the tab at the top left of the editor now says *HelloWorld. The asterisk signifies that the program code has changed since it was last saved. The Save icon on the toolbar also changes from grey to its active state, again indicating that there are unsaved changes.
  11. Click on the Save icon to save the changes you have just made.
    Note that the asterisk has gone, and the save icon is now in its inactive (grey) state.
    Tip: Computers can crash at any time, so always remember to save your work frequently!
  12. Run the program again.
    With the readln; back in action, the console window will not vanish, as before, but will wait for your input. Press the Enter key to allow the program to end.
  13. In the editor, change writeln to WRITELN and run the program again.
    Pascal is not case sensitive, so the program still runs as it did before.
  14. In the editor, change WRITELN to write and run the program again.
    Notice that the cursor is now positioned at the end of Hello World!. The writeln instruction performed this write instruction then positioned the cursor at the start of the following line.
  15. Amend the program to:
    1. Output “Hello”
    2. Position the cursor at the start of a new line
  16. Save the program as Hello. (Accept the default file extension .lpi).
  17. Run the program to test it.
Features introduced:
  • Various ways of stopping a program

  • Use of readln to prevent application window from disappearing

  • Use of comment characters // to comment out the rest of a line

  • Presence of asterisk to signify that unsaved changes have been made to the code.

Programming - a skill for life!

How to install and set up Lazarus to create, edit and run Pascal programs