Kamis, 09 Oktober 2014

PROGRAM PASCAL


SEKITAR PROGAM PASCAL
 Program HelloWorld;
Begin WriteLn('Hello world!') {no ";" is required after the last statement of a block - adding one adds a "null statement" to the program} End.
Data types A type in Pascal, and in several other popular programming languages, defines a variable in such a way that it defines a range of values which the variable is capable of storing, and it also defines a set of operations that are permissible to be performed on variables of that type. The predefined types are: Data type Type of values which the variable is capable of storing integer integer (whole) numbers real floating-point numbers boolean the value True or False char a single character from an ordered character set string a group or "string" of characters The range of values allowed for each (except boolean) is implementation defined. Functions are provided for some data conversions. For conversion of real to integer, the following functions are available: round (which rounds to integer using banker's rounding) and trunc (rounds towards zero). The programmer has the freedom to define other commonly used data types (e.g. byte, string, etc.) in terms of the predefined types using Pascal's type declaration facility, for example type byte = 0..255; signed_byte = -128..127; string = packed array[1..255] of char; (Often-used types like byte and string are already defined in many implementations.) Subrange types Subranges of any ordinal data type (any simple type except real) can also be made: var x : 1..10; y : 'a'..'z'; Set types In contrast with other programming languages from its time, Pascal supports a set type: var Set1 : set of 1..10; Set2 : set of 'a'..'z'; A set is a fundamental concept for modern mathematics, and they may be used in many algorithms. Such a feature is useful and may be faster than an equivalent construct in a language that does not support sets. For example, for many Pascal compilers: if i in [5..10] then ... executes faster than: if (i > 4) and (i < 11) then ... Sets of non-contiguous values can be particularly useful, in terms of both performance and readability: if i in [0..3, 7, 9, 12..15] then ... For these examples, which involve sets over small domains, the improved performance is usually achieved by the compiler representing set variables as bit vectors. The set operators can then be implemented efficiently as bitwise machine code operations. Type declarations Types can be defined from other types using type declarations: type x = integer; y = x; ... Further, complex types can be constructed from simple types: type a = array[1..10] of integer; b = record x : integer; y : char end; c = file of a; File type As shown in the example above, Pascal files are sequences of components. Every file has a buffer variable which is denoted by f^. The procedures get (for reading) and put (for writing) move the buffer variable to the next element. Read is introduced such that read(f, x) is the same as x := f^; get(f);. Write is introduced such that write(f, x) is the same as f^ := x; put(f); The type text is predefined as file of char. While the buffer variable could be used for inspecting the next character to be used (check for a digit before reading an integer), this leads to serious problems with interactive programs in early implementations, but was solved later with the "lazy I/O" concept. In Jensen & Wirth Pascal, strings are represented as packed arrays of chars; they therefore have fixed length and are usually space-padded. Some dialects have a custom string type; e.g. Delphi 2 has a new string type whose length is stored in a 32-bit value instead of a single byte. Pointer types Pascal supports the use of pointers: type pNode = ^Node; Node = record a : integer; b : char; c : pNode {extra semicolon not strictly required} end; var NodePtr : pNode; IntPtr : ^integer; Here the variable NodePtr is a pointer to the data type Node, a record. Pointers can be used before they are declared. This is a forward declaration, an exception to the rule that things must be declared before they are used. To create a new record and assign the value 10 and character A to the fields a and b in the record, and to initialise the pointer c to NIL, the commands would be: New(NodePtr); ... NodePtr^.a := 10; NodePtr^.b := 'A'; NodePtr^.c := NIL; ... This could also be done using the with statement, as follows: New(NodePtr); ... with NodePtr^ do begin a := 10; b := 'A'; c := NIL end; ...

Tidak ada komentar:

Posting Komentar

CEK CEK DAN CEK

LINK SOAL SMK AL FURQON

  PILIH  MENU    DIBAWAH   LALU  COLEK   DAN  COPAS  DAN PASTE  DIBROWSER   https://docs.google.com/document/d/1rb0LF_1GaQZopEUQRm9iAWBVAO4...

UPDATE