The semicolon is used after each statement in the program, except those that you will learn later. Another important thing which must be noticed is the semi-colon ( ). In line 9, the CPU will not display 'Sum' on the screen, but the stored number in the memory. However, if a variable is used instead of a message, without using the inverted commas, the CPU will display the stored variable in the memory, on the screen. When using these two terms, any message that will be typed in between the brackets and the inverted commas ' (' ')', is displayed on the screen. If you run this program, you will notice the difference between them. Both has the same function, except that the ' write' function, does not proceed to the following line when writing a statement. In the program above, both of the two types of 'write' are used. ![]() Without this word, the compiler will display a diagnostic (error message). As you can see, it is much better to shorten the variables than writing long words, such as 'variable_number1' but we still need give them a meaningful name to remind us of its storage purpose.Īfter declaring all the variables which are required to be used later in the program, the main program always starts with the reserved word ' Begin'. Moreover, we could have used 'number1', 'number2' and 'totalsum' (note that there must be no spaces within the variables), instead of 'Num1', 'Num2' and 'Sum', respectively. The variables 'Num1', 'Num2' and 'Sum' are identifiers which are not reserved words, but can be used as our variables in the program to store data in them. There are other types which are wider in range, but for now the integer type is enough to hold up our values. So values which are not within the specified range cannot be stored by an integer type. The Pascal Integer data type ranges from -32768 to 32767. a number which is not a decimal number but can be either a positive or a negative number. The data type 'Integer' means any whole number, i.e. As you can see in the example above, these variables are declared as Integers. The variable names 'Num1', 'Num2' and 'Sum' in the program are data placeholders which will store whole numbers, not real/floating-point numbers (in fact, during the execution of the program, a runtime error may occur if a decimal number is input). The ' Var' keyword is used to introduce variables in a program to be used later on as temporary data storage elements. A full-stop is never used within the program, except when dealing with records (later topics) and at the end of the program as seen in the example above. ![]() A program in Pascal starts with the reserved word ' Program' (although it is not explicitly required) and ends with ' End', following a full stop (this is required though). In the following program, the computer must prompt the user to enter a number, then the latter is added to the second number input by the user. (Remember: you can copy and paste the program in a text file, save the text file as filename.pas and open it with your compiler (such as Free Pascal). Below is a simple example of a small program. There are various restrictions on how to write this statement. You will note how I indent programs.Ī program in Pascal always starts by the reserved word 'Program' following the title of the program. Indentation also helps with debugging and code presentation. Indentation is a must in writing programs as it makes it easier to read ie. This first program is, what is commonly referred to in programming, as 'indented'. ![]() The only difference is: neatness and friendliness. This program also runs perfectly as the previous one. Now, look at this: Program Lesson1_Program2 begin Try running this program with and without the 'readln' procedure and notice the difference. If 'readln' is removed from that line, then the message is displayed on the screen without giving any chance for the user to read it and exits! The 'readln' procedure, here is used to pause the program and waits until the user hits the return key. So, to display any message on the screen, you should use 'write' (or 'writeln'). Prepare to learn PASCAL!!' - an introductory message that is displayed to you whenever you are going to learn a new programming language. The program is written only to display the message : 'Hello World. A very simple program is shown below: Program Lesson1_Program1 As you go along with the lessons, you must note what you can do and what you cannot do in writing a Pascal program. The Pascal programming language is a high level language that has its own syntax rules and grammar rules. A natural language has its own grammar rules, spelling and sentence construction. In a program, you must always obey the rules of the language, in our case, the Pascal language. Read the lessons FREE & OFFLINE from the convenience of your phone.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |