Course 101 Cheatsheet

This is the cheatsheet page for computer programming course 101, an introductions to computer programming. The following section can be used as a quick reference to understanding concepts taught in this course.

Identifiers

An identifier is a name that can be used to identify a variable, type, or routine. A identifier must start with a letter 'A'..'Z', 'a'..'z','_' and can contain the characters 'A'..'Z', 'a'..'z', '0'..'9', '_'.

The following are examples of valid identifiers.
Name
Address1
_Release
ShowMessage

Types

Types are used to define what information is associated with variables. Every type has a name in the form of an identifier. There are are several built in types and here are a few of their names.
Boolean        Byte        Char        Integer        string        Single

Variables

Variables are used to store information to be referenced and manipulated in a your programs. They also provide a way of labeling data with a descriptive name. The name of a variable is an identifier name, and the data type is specified through a type name.

When declaring a the following syntax is used:
var
  VariableName: TypeName;
Where VariableName is a valid identifier and TypeName is a valid type.

Here is an example of a variable declaration where FirstName is the variable name and string is the type.
var
  FirstName: string;
Note that a colon : symbol separates the variable name from the type name and the declaration ends with a semicolon ; symbol.

Multiple variables of the same type can be combined into a single declaration like so:
var
  FirstName, LastName, StreetAddress, City: string;
Variables of differing types can be declared like so:
var
  Message: string;
  X, Y: Integer;
  Found: Boolean;
Variables can only be declared outside of statement blocks which are enclosed by begin and end.

Assignment Statements

Assignment statements are use to change the values held by variables. When you assign a value to a variable the variable is on the left side and an expression which resolves to the same type as the variable is on the right. The two sides are separated by an assignment := operator.

When writing an assignment statement the following syntax is used:
  VaribleName := Expression;
Where VariableName is a variable name and Expression is any expression or value with a type that matches the variable, and the variable name is always on the left side of an := assignment operator.

The following are examples of assignment statements.
begin
  FirstName := EditBox.Text;
  CustomerList := CustomerSearch(FirstName);
  Found := CustomerList.Count > 0;
end;
Note, assignment statements can only be used between begin and end blocks.

Assignments have the following effects on variables.
var
  Name, Message: string;
  Age: Integer;
begin
  EditBox.Text := 'Jimmy'; // EditBox.Text now hold the value 'Jimmy'
  Name := EditBox.Text; // Name now holds the Text that was in the EditBox control
  Message := 'Hello there ' + Name; // Message now holds 'Hello there Jimmy' 
  Age := 20; // Age now holds the integer value 20
  Age := Age * 2; // Age now holds the integer value 40
  WriteLn(Message, ', your age is', Age); // Writes out 'Hello there Jimmy, your age is 40'
end;

Procedure and Function Call Statements

Statements that do not assign values to a variable can be used to call routines. These statements follow the same rules as assignment statements, but do not have an assignment := operator.

The following are examples of procedure or function call statements.
begin
  ShowMessage('Shutting down the server');
  Shutdown;
  Close;
end;

Declaring Procedures and Functions

To reuse frequent programming task you may decide to declare your own procedures or functions. A function differs from a procedure in that a function has a result.

A procedure may be declared like so:
procedure Alert;
begin
  ShowMessage('An alert has been issued');
end;
Optionally you may want to add arguments to your procedure. Arguments act as variables local to your procedure and are assigned a value when the procedure is called.
procedure Alert(Message: string);
begin
  ShowMessage('An alert has been issued with the following message: ' + Message);
end;

procedure CheckMemory;
begin
  if MemoryIsLow then
    Alert('You are running low on memory');
end;
In the case above the Message argument may contain the text 'You are running low on memory'.

Functions can return values back to the caller. A function may look like this:
function StrToUpper(S: string): string;
var
  I: Integer;
begin
  Result := '';
  for I := 1 to Length(S) do
    if (S[I] >= 'a') and (S[I] <= 'z') then
      Result := Result + Chr(Ord(S[I]) + Ord('A') - Ord('a'))
    else
      Result := Result + S[I];
end;

begin
  WriteLn(StrToUpper('Hello')); // Writes out 'HELLO'
end.