KEMBAR78
Pseudocode Caie | PDF | Data Type | Inheritance (Object Oriented Programming)
0% found this document useful (0 votes)
80 views6 pages

Pseudocode Caie

The document provides an overview of pseudocode conventions and programming concepts that may be examined, including: - Lines are indented by three spaces and keywords are in uppercase or PascalCase - Basic data types include integers, reals, characters, strings, booleans, and dates - Variables are declared with identifiers and types, and arrays can be one or two dimensional - User-defined types like enums, records, sets, and pointers can be created - Common operations include arithmetic, relational, logical, strings, and numeric functions - Control structures include if/else selection and for, repeat, and while iteration - Procedures and functions can be used along with passing arguments by value or reference -

Uploaded by

abkjr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views6 pages

Pseudocode Caie

The document provides an overview of pseudocode conventions and programming concepts that may be examined, including: - Lines are indented by three spaces and keywords are in uppercase or PascalCase - Basic data types include integers, reals, characters, strings, booleans, and dates - Variables are declared with identifiers and types, and arrays can be one or two dimensional - User-defined types like enums, records, sets, and pointers can be created - Common operations include arithmetic, relational, logical, strings, and numeric functions - Control structures include if/else selection and for, repeat, and while iteration - Procedures and functions can be used along with passing arguments by value or reference -

Uploaded by

abkjr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

#cs/pseudocode

Taken from the CAIE pseudocode 9618 guide.

## 1 Pseudocode in examined components

**Lines are indented usually by {{three}} spaces**


```
//comment here
INPUT Password
IF Password = "" THEN
OUTPUT "Domain name not found"
ENDIF
```

**Keywords are in UPPERCASE and {{PascalCase}} is preferred for identifiers**


```
DECLARE CountNumber : INTEGER
```

## 2 Variables, constants and data types

| Data Type | Description | Examples (literals) |


| ---- | ---- | ---- |
| INTEGER | A whole number | 5, -3 |
| REAL | A number capable of containing {{fractions}} | 4.7, -4.0 |
| CHAR | A single character | 'c', 'A' |
| STRING | A sequence of zero or more characters | "This is a string", "" |
| BOOLEAN | Logical values TRUE and FALSE | TRUE, FALSE |
| DATE | A valid calendar date | 12/12/2012 |
**Basic data types available**

**Identifiers**
- Can only contain letters (A–Z, a–z), digits (0–9) and the underscore character
( _ )
- The names given to variables, constants, procedures and functions
- They must start with a letter and not a digit
- Good practice to make them descriptive
- Should be unique (+ case insensitive)

**Declare a variable with identifier Name and type STRING**


?
```
DECLARE Name : STRING
```

**Declare a constant (does not require type definition), identifier PI and value
3.14**
?
```
CONSTANT PI = 3.14
```

**Assigning a value to an identifier**


```
Count <- Count + 1
```

## 3 Arrays
**Declaring and accessing 1d/2d arrays**
```
DECLARE StudentNames : ARRAY[1:30] OF STRING //1d array
DECLARE NoughtsAndCrosses : ARRAY[1:3,1:3] OF CHAR //2d array

//Accessing array index values


StudentNames[1] ← "Ali"
NoughtsAndCrosses[2,3] ← ꞌXꞌ
```

## 4 User-defined data types

### Non-composite data types

```
//enum type
TYPE Season = (Spring, Summer, Autumn, Winter)

//pointer type, uses (^ (caret)) to reference


TYPE TSeasonPointer = ^Season

DECLARE MyPointer: TSeasonPointer


DECLARE ThisSeason : Season
DECLARE NextSeason : Season

ThisSeason ← Spring
MyPointer ← ^ThisSeason
NextSeason ← MyPointer^ + 1
// access the value stored at the memory address
```

### Composite data type

Consisting of one or more data types, grouped under one identifier.

```
//record
TYPE StudentRecord
DECLARE Name : STRING
DECLARE YearGroup : INTEGER
ENDTYPE

//set (collection of unique values)


TYPE LetterSet = SET OF CHAR
DEFINE Vowels ('A','E','I','O','U'): LetterSet

//using user-defined data types


DECLARE Class: ARRAY[1:10] OF StudentRecord

FOR Index ← 1 TO 10
Class[Index].YearGroup ← Class[Index].YearGroup + 1
//+1 to all StudentRecord YearGroups
NEXT Index
```

## 5 Common operations
```
//input from keyboard and output to console
INPUT Lives
OUTPUT "You have ", Lives, " lives left"

//Arithmetic and relational operations

+, -, *, /, DIV, MOD

>,<, <=, >=

= (equal to)

<> (not equal to)

//logic operators

AND, OR, NOT

//string functions and operations

String manipulation functions will always be provided in examinations

//numeric functions
INT(x : REAL) RETURNS INTEGER

RAND(x : INTEGER) RETURNS REAL (not inclusive of x)

```

## 6 Selection

```
//if statement
IF <condition> THEN
OUTPUT "true"
ELSE
OUTPUT "FALSE"
ENDIF

//case statement
INPUT Number
CASE OF Number
0 TO 10: OUTPUT "10 or below"
11: OUTPUT "11 entered"
OTHERWISE: OUTPUT "invalid number"
ENDCASE

```

## 7 Iteration(repetition)

```
//count-controlled (FOR) loops
Total ← 0
FOR Index <- 1 TO 10 (+ optional 'STEP <increment>' using integer)
Total <- Total + 1
NEXT Index
//post-condition (REPEAT) loops
REPEAT
OUTPUT "Please enter the password"
INPUT Password
UNTIL Password = "Secret"

//pre-condition (WHILE) loops


WHILE Number > 9
Number ← Number – 9
ENDWHILE
```

## 8 Procedures and functions

```
//procedures do not return a value
PROCEDURE OutputString(StringValue: STRING)
OUTPUT ""
ENDPROCEDURE

//procedures are called like so


CALL OutputString("hello")

//functions DON'T use the CALL keyword, and must return a value
FUNCTION AddTen(Number: REAL) RETURNS REAL
RETURN (Number + 10.0)
ENDFUNCTION

OUTPUT AddTen(10.0) //outputs 20.0

//Passing by value is assumed, not reference (pointer)


//allowing passing by reference
PROCEDURE SWAP(BYREF X : INTEGER, BYVALUE Y : INTEGER) //BYVALUE is inferred by not
using it
Temp ← X
X ← Y
Y ← Temp
ENDPROCEDURE

```

## 9 File handling

```
//four file modes:
- READ: reading a file
- WRITE: a new file created, any existing data in the file is lost
- APPEND: appending data, after any existing data
- RANDOM: opening random file

//reading and writing


- WRITEFILE "books.txt", "content" (writes a line)
- READFILE "books.txt", Variable (reads a line into Variable)

//opening a file
DECLARE BookNameLine : STRING
OPENFILE "books.txt" FOR READ
WHILE NOT EOF("books.txt")
READFILE "books.txt", BookNameLine //line by line
IF BookNameLine <> "" THEN
OUTPUT BookNameLine
ENDIF
ENDWHILE
CLOSEFILE "books.txt"

//random files
- Random files contain a collection of data, normally as records of fixed length.
They can be thought of as having a file pointer which can be moved to any location
or address in the file. The record at that location can then be read or written.

The SEEK command moves the file pointer to a given location:


SEEK <file identifier>, <address>

The command GETRECORD should be used to read the record at the file pointer:
GETRECORD <file identifier>, <variable>

The command PUTRECORD is used to write a record into the file at the file pointer:
PUTRECORD <file identifier>, <variable>

Example – handling random files


The records from positions 10 to 20 of a file StudentFile.Dat are moved to the next
position and a new
record is inserted into position 10. The example uses the user-defined type Student
defined in Section 4.1.
DECLARE Pupil : Student
DECLARE NewPupil : Student
DECLARE Position : INTEGER
NewPupil.LastName ← "Johnson"
NewPupil.Firstname ← "Leroy"
NewPupil.DateOfBirth ← 02/01/2005
NewPupil.YearGroup ← 6
NewPupil.FormGroup ← ꞌAꞌ
OPENFILE "StudentFile.Dat" FOR RANDOM
FOR Position ← 20 TO 10 STEP -1
SEEK "StudentFile.Dat", Position
GETRECORD "StudentFile.Dat", Pupil
SEEK "StudentFile.Dat", Position + 1
PUTRECORD "StudentFile.Dat", Pupil
NEXT Position
SEEK "StudentFile.Dat", 10
PUTRECORD "StudentFile.Dat", NewPupil
CLOSEFILE "StudentFile.dat"
```

## Object-oriented Programming

```
Constructors will be procedures with the name NEW.
CLASS Pet
PRIVATE Name : STRING
PUBLIC PROCEDURE NEW(GivenName : STRING)
Name ← GivenName
ENDPROCEDURE
ENDCLASS

Inheritance is denoted by the INHERITS keyword; superclass/parent class methods


will be called using the
keyword SUPER, for example:

CLASS Cat INHERITS Pet


PRIVATE Breed: INTEGER
PUBLIC PROCEDURE NEW(GivenName : STRING, GivenBreed : STRING)
SUPER.NEW(GivenName)
Breed ← GivenBreed
ENDPROCEDURE
ENDCLASS
```

You might also like