pascal cheat sheett
Pascal Cheat Sheet
Variables:
- Variables in Pascal are declared using the var
keyword followed by the variable name and its data type.
- Example:
pascal
var
x: integer;
name: string;
Data Types:
- Pascal supports various data types such as integer, real, char, string, boolean, etc.
- Example:
pascal
var
age: integer;
price: real;
initial: char;
message: string;
isFound: boolean;
Constants:
- Constants in Pascal are declared using the const
keyword followed by the constant name and its value.
- Example:
pascal
const
PI = 3.14159;
MAX_VALUE = 100;
Control Structures:
- Pascal supports control structures like if-then-else, case, for, while, and repeat-until.
- Example:
pascal
if x > 10 then
writeln('x is greater than 10')
else
writeln('x is less than or equal to 10');
Procedures and Functions:
- Procedures and functions are defined using the procedure
and function
keywords respectively.
- Example:
```pascal
procedure greet;
begin
writeln('Hello, World!');
end;
function add(x, y: integer): integer; begin add := x + y; end; ```
Arrays:
- Arrays in Pascal are declared using the array
keyword followed by the data type and size.
- Example:
pascal
var
numbers: array[1..5] of integer;
File Handling:
- Pascal provides file handling capabilities for reading from and writing to files.
- Example:
pascal
var
fileVar: text;
begin
assign(fileVar, 'data.txt');
reset(fileVar);
// Read from the file
close(fileVar);
end;
Pointers:
- Pascal supports pointers which are variables that store the memory address of another variable.
- Example:
pascal
var
ptr: ^integer;
begin
new(ptr);
ptr^ := 10;
dispose(ptr);
end;
Functions and Procedures: - Functions return a value, while procedures do not. - Example: ```pascal function add(x, y: integer): integer; begin add := x + y; end;
procedure greet; begin writeln('Hello, World!'); end; ```
Note: For more detailed information and examples, you can refer to the official Pascal documentation or online tutorials.