On the Internet, you can find a lot of programs in the Pascal language, but itβs much harder to figure out how they work and how they work. Let's learn the basics of programming together!
Algorithmic language: basic concepts
In colloquial speech, we use the basic units: symbols, words, phrases and whole sentences. Algorithmic language also has a similar structure, only its components are called differently. We are talking about elementary constructions, expressions and operators. All these units form a hierarchical structure, since each subsequent element is formed from the previous one.
Symbols of an algorithmic language are indivisible atoms used to write code.
Elementary constructions are minimal units that have their own meaning.
Classical language expressions are formed from the two above units and set the rules for finding the necessary value.
The operator is responsible for describing a specific transformation, which is mandatory for the correct execution of the program. There may be several, if necessary - the program must perform a complex operation. In such situations, they are combined into a block or a compound operator.
Pascal language
There are a large number of algorithmic languages. Pascal (for beginners there are separate benefits) is one of them. Its alphabet consists of numbers, letters and special characters. Here is a list of them:
- 26 latin uppercase and lowercase letters ;
- underscore
- ten digits;
- limiters;
- signs of operations;
- qualifiers;
- system reserved (service) words.
In addition to the elements listed above, a βspaceβ belongs to the core set, which cannot be used inside the construction of reserved expressions and double characters.
Elementary language constructs
Pascal for beginners includes strings, numbers, and names.
The numbers used in the code of the programming language in question are usually written in the decimal system. They can be either real or integer, which is customary to mention without a decimal point. If the number is positive, then its sign can be omitted.
Pascal is an algorithmic programming language in which strings are a sequence of characters enclosed in apostrophes. If you need to use the apostrophe itself, then this symbol is worth mentioning twice.
A name is a sequence that begins with a letter and can contain numbers. Identifiers are called labels, types, constants, functions, procedures, variables, objects, and even modules. When forming identifiers, you can use the underscore character. A name can have a lot of characters, but the compiler will only read the first 63 characters. Pascal, the description of which may seem so complicated, is not so scary, so do not rush to get scared and close the browser page!
As language identifiers it is forbidden to use standard names of constants, procedures, types, files, functions, as well as utility expressions.
Gaps can help improve the visibility of the code, but remember that they cannot be used to separate names and numbers in the middle.
Algorithmic Language Syntax
Each line must end with a semicolon in a program written in the language we are considering ("Pascal"). Computer science teaches this to schoolchildren and students, and you can understand these rules yourself!
A semicolon (;) is a conditional signal that indicates the end of the current line and the need to switch to a new one. But the exception may be utility commands: const, var, begin, and others.
The end statement closes the program, so a dot is required after it. Sometimes the code may contain several attachments, then the beginning and end of the block will be separated by a semicolon.
To assign a certain value to a variable, a colon must be placed before the sign. For example, you go to ask n = 13, and in the code it will look like n: = 13.
If you learn these rules, you can quickly learn how to write program code without syntax errors.
Classical Pascal language operators
You can program repeating code fragments of a future application and carry out any actions with it using various methods. But the Pascal language uses various operators for this. We will not be able to consider all of them, so we will deal only with some.
For example, using the selection operator, you can choose one of the alternative paths for the program. The parameter in this case is an expression of ordinal type. But there is one caveat: this selection key cannot be of type string or real.
There are also assignment operators, conditional, compound and empty, as well as a host of other useful attachments. Knowing only a few of them allows you to write code with excellent functionality. Operators should not be abused, because their large number makes the program difficult to debug by the compiler, confusing and very difficult for other people to perceive.
Assignment operator
This expression has the form of a colon and an equal sign. It is used to assign a specific variable a specific value. It is important to remember that the type of the expression and the variable must be the same if they do not belong to the integer and the actual type, respectively. Only in such a situation will a direct transformation take place.
Compound statements
Pascal is a programming language that uses sequences of arbitrary program statements enclosed in special brackets. We are talking about composite constructions, limited by the words begin and end. This is an important tool of the algorithmic language, with the help of which it becomes possible to write code using the structural methodology.
The operators of the Pascal language, which are part of the composite structure, can be completely different, because there are no restrictions. The depth of nesting can also be different.
Programming language conditional operator
This component provides an opportunity during the program to check a given condition and perform an action that depends on the results of its passage. Thus, a conditional command is one of the means of forming branches in the process of performing calculations.
The structurally conditional operator is as follows:
IF <condition> THEN <statement1> ELSE <statement2>.
In this expression, else, then, and if are reserved words, the condition is a logical expression with arbitrary content, and the operators are any commands of the programming language used.
Program Code Structure
The heading, sections of operators and descriptions are key components of an application written in a language such as Pascal. Computer science allows you to fully study these elements and learn how to use them correctly.
The header usually contains the code name. For example, Program MyFirst.
In the descriptions section, connected libraries, modules, labels, constants, types, variables, the chapter describing functions and procedures can appear.
The module description section contains the names of the connected libraries inside and begins with the reserved word uses. It should be the first among all other descriptions. Module names must be separated by commas.
You can put a label on any operator of the program code whose name should be mentioned in the corresponding section of the description.
A premature description of constants allows you to further write their names in the code instead of numeric or alphabetic values.
In the description section of the variables used, you should indicate all the types that will be involved: "var c, a, r: integer; k, l, m: char; h1, h2: boolean;".
Do not forget that Pascal is a programming language that requires a mandatory preliminary description of all the components involved in the program.
The code text must end with a period.
Program Examples
Pascal is an elementary language, and after studying the above information, you can proceed directly to writing the code.
Let's make the application display the phrase βIt is my first program!β
Pascal's sample programs are very important to understand, so try it now.
Begin
Writeln (It is my first program! ');
End.
That's so simple!
Look at more complex code that can help you find the roots of a quadratic equation. Pay attention to the principle of the formation of computational expressions.
We hope that the examples of programs on Pascal were useful to you.