Lecture notes, cheat sheets
Computer science and information technology. Pascal language (lecture notes) Directory / Lecture notes, cheat sheets Table of contents (expand) LECTURE No. 2. Pascal language 1. Introduction to the Pascal language The basic symbols of the language - letters, numbers and special characters - make up its alphabet. The Pascal language includes the following set of basic symbols: 1) 26 Latin lowercase and 26 Latin uppercase letters: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z abcdefghijklmnopqrstuvwxyz; 2) _ (underscore); 3) 10 digits: 0123456789; 4) signs of operations: + - x / = <> = := @; 5) limiters: ., ' ( ) [ ] (..) { } (* *).. : ; 6) specifiers: ^ # $; 7) service (reserved) words: ABSOLUTE, ASSEMBLER, AND, ARRAY, ASM, BEGIN, CASE, CONST, CONSTRUCTOR, DESTRUCTOR, DIV, DO, DOWNTO, ELSE, END, EXPORT, EXTERNAL, FAR, FILE, FOR, FORWARD, FUNCTION, GOTO, IF, IMPLEMENTATION, IN, INDEX, INHERITED, INLINE, INTERFACE, INTERRUPT, LABEL, LIBRARY, MOD, NAME, NIL, NEAR, NOT, OBJECT, OF, OR, PACKED, PRIVATE, PROCEDURE, PROGRAM, PUBLIC, RECORD, REPEAT, RESIDENT, SET, SHL, SHR, STRING, THEN, TO, TYPE, UNIT, UNTIL, USES, VAR, VIRTUAL, WHILE, WITH, XOR. In addition to those listed, the set of basic characters includes a space. Spaces cannot be used inside double characters and reserved words. Type concept for data It is customary in mathematics to classify variables according to some important characteristics. A strict distinction is made between real, complex and logical variables, between variables representing individual values and a set of values, etc. When processing data on a computer, such a classification is even more important. In any algorithmic language, every constant, variable, expression, or function is of a particular type. There is a rule in Pascal: the type is explicitly specified in the declaration of a variable or function that precedes its use. The Pascal type concept has the following main properties: 1) any data type defines a set of values to which a constant belongs, which a variable or expression can take, or an operation or function can produce; 2) the type of value given by a constant, variable or expression can be determined by their form or description; 3) each operation or function requires fixed type arguments and produces a fixed type result. It follows that the compiler can use type information to check the computability and correctness of various constructs. The type defines: 1) possible values of variables, constants, functions, expressions belonging to a given type; 2) the internal form of data presentation in a computer; 3) operations and functions that can be performed on values belonging to a given type. It should be noted that the mandatory description of the type leads to redundancy in the text of programs, but such redundancy is an important auxiliary tool for developing programs and is considered as a necessary property of modern high-level algorithmic languages. There are scalar and structured data types in Pascal. Scalar types include standard types and user-defined types. Standard types include integer, real, character, boolean, and address types. Integer types define constants, variables, and functions whose values are realized by the set of integers allowed in a given computer. Real types defines those data that are implemented by a subset of real numbers that are allowed in a given computer. The user-defined types are enum and range. Structured types come in four flavors: arrays, sets, records, and files. In addition to those listed, Pascal includes two more types - procedural and object. A language expression consists of constants, variables, function pointers, operator signs, and brackets. An expression defines a rule for calculating some value. The order of calculation is determined by the precedence (priority) of the operations contained in it. Pascal has the following operator precedence: 1) calculations in parentheses; 2) calculation of function values; 3) unary operations; 4) operations *, /, div, mod, and; 5) operations +, -, or, xor; 6) relational operations =, <>, <, >, <=, >=. Expressions are part of many Pascal language operators and can also be arguments to built-in functions. 2. Standard procedures and functions Arithmetic functions 1.Function Abs(X); Returns the absolute value of the parameter. X is an expression of real or integer type. 2. Function ArcTan(X: Extended): Extended; Returns the arc tangent of the argument. X is an expression of real or integer type. 3. Function exp(X: Real): Real; Returns the exponent. X is an expression of real or integer type. 4.Frac(X: Real): Real; Returns the fractional part of the argument. X is a real type expression. The result is the fractional part of X, i.e. Frac(X) = X-Int(X). 5. Function Int(X: Real): Real; Returns the integer part of the argument. X is a real type expression. The result is the integer part of X, i.e. X rounded towards zero. 6. Function Ln(X: Real): Real; Returns the natural logarithm (Ln e = 1) of a real-type expression X. 7.Function Pi: Extended; Returns the Pi value, which is defined as 3.1415926535. 8.Function Sin(X: Extended): Extended; Returns the sine of the argument. X is a real type expression. Sin returns the sine of angle X in radians. 9.Function Sqr(X: Extended): Extended; Returns the square of the argument. X is a floating point expression. The result is of the same type as X. 10.Function Sqrt(X: Extended): Extended; Returns the square root of the argument. X is a floating point expression. The result is the square root of X. Value Conversion Procedures and Functions 1. Procedure Str(X [: Width [: Decimals]]; var S); Converts the number X to a string representation according to Width and Decimals formatting options. X is an expression of a real or integer type. Width and Decimals are integer type expressions. S is a variable of type String or a null-terminated character array if the extended syntax is allowed. 2. Function Chr(X: Byte): Char; Returns the character with ordinal X in the ASCII table. 3.Function High(X); Returns the largest value in the range of the parameter. 4.FunctionLow(X); Returns the smallest value in the parameter range. 5 FunctionOrd(X): Longint; Returns the ordinal value of an enumerated type expression. X is an enumerated type expression. 6. Function Round(X: Extended): Longint; Rounds a real value to an integer. X is a real type expression. Round returns a Longint value, which is the value of X rounded to the nearest whole number. If X is exactly halfway between two integers, the number with the largest absolute value is returned. If the rounded value of X is outside the Longint range, a run-time error is generated that you can handle using the EInvalidOp exception. 7. Function Trunc(X: Extended): Longint; Truncates a real type value to an integer. If the rounded value of X is outside the Longint range, a run-time error is generated that you can handle using the EInvalidOp exception. 8. Procedure Val(S; var V; var Code: Integer); Converts a number from a string value S to a number representation V. S - string type expression - a sequence of characters that forms an integer or real number. If the S expression is invalid, the index of the invalid character is stored in the Code variable. Otherwise Code is set to zero. Ordinal Value Procedures and Functions 1. Procedure Dec(varX [; N: LongInt]); Subtracts one or N from the variable X. Dec(X) corresponds to X:= X - 1, and Dec(X, N) corresponds to X:= X - N. X is a variable of an enumerated type, or of type PChar if the extended syntax is allowed, and N is an expression of integer type. The Dec procedure generates optimal code and is especially useful in long loops. 2. Procedure Inc(varX [; N: LongInt]); Adds one or N to the variable X. X is a variable of enumerated type or PChar type if the extended syntax is allowed, and N is an expression of integral type. Inc (X) matches the instruction X:= X + 1, and Inc (X, N) matches the instruction X:= X + N. The Inc procedure generates optimal code and is especially useful in long loops. 3. FunctionOdd(X: LongInt): Boolean; Returns True if X is an odd number, False otherwise. 4.Function Pred(X); Returns the previous value of the parameter. X is an enumerated type expression. The result is of the same type. 5 Function Succ(X); Returns the next parameter value. X is an enumerated type expression. The result is of the same type. 3. Pascal language operators Conditional operator The format of the full conditional statement is defined as follows: If B then SI else S2; where B is a branching condition (decision making), a logical expression or a relation; SI, S2 - one executable statement, simple or compound. When executing a conditional statement, first the expression B is evaluated, then its result is analyzed: if B is true, then the statement S1 is executed - the branch of then, and the statement S2 is skipped; if B is false, then statement S2 - the else branch is executed, and statement S1 is skipped. There is also an abbreviated form of the conditional operator. It is written as: If B then S. Selection operator The operator structure is as follows: case S of c1: instruction1; c2: instruction2; ... cn: instructionN; else instruction end; where S is an ordinal type expression whose value is being calculated; с1, с2..., сп - constants of ordinal type with which expressions are compared S; instruction1,..., instructionN - operators of which the one whose constant matches the value of the expression S is executed; instruction - a statement that is executed if the value of the Sylq expression matches none of the constants c1, c2.... cn. This operator is a generalization of the conditional If operator for an arbitrary number of alternatives. There is an abbreviated form of the statement where there is no else branch. Loop statement with parameter Parameter loop statements that begin with the word for cause the statement, which can be a compound statement, to be repeatedly executed while the control variable is assigned an ascending sequence of values. General view of the for operator: for <loop counter> := <start value> to <end value> do <statement>; When the for statement starts executing, the start and end values are determined once, and these values are retained throughout the execution of the for statement. The statement contained in the body of the for statement is executed once for each value in the range between the start and end values. The loop counter is always initialized to an initial value. When the for statement is running, the value of the loop counter is incremented with each iteration. If the start value is greater than the end value, then the statement contained in the body of the for statement is not executed. When the downto keyword is used in a loop statement, the value of the control variable is decremented by one on each iteration. If the start value in such a statement is less than the end value, then the statement contained in the body of the loop statement is not executed. If the statement contained in the body of the for statement changes the value of the loop counter, then this is an error. After the execution of the for statement, the value of the control variable becomes undefined, unless the execution of the for statement was interrupted by a jump statement. Loop statement with precondition A precondition loop statement (beginning with the while keyword) contains an expression that controls the repeated execution of the statement (which can be a compound statement). Cycle shape: While B do S; where B is a logical condition, the truth of which is checked (it is a condition for terminating the loop); S - loop body - one statement. The expression that controls the repetition of a statement must be of type Boolean. It is evaluated before the inner statement is executed. The inner statement is executed repeatedly as long as the expression evaluates to True. If the expression evaluates to False from the start, then the statement contained within the precondition loop statement is not executed. Loop statement with postcondition In a loop statement with a postcondition (beginning with the word repeat), the expression that controls the repeated execution of a sequence of statements is contained within the repeat statement. Cycle shape: repeat S until B; where B is a logical condition, the truth of which is checked (it is a condition for terminating the loop); S - one or more loop body statements. The result of the expression must be of a boolean type. The statements enclosed between the repeat and until keywords are executed sequentially until the result of the expression evaluates to True. The statement sequence will be executed at least once because the expression is evaluated after each execution of the statement sequence. Author: Tsvetkova A.V. << Back: Introduction to Computer Science (Informatics. Information. Presentation and processing of information. Number systems. Representation of numbers in a computer. Formalized concept of an algorithm) >> Forward: Procedures and functions (The concept of an auxiliary algorithm. Procedures in Pascal. Functions in Pascal. Anticipatory descriptions and connection of subroutines. Directive) We recommend interesting articles Section Lecture notes, cheat sheets: ▪ World economy. Lecture notes See other articles Section Lecture notes, cheat sheets. Read and write useful comments on this article. Latest news of science and technology, new electronics: The existence of an entropy rule for quantum entanglement has been proven
09.05.2024 Mini air conditioner Sony Reon Pocket 5
09.05.2024 Energy from space for Starship
08.05.2024
Other interesting news: ▪ Ultrasonic tweezers move living cells ▪ Unmanned amphibious aircraft to deliver mail to the island ▪ Underwater sacrificial objects discovered ▪ Wireless charging from Intel ▪ Photosynthetic engine for artificial cells News feed of science and technology, new electronics
Interesting materials of the Free Technical Library: ▪ section of the site Biographies of great scientists. Article selection ▪ article Right to work. Popular expression ▪ Where Did Dinosaurs Live? Detailed answer ▪ article Urd. Legends, cultivation, methods of application ▪ article Null Modem 9-25 pin (COM-COM). Encyclopedia of radio electronics and electrical engineering
Leave your comment on this article: All languages of this page Home page | Library | Articles | Website map | Site Reviews www.diagram.com.ua |