Python:
# variable
firstVar = 20
# constant
FIRST_CONSTANT = 20
Pseudocode
DECLARE FirstVar: Integer
FirstVar = 10
CONSTANT FirstConst = 500
pseudocode
DECLARE FirstInt : INTEGER
DECLARE FirstReal : REAL
DECLARE Female : CHAR
DECLARE FirstName : STRING
DECLARE Flage : BOOLEAN
Python
firstInt = 25
firstReal = 25.0
female = 'F'
firstName = 'Emma'
flag = True
pseudocode
INPUT FirstInt
OUTPUT "Enter a whole number "
OUTPUT "Number is " FirstInt
python
a = input()
b = input("Enter a whole number")
c = int(b)
firstInt = int(input ("Enter a whole number"))
print("number is: ", firstInt)
The ordering of the steps in an algorithm.
An incorrect order can lead to incorrect results or unnecessary extra steps.
This allows different routes through the steps of a program.
pseudocode
IF Age > 17
THEN
OUTPUT "You are an adult"
ELSE
OUTPUT "You are a child"
ENDIF
CASE OF OpValue
"+" : Answer = Number1 + Number2
"-" : Answer = Number1 - Number2
"*" : Answer = Number1 * Number2
"/" : Answer = Number1 / Number2
OTHERWISE OUTPUT "invalid operator"
ENDCASE
python
if Age > 17:
print("You are an adult")
else:
print("You are a child")
# Python doesn't have switch case
if opValue == "+":
answer = number1 + number2
elif opValue == "-":
answer = number1 - number2
elif opValue == "*":
answer = number1 * number2
elif opValue == "/":
answer = number1 / number2
else:
print("invalid operator")
pseudocode
FOR Counter ← 1 TO 10 STEP 2
OUTPUT Counter
NEXT Counter
WHILE TotalWeight < 100
TotalWeight = TotalWeight + Weight
ENDWHILE
REPEAT
NumberOfItems = NumberOfItems + 1
UNTIL NumberOfItems > 19
python
for Counter in range (1,10,2):
print(Counter)
while TotalWeight < 100:
TotalWeight = TotalWeight + Weight
pesudocode
# Totalling
TotalWeight = TotalWeight + Weight
# Counting
NumberOfItems = NumberOfItems + 1
Strings store text: every string contains several characters and each character in a string can be found by its position number.
The first character of a string in pseudocode is at position 1; the first character of a string is at position zero for Java, Python and Visual Basic.
pseudocode
LENGTH("Computer Science")
LENGTH(MyString)
SUBSTRING("Computer Science", 10, 7)
SUBSTRING(MyString, 10, 7)
UCASE("Computer Science")
UCASE(MyString)
LCASE("Computer Science")
LCASE(MyString)
Selection and iteration statements can be nested one inside another.
pseudocode
PROCEDURE Stars
OUTPUT "********"
ENDPROCEDURE
PROCEDURE Stars (Number:INTEGER)
DECLARE Counter : INTEGER
FOR Counter ← 1 TO Number
OUTPUT "*"
NEXT
ENDPROCEDURE
FUNCTION Celsius(Temperature: REAL) RETURNS REAL
RETURN (Temperature – 32) / 1.8
ENDFUNCTION
Python
def Stars():
print("************")
def Stars(Number):
for counter in range (Number):
print("*", end = '')
def Celsius(Temperature):
return (Temperature - 32) / 1.8
A global variable can be used in any part of a program – its scope covers the whole program.
A local variable can only be used by the part of the program it has been declared in; it is restricted to that part of the program.
Library routines are fully tested and ready for use in a program.
pseudocode
Value1 ← MOD(10, 3)
Value2 ← DIV(10, 3)
Value3 ← ROUND(6.97354, 2)
Value4 ← RANDOM()
python
value1 = 10%3
value2 = 10//3
value = divmod(10,3)
value3 = round(6.97354, 2)
from random import random
value4 = random()
pseudocode
// pseudocode uses a double slash to start a comment
python
#Python uses hash to start a comment for every line
pesudocode
DECLARE MyTable : ARRAY[0:9,0:2] OF INTEGER
OUTPUT “Enter these values in order”
OUTPUT “27, 19, 36, 42, 16, 89, 21, 16, 55, 34”
OUTPUT “31, 67, 98, 22, 35, 46, 71, 23, 11, 76”
OUTPUT “17, 48, 29, 95, 61, 47, 28, 13, 77, 21”
FOR ColumnIndex ← 0 TO 2
FOR RowIndex ← 0 TO 9
OUTPUT “Enter next value “
INPUT MyTable[RowIndex, ColumnIndex]
NEXT RowIndex
NEXT ColumnIndex
Computer programs store data that will be required again in a file.
pseudocode
DECLARE TextLine : STRING
DECLARE MyFile : STRING
MyFile ← "MyText.txt"
// variables are declared as normal
// writing the line of text to the file
OPEN MyFile FOR WRITE // opens file for writing
OUTPUT "Please enter a line of text"
INPUT TextLine
WRITEFILE, TextLine // writes a line of text to the file
CLOSEFILE(MyFile) // closes the file
// reading the line of text from the file
OUTPUT "The file contains this line of text:"
OPEN MyFile FOR READ // opens file for reading
READFILE, TextLine // reads a line of text from the file
OUTPUT TextLine
CLOSEFILE(MyFile) // closes the file