WRITING YOUR FIRST PROGRAM
(5 points)
To get started, you need to complete the PASCAL tutorial, which is a introduction to the skills you need to know in order to program in the PASCAL language on the Lazarus compiler.
As you work through the tutorial, you will be creating your first piece of software called MyFirstProgram. Please be looking for the purple text. These instructions will tell you how to modify MyFirstProgram as you learn new skills.
SCROLL DOWN TO BEGIN!
As you work through the tutorial, you will be creating your first piece of software called MyFirstProgram. Please be looking for the purple text. These instructions will tell you how to modify MyFirstProgram as you learn new skills.
SCROLL DOWN TO BEGIN!
Create a New Program File
Welcome to your first (or not your first) experience with creating software, or computer programming! Writing software code is done in an application called a compiler.
The compiler we will use is loaded on your PC. In the Windows menu under All Programs, find the folder for Lazarus. Open the folder and select "Lazarus" (fifth one down with the icon shown to your right). It will prompt you with a window that has a button to "Start IDE"; press that button. You should now see several windows pop up. Read on... There are hundreds of computer languages to choose from for programmers. Some more famous examples you may have heard of include BASIC, C++, JAVA, and PYTHON. We will be using a language called PASCAL, named for a French mathematician. This language is older but is easy to use because the commands align very well with the English language. Follow these instructions to create a PASCAL program file in Lazarus.
Note: While it would be wonderful to work "in the cloud", we can't quite get that support yet. I will be showing you how to download Lazarus at home. You can move your files back and forth on a flash drive, or by dragging the entire file folder you created into Google Drive, then dragging the entire file folder onto your home desktop. You would reverse this to bring your work back to school. You should now be staring at a window that says: Program MyFirstProgram; begin end. If so, you're ready to go! Move on down. Run, Debug, and Save Your Program To run a program means to command the complier to translate the code into computer machine language so that the CPU creates your software. To debug a program means to allow the compiler to look for code is incorrect and prompt you to fix it. Place your cursor at the beginning of line 4 (to the left of end.) Hit enter four times so that end. is on line 8. On line 7, type this: readln; Please note that the fifth letter of that command is an l (as in Lazarus), not an i. This is a necessary function needed to 'pause' your software when running it. YOU MUST DO THIS IN EVERY PROGRAM YOU WRITE. Note: All of the commands you have in your program must be placed between begin and end. This section is what will be analyzed by the compiler. On line 5, type this: HELLO WORLD! My name is {put your name here}! Now, run and debug your program by pressing the F9 key. Uh-oh. There's an error message down in the Messages window at the bottom of your screen, highlighted in red. If you don't see it, locate it (it looks like the image shown at right). The second line reads "Error: Identifier not found "HELLO". You have created a bug in your program, or code that the compiler does not understand. PASCAL is not English, and HELLO is not part of the language. What we can do is modify how we present our message to the compiler to alleviate this problem. To the left of HELLO, type this: writeln(' To the right of your name and the !, type this: '); It should look like this: writeln('HELLO WORLD! My name is _________!'); Hit F9 again. No more bugs, right? If you have one, it may be because you typed writein instead of writeln (W-R-I-T-E-L-N). It may also be because you didn't end the line with a semi-colon (;). Fix it up until you don't have a bug. See Mr. Kindt if you are stuck. Note: All commands, like writeln's, end in a semi-colon (;). VOILA! You have made software :) A new window has popped up with a black backdrop and gray text. This is called an executable file (.exe). In the Windows operating system, software applications are in the .exe file format. Hit enter or press the red X to close your .exe file. If you get a message that says "Execution Stopped", select the option "Don't show this message again". When it comes to saving your work, DON'T. Well, OK, of course you want to save it. Here's the thing: PRESSING F9 ALWAYS SAVES YOUR WORK. This is the only way that you should save during programming. NEVER GO TO THE FILE MENU AND SAVE. The only time you ever use an actual save command is when you "Save Project As" when first opening your new project file. Outputs The writeln command, pronounced write-line, allows the programmer to output something to the user. It was given this name because it will WRITE what is inside onto the screen, then move the cursor down to the next LINE. Notice the use of single quotes/apostrophes bordering your HELLO WORLD message. Text inside of those single quotes is called WYSIWYG (pronounced Wizzy-wig), which stands for "What You See Is What You Get". This means that the user will see exactly what you type. Examples: CODE -----> OUTPUT writeln('Please enter a few numbers.'); ------> Please enter a few numbers. writeln('Howdy pardner!'); -----> Howdy pardner! writeln; -----> No output--just skips the cursor down a line. Follow up your HELLO WORLD! message with the following with each bulleted item on its own line.
Make sure that your program still finishes with readln; and end. (probably on lines 11 and 12 or somewhere near there). Hit F9 and watch your outputs come alive! Variables, Memory, and Input A key element of any software program is input, which is the ability for the user to enter some information into the program for processing by the computer. In these older-style programs, all input is done using the keyboard and not the mouse. In order to do this, you must first create variables to store the user's inputs. In mathematics, a variable is thought of as an unknown value. In computer programming, a variable is a unit of memory that the computer can use to store information. Think of a variable as a box used to store a number, letter, or word as long as you need it to do so. The compiler does ask that you are up front about what type of information you will be storing in the variable. There are three types of variables in PASCAL you will need for your programs.
The programmer creates variables before the program begins in a special subsection of the code by generating a name and a type.
Here is what a variable section (in orange) might look like. This example contains five variables of three different types. 1) Program MyFirstProgram; 2) 3) VAR 4) input1, input2: longint; 5) quotient: real; 6) first_name, last_name: string; 7) 8) BEGIN Let's set up some variables in your program. Make some space below Program MyFirstProgram; so that line 3 is empty. Then:
Hit F9 to run the program and make sure there are no bugs. Your program should still only show your outputs. The next step is to get some input from the user. The input function in PASCAL is readln (read-line). This will provide the user with a blinking cursor and READ what the user inputs until the user hits enter to skip a LINE. When using readln as a command, it is followed by a set of parentheses with a single variable inside and ended with a semi-colon. EXAMPLES: readln(number1); readln(user_input); The variable inside the readln command is the memory space where the user input will be stored. Below the writeln in which you ask the user for his/her name, present the user with a blinking cursor and store their input into the variable user_name. Hit F9 and run the program to see if it works. If so, hooray for you! Move on down. Expanding Input/Output Skills You can output variables inside of a writeln statement. When you do so, you do not use quotes around the variable name. If you do, then WYSIWYG (you will see the variable name--not what is stored in the variable!) Below your readln(user_name); do the following:
Hit F9 and see the difference! The first output line is useless to us. Once you feel you understand this, delete both of those lines you just created. You can mix outputs in a writeln between variables and WYSIWYG text as long as you separate them with a comma. Below your readln(user_name); where you just deleted the two writelns, do the following:
Hit F9. Pretty nice, yes? Now get rolling and do the following on the lines beneath the writeln you just created.
Hit F9 and ensure everything is working. Don't be afraid to ask your local pro or Mr. Kindt for help. You are learning a new language, and that is not easy! Assignment Statements & Calculation Don't we want the computer to do some of the work? Isn't that the point? Let's get on that. Everything you have done so far has been visible to the user.
Assignment statements are different than writeln and readln because:
Here is an example of an assignment statement: copy_of_word := word; In this statement:
More commonly, assignment statements are used for calculation. Such calculation always occurs on the right side and uses the following mathematical operators:
number_squared := sqr(number); doubled_value := double * 2; difference := value1 - value2; Note: The commonly used ^ for exponents is NOT supported. You must multiply repeatedly instead. For example, to cube a variable number do this: number*number*number; Do the following:
Note: To clean up that messy looking square root (which is in scientific notation), do the following.
If-Then Statements Because the programmer cannot sit and wait by the computer to determine how a program will function based on the user's inputs, you need to inform the compiler of particular decisions that may need to be made in the program. This is where the IF-THEN statement will come in handy. The format of the IF-THEN statement is written as follows: IF (Boolean argument) THEN BEGIN executable statement(s); END; The IF-THEN statement is an executable statement of its own and therefore only uses ONE semicolon. Notice how the lines with IF (Boolean argument), THEN, and BEGIN do not have semicolons to end them. This is because they are part of the IF-THEN and the IF-THEN is not over yet. Between the BEGIN and END;, however, you may have several executable statements with their own semicolons. The BEGIN and END; is called a block; it acts like a "semicolon shield" that lets the IF-THEN statement continue on without letting the semicolons at the end of writeln/readln/assignment statements get in the way. What is a Boolean argument? A key element of formal logic, a Boolean argument is any comparison of two values using a comparative operator. There are six comparative operators in PASCAL:
A Boolean argument could be something like: (number1 < 10), or like (value1 >= value2). When the Boolean argument in an IF-THEN statement is TRUE, then what rests in the BEGIN-END; block will be executed by the CPU. If that argument is FALSE, then that code will not be executed. Here's an example. IF (value4 > 25) THEN BEGIN writeln('You have more than 22 people. How many rooms would you like to reserve?'); readln(number_of_rooms); END; Note: When you use a Boolean test with a string, you need to put the letter/word you are testing against in quotes such as (word1 = 'Muskrat') or (input = 'X'). In addition, there are Boolean connectives that allow you to look at several Boolean arguments in a single IF-THEN statement. These connectives are AND and OR. The format of an IF-THEN with these connectives looks like this. IF (Boolean argument) AND/OR (Boolean argument) AND/OR ... THEN BEGIN executable statement(s); END; When AND is used as a connective, then ALL Boolean arguments must be true to execute the block. When OR is used as a connective, then only ONE Boolean argument must be true to execute the block. Let us deal with the "Wanna do some math" question you had before. Begin an if-then statement following the readln for Y_N. If the user input a Y or y, then allow all of the inputs and calculations to occur. If you do this correctly, then entering anything besides a Y or y should take the user directly to the goodbye message. One additional note: When using Boolean connectives, the AND will always be executed before the OR in the order of operations. You may need to put groups of connected arguments in parentheses to ensure that they occur first, like is shown below in the red parentheses. IF ((Boolean argument) OR (Boolean argument)) AND ((Boolean argument) OR (Boolean argument)) THEN BEGIN executable statement(s); END; Submitting Your Work Hit F9 to re-save your work one last time. At this point, your "app" (.exe file) is saved. However, to make it easy for me to inspect your code, you need to complete one more task. In the File Menu, select "Export HTML". You should be directed to your student folder into the program folder in which you are currently working (MyFirstProgram). Save this file in the folder. You will submit your work through dropboxes embedded in this website, like the one shown below. Each will contain four input fields: Your First/Last Name, Program Selection Drop Down Menu, Exported HTML, and .exe file. The icons for .exe and your .html files are shown below. For .html, it merely depends which browser is set as your default--so you will see it as an IE, Firefox, or Chrome icon. Go ahead and submit MyFirstProgram through the dropbox below! |
Reflect
Using the link below, finalize the MyFirstProgram assignment by reflecting on your first two-three days of Computer Programming I. This portion is worth 2 of the 7 points.
Reflection
Using the link below, finalize the MyFirstProgram assignment by reflecting on your first two-three days of Computer Programming I. This portion is worth 2 of the 7 points.
Reflection