Subtopic Notes

8.1 - Programming concepts

8. Programming

Programming is the process of writing instructions in a language a computer can understand to solve problems or perform tasks. It is a transferable skill, meaning the skills developed for one scenario can be easily transferred to another.

Algorithm

Solution to a problem expressed as a sequence of defined steps

Stages of Computer Program

  • Input: Takes in values from the user to the program
  • Storage: Temporarily or permanently saves data for use during or after program execution
  • Process: Operations that transform inputs into outputs
  • Output: Displays values to the user

Output

PYTHON

Python can be executed directly in the browser.

Editor — Python
Output
No output yet.

PSEUDOCODE

Pseudocode uses basic Cambridge-style keyword highlighting. Execution is not supported yet.

Editor — Pseudocode

Comments

You may wish to write something in the code which the computer will ignore which is why comments are needed. Text written in a program but not run by the computer is called a comment. These are basically used to explain why a part of code is used, help other people reading the code understand it faster, or Ignore a line of code.

PYTHON

Python can be executed directly in the browser.

Editor — Python
Output
No output yet.

PSEUDOCODE

Pseudocode uses basic Cambridge-style keyword highlighting. Execution is not supported yet.

Editor — Pseudocode

Variable

  • Name given to a distinct memory location and contains a value
  • Can be updated throughout the program

Constant

  • Name given to a distinct memory location and contains a value
  • Cannot be updated once it is created
  • It is good practice to use constants if this makes the pseudocode more readable

Data Types

  • INTEGER: Represents a whole number
  • REAL: Represents a floating point number
  • CHAR: Represents a character, surrounded by single quotes (‘A’)
  • STRING: Sequence of characters, surrounded by “double quotes”
  • BOOLEAN: The logical values True or False
  • ARRAY: A list of same data type
  • FILE: Resource for recording data in a computer
  • NULL: No Value

PYTHON

Python can be executed directly in the browser.

Editor — Python
Output
No output yet.

PSEUDOCODE

Pseudocode uses basic Cambridge-style keyword highlighting. Execution is not supported yet.

Editor — Pseudocode

Identifier

  • Name given to an entity distinctly identifying an entity in a program
  • Variable names, function names, class names all are identifiers
  • It’s a good practice to use PascalCase or camelCase
  • Rules
    • Must be unique
    • Cannot have spaces
    • Must begin with a letter
    • Can consist of letters, digit or underscore
    • Reserved words in the language cannot be used (You’ll notice that in pseudocode, keywords are all written in upper-case)
    • Names are case sensitive

Input

PYTHON

Python can be executed directly in the browser.

Editor — Python
Output
No output yet.

PSEUDOCODE

Pseudocode uses basic Cambridge-style keyword highlighting. Execution is not supported yet.

Editor — Pseudocode

Operators

  • An operator is a symbol or keyword in programming that tells the computer to perform a specific operation on one or more values (called operands)
  • May be used to compare types REAL/FLOAT and INTEGER
  • May be used to compare types CHAR and STRING
  • Case sensitive when used to compare types CHAR and/or STRING
  • Cannot be used to compare two records or object

Arithmetic operators

OperatorNameDescriptionPythonPseudocode
+ AdditionAdds together two valuesa + b a + b
- SubtractionSubtracts one value from anothera - b a - b
* MultiplicationMultiplies two valuesa * b a * b
/ DivisionDivides one value by anothera / y a / y
% ModulusReturns the division remaindera % y MOD(a, b)
**ExponentReturns a to the power of ba ** b ^
/ / Floor DivideDivide & return the whole numbera // b DIV(a, b)

Assignment operators

OperatorExamplePseudocode
=a = 5 a ← 5
<operator>=a += 5 (Represents a = a + 5)Not supported

Comparison operators

Returns a boolean value

PythonPseudocodeDescription
var == var = Equal to
var != var<> Not Equal
<, >, <=, >= <, >, <=, >=Greater/Less than (or equal to)

Logical operators

Works similar to logic gates

OperatorNamePseudocodeExample (Pseudocode)
andLogical andANDa > 9 AND a < 25
orLogical orORa > 9 OR a <25
notLogical NotNOTNOT (a = 25)

Precedence

Sets the rule on the order of operators

OperatorName
()Parentheses
**Exponent
* / // % Multiplication, Division, Floor Division, Modulus
+ -Addition, Subtraction
== != > < <= >=Comparison Operators
notLogical NOT
andLogical AND
orLogical OR

Conditional/Selection Statements

  • Allows a program to make decisions and execute different blocks of code based on certain conditions
  • Selects specific paths depending on the evaluation of boolean expressions

IF Statements

PYTHON

Python can be executed directly in the browser.

Editor — Python
Output
No output yet.

PSEUDOCODE

Pseudocode uses basic Cambridge-style keyword highlighting. Execution is not supported yet.

Editor — Pseudocode

Indentation: Lines are indented (usually by three spaces) to indicate that they are contained within a statement in a previous line. Notice how in the condition statements, after the conditions are given, there are some blank spaces

Else Statements

Executes one block if the condition is true, another if it's false.

PYTHON

Python can be executed directly in the browser.

Editor — Python
Output
No output yet.

PSEUDOCODE

Pseudocode uses basic Cambridge-style keyword highlighting. Execution is not supported yet.

Editor — Pseudocode

Elif Statements

PYTHON

Python can be executed directly in the browser.

Editor — Python
Output
No output yet.

PSEUDOCODE

Pseudocode uses basic Cambridge-style keyword highlighting. Execution is not supported yet.

Editor — Pseudocode

Nested If

The same program like above is now being done with nested if statements

PYTHON

Python can be executed directly in the browser.

Editor — Python
Output
No output yet.

Pass Statement (Do Nothing)

PYTHON

Python can be executed directly in the browser.

Editor — Python
Output
No output yet.

Logical Operators

PYTHON

Python can be executed directly in the browser.

Editor — Python
Output
No output yet.

CASE Statements

The match-case statement gives a more readable and efficient way to handle multiple conditions

PYTHON

Python can be executed directly in the browser.

Editor — Python
Output
No output yet.

PSEUDOCODE

Pseudocode uses basic Cambridge-style keyword highlighting. Execution is not supported yet.

Editor — Pseudocode

Iteration/Loops

Iteration is used when you want to execute a block of code repeatedly. The code is repeated for a specified number of times or until a condition is met.

Count-Controlled Iteration

The code is repeated a fixed number of times. You should use it when you know exactly how many times a block will loop. It clearly expresses the variable initialization, condition and increment in one line (Best for incrementing through an array).

range(start, stop, step)

  • start (optional):
    • Starting value of the sequence.
    • Inclusive
    • Default Value 0
  • stop (required):
    • The endpoint of the sequence
    • Exclusive
  • step (optional):
    • The difference between each number in the sequence
    • Defaults Value: 1

PYTHON

Python can be executed directly in the browser.

Editor — Python
Output
No output yet.

PSEUDOCODE

Pseudocode uses basic Cambridge-style keyword highlighting. Execution is not supported yet.

Editor — Pseudocode

Pre-Conditioned Loops

  • The condition is checked before each iteration
  • The loop executes only if the condition is true.
  • May execute zero times if the condition is false initially
  • Useful for indefinite loops or when the condition may initially be false

PYTHON

Python can be executed directly in the browser.

Editor — Python
Output
No output yet.

PSEUDOCODE

Pseudocode uses basic Cambridge-style keyword highlighting. Execution is not supported yet.

Editor — Pseudocode

Post-Conditioned Loops

  • Condition is checked after the block of code executes
  • Executes at least once regardless of the condition
  • Not available in python

PSEUDOCODE

Pseudocode uses basic Cambridge-style keyword highlighting. Execution is not supported yet.

Editor — Pseudocode

Infinite Loops: A loop that continues to execute endlessly because its termination condition is never met or there is no condition to stop it. It might cause the program to freeze so always make sure to change values of variables when needed. Some infinite loops might be intentional (eg. waiting for an input)

PYTHON

Python can be executed directly in the browser.

Editor — Python
Output
No output yet.

Nested Statements

  • Different types of constructs can be placed one inside another using indentation
  • It helps to minimize code duplication and makes testing easier

Subroutine

  • Subroutine is a reusable block of code that is given a name
  • Designed to perform a specific task
  • Often called within a program to avoid repetition.
  • Can be tested/improved independently of program
  • Easy to share procedures/functions with other programs
  • Create routines that can be called like built-in command
  • Header: The first statement in subroutine definition
    • Includes name of the subroutine
    • Includes parameter passed along with data type
    • Includes return data type if it exists

Procedure

  • Subroutine that does not return a value
  • Procedure calls are standalone statements

Function

  • Subroutine that returns a value.
  • Function calls are always made as part of an expression
  • The keyword RETURN is used as one of the statements within the body of the function to specify the value to be returned.
  • Normally, this will be the last statement in the function definition, however, if the RETURN statement is in the body of the function its execution is immediate and any subsequent lines of code are omitted.
  • Because a function returns a value that is used when the function is called, function calls are not complete program statements
  • When the RETURN statement is executed, the value returned replaces the function call in the expression and the expression is then evaluated

Parameter

  • Variables used in a subroutine to accept input values, allowing data to be passed into the subroutine.
  • A subroutine may or may not have parameters.
  • When calling a function, the parameters used must be of correct data type and of the same sequence as the definition

Argument: The actual value or data passed to the subroutine when it is called

Local Variable: Variables defined within a function or block, accessible only within that specific scope. These variables are not accessible outside the subroutine

Global Variable: Variables defined outside all functions or blocks, accessible throughout the entire program.

Procedure/Function Interface: The way a procedure or function interacts with the rest of the program. It includes its name, parameters (inputs), and, for functions, the return value.

PYTHON

Python can be executed directly in the browser.

Editor — Python
Output
No output yet.

PSEUDOCODE

Pseudocode uses basic Cambridge-style keyword highlighting. Execution is not supported yet.

Editor — Pseudocode

Library Routines

  • A library routine is a pre-written, reusable function or procedure provided by a programming library to perform common tasks, such as mathematical operations or input/output handling.
  • IDEs typically include a standard library of functions and procedures
  • Programming language development systems often offer library routines that can be easily integrated into programs
  • Some examples of library routines in pseudocode are: MOD, DIV, ROUND, RANDOM

Best Coding Practices

  • Use meaningful identifiers name for variables, constants, arrays, procedure, functions
  • Use commenting feature wisely
  • Divide the program into different modules using procedures and functions

String Manipulation

Strings are conceptually like arrays of characters. This means many operations done in arrays can be done in string. It is not zero indexed.

PYTHON

Python can be executed directly in the browser.

Editor — Python
Output
No output yet.

PSEUDOCODE

Pseudocode uses basic Cambridge-style keyword highlighting. Execution is not supported yet.

Editor — Pseudocode

Slicing/Substring: Returns a range of characters by specifying a start and end index (exclusive)
Concatenation: Combining two or more strings together

PYTHON

Python can be executed directly in the browser.

Editor — Python
Output
No output yet.

PSEUDOCODE

Pseudocode uses basic Cambridge-style keyword highlighting. Execution is not supported yet.

Editor — Pseudocode

Pseudocode String Functions

An error will be generated if a function call is not properly formed or if the parameters are of an incorrect type or an incorrect value.

String and character functions

  • A string of length 1 may be considered to be either of type CHAR or STRING
  • A CHAR may be assigned to, or concatenated with, a STRING
  • These functions are not zero-indexed
Common string/character functions
LENGTH(<identifier>) Returns the integer value representing the length of string. The identifier should be of data type string.
SUBSTRING(<identifier>, <start>, <length>) Returns a string of length length starting at position start. The identifier should be of data type string, length and start should be positive and data type integer.
LCASE(<identifier>) Returns the string/character with all characters in lower case. The identifier should be of data type string or char.
UCASE(<identifier>) Returns the string/character with all characters in upper case. The identifier should be of data type string or char.
In pseudocode, the operator , is used to concatenate (join) two strings. Example: "Summer" , "" , "Pudding" produces "Summer Pudding"

Numeric Functions

Pseudocode Numeric Function
ROUND(<identifier>, <places>) Returns the value of the identifier rounded to places number of decimal places. The identifier should be of data type real; places should be data type integer.
RANDOM() Returns a random number between 0 and 1 inclusive.
DIV(<identifier1>, <identifier2>) Returns the quotient of identifier1 divided by identifier2 with the fractional part discarded. Identifier data type is integer.
MOD(<identifier1>, <identifier2>) Returns the remainder of identifier1 divided by identifier2. Identifier data type is integer.
Example Value ← ROUND (RANDOM() * 6, 0) Returns a whole number between 0 and 6