0% found this document useful (0 votes)
21 views

2 2-Programming-Fundamentals 250009778

This document provides an overview of various programming fundamentals including data types, operators, variables, constants, arrays, selection, iteration, functions, procedures, strings, file handling, SQL, and records. It defines each concept and provides examples to illustrate how they work in a programming context.

Uploaded by

Mark Robson
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

2 2-Programming-Fundamentals 250009778

This document provides an overview of various programming fundamentals including data types, operators, variables, constants, arrays, selection, iteration, functions, procedures, strings, file handling, SQL, and records. It defines each concept and provides examples to illustrate how they work in a programming context.

Uploaded by

Mark Robson
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

2.

2 PROGRAMMING FUNDAMENTALS
DATA TYPES OPERATORS

Data Definition Operator/Function Definition


Type Exponentiation Raises a number to a power eg: 2**3 OR 2 ^3 (=23)
String Text eg: “Hello” Quotient/DIV Gives the whole number after a division
Integer Whole number eg: 32 Remainder/MOD Gives the remainder part of a division
Float/Real Decimal number eg: 1.2 == Is equal to
Boolean Two values eg: true or false ! or <> Is not equal to
Character A single character eg: b < Is less than
> Is more than
Casting is when you want to change >= Is more than or equal to
between data types. Eg – if you want to <= Is less than or equal to
use an integer in a sentence you would
need to convert it to a string.
ARRAYS

One-Dimensional Arrays- this is like a list. array students [3]


VARIABLES AND CONSTANTS In this example an array has been created called students [0] = “Bob”
students. The list can hold 3 items (as shown). students [1] = “Dave”
students [2] = “Bob”
Variable – A value which may change
while the program is running. Variables This command would print the second item (1)
can be local or global. From the array. It would print “Dave”. print(students[1])

Local Variable – a variable which can


only be used within the structure they Two-Dimensional Arrays – these are lists within lists (like a table)
are declared in.
Grades=[[“Bob”, “22%”, “44%”], [“Dave”, 0 1 2
Global Variable – a variable which can “85%”, “100%”]]
be used in any part of the code after 0 Bob 22% 44%
they are declared The code above creates the 2D array. The code
Below would output: 1 Dave 85% 100%
Constant – A value which cannot be “Bob’s first test score was 22%”
altered as the program is running.
print(“Bob’s first test score was “ + Grades [0, 1]
2.2 PROGRAMMING FUNDAMENTALS CONTINUED
PROGRAMMING CONSTRUCTS STRING MANIPULATION
A Sequence is when there are 0 1 2 3 The characters in a string are numbered starting
programming steps that are W o r d with position 0.
carried out one after another.
Function Purpose
Selection is where there are x.length Gives the length of the string
different paths in your code x.upper Changes the characters in the string to upper case
eg: IF, ELIF, ELSE x.lower Changes the characters in the string to lower case
x[i] Gives the character in position i. Eg: x[2] = “r”
Iteration is when there is x.substring(a,b) Gives the characters from position a with length b.
repetition (loops) in code. Eg: x.subString(1,2) = or
This could be a WHILE loop (do + Joins (concatenates) two strings together
something WHILE a condition is
met) or a FOR loop (do
something for a set number of FILE HANDLING
times)
Myfile=openRead(“myfile.text”) Opens the file in read mode
This count-controlled loop would print Myfile=openWrite(“myfile.text”) Opens the file in write mode
“Hello World” 8 times.: Myfile.writeLine (“Hello”) Writes a line to the file
for i=0 to 7 Line1=myfile.readLine() Reads one line of the file
print (“Hello”) Myfile.close() Closes the file
next i endOfFile() Used to determined the end of a file

These condition controlled loops would IF/ELSE AND SWITCH/CASE FOR SELECTION
check if a password’s correct:
Selection can be shown using IF/ELSE or SWITCH/CASE
while answer != ”letmein123” IF ELSE SWITCH/CASE
answer=input(“Enter password”) If choice == “a” then Switch entry:
endwhile print(“You chose A”) case “A”:
elseif choice==”b” then print(“You chose A”)
print(“You chose B”) case “B”:
do else print(“You chose B”)
answer=input(“Enter password”) print(“Unrecognised choice”) default:
until answer==”letmein123” print(“Unrecognised choice”)
2.2 PROGRAMMING FUNDAMENTALS CONTINUED
SUB PROGRAMS RECORDS
Procedures are a set of instructions stored under a name so that you
can call the procedure to run the whole set of instructions. Records are a data structure used to store
A function is like a procedure but always returns a value. a collection of data. They can store
Parameters are variables used to pass values into a function or information of different data types.
procedure. Field = each item in a record is a field.
Each field has a name and data type.
A procedure with parameters A procedure without parameters
procedure intro (name) procedure intro () A record can be created like this:
print(“Hello “ +name) print(“Hello”)
print(“Welcome to the game”) print(“Welcome to the game”) record students
endprocedure endprocedure int student_number
string student_name
Functions must take at least one parameter and must return a value: bool passed_test
function double(number) endrecord
print number*3
endfunction
Data can be assigned using variables:

SQL (Structured Query Language) Student1=students(1,”Bob Jones”, True)


Student2=students(2,”Steve Smith”, False)
SQL is the language used to manage and search databases. Student3=students(3,”Sally Roberts”, True)
Commands Example What it does
SELECT SELECT name, age Displays the name and age of The whole record can be accessed using the
FROM FROM students everyone in the students table variable name:
WHERE SELECT name FROM students Displays the name of everyone in
WHERE gender=male the students table who’s gender print(Student1)
is male
LIKE SELECT name FROM students Displays the students names that (1, “Bob Jones”, True)
WHERE name LIKE “% Smith” end with Smith.
AND SELECT name FROM students Displays the students who are or part of a record can be accessed:
WHERE gender=male AND male and have an attendance of
attendance > 90 more than 90. print(Student3.student_name)
* SELECT * from students Selects all of the fields from
the students table Sally Roberts

You might also like