Data Types, Algorithms, and Arrays in Programming

Data Types and Basic Algorithms

1. Defining Data Types

Data types define the set of values a variable can hold and the set of operations that can be performed on that variable.

2. Primitive Data Types

Primitive data types can be divided into:

  • Numeric: Integer and Real
  • Alphanumeric: Character and String
  • Logical: True or False

3. Primitive Instructions

  • Input: Allows the user to provide data. Example: READ(NUM)
  • Assignment: Stores information in a variable. Example: X <- 5
  • Output: Displays information to the user. Example: WRITE("The sum is", SUM)

4. Purely Sequential Algorithm

An algorithm where instructions are executed sequentially, one after another. Each instruction is executed only once and uses only the three primitive instructions.

5. Simple Selection Structure Syntax

Syntax:

IF <condition> THEN
  <commands1>
  [ELSE <commands2>] (optional)
ENDIF

Operation: The <condition> (a logical expression) is evaluated. If true, <commands1> are executed. If false, <commands2> are executed (if present). Execution then continues after ENDIF.

6. Selection Condition Examples

A condition is a logical expression that evaluates to true or false. Examples:

  • X >= 10
  • (NUM >= 1) AND (NUM <= 3)
  • NAME = "MARY"

7. Repetition Structure (WHILE) Syntax

Syntax:

WHILE <condition> DO
  <commands>
ENDWHILE

The condition is evaluated. If true, the commands are executed. ENDWHILE triggers a re-evaluation of the condition. This repeats until the condition is false. Execution then continues after ENDWHILE.

Why a command must modify the condition’s variable: Without a command that modifies the variable within the condition, the loop will repeat indefinitely, creating an infinite loop.

Advanced Concepts: Initialization, Variables, and Arrays

1. Initialization

Placing an initial value in a variable. Variables should always be initialized before use.

2. Counter and Accumulator Variables

  • Accumulator: A numeric variable that receives an initial value and is incremented by a variable amount with each repetition.
  • Counter: A numeric variable that receives an initial value and is incremented by a fixed amount with each repetition.

3. Flag Variable

A logical variable used to establish and test a condition to control a repetition.

4. Types of Control for Final Value in Repetition

  1. Fixed and known number of repetitions.
  2. Variable number of repetitions, known within the algorithm.
  3. Unknown number of repetitions.

5. FOR Structure

Used when the number of repetitions is known in advance.

Operation (simplified): A variable is assigned a starting value and compared to a final value. If the variable is within the range, the commands are executed. ENDFOR triggers a re-evaluation. This continues until the variable is outside the range.

6. Vectors

Vectors are sets of variables of the same data type, sharing the same name, and accessed by an index.

7. Arrays (Matrices)

Arrays are homogeneous, multidimensional structures (all elements are of the same type).

Declaration Example (3×4 matrix):

VAR
  MAT: ARRAY[1..3, 1..4] OF REAL

8. Square Matrices

A square matrix has an equal number of rows and columns.

  • Main Diagonal: Row index equals column index.
  • Secondary Diagonal: Row index equals (matrix size + 1) minus column index.