2.2 Programming Fundamentals Flashcards
Sequence
Executing one instruction after another
Selection
A program branching depending on a condition
IF / ELSE statements.
Iteration
Repeating sections of code - looping.
Types of Iteration
FOR loops
WHILE loops
FOR Loops
AKA count-controlled loops are used when the number of iterations needed are known ahead of the iteration executing.
Example of FOR loops
for i in range(3):
print(“Hello”)
Hello
Hello
Hello
WHILE Loops
Condition-controlled loops are used when the number of iterations needed are not known beforehand as the variable used to determine that changes.
Example of WHILE loops
count= 10
while count>0:
print(count)
count = count-1
print(“Blast off”)
10
9
8
7
6
5
4
3
2
1
Blast off
The 4 Data Types
String
Integer
Float
Boolean
Integer
A whole number, positive or negative. Used for calculations.
Real / Float
A number with a decimal point. Used for calculations.
String
A set of characters.
Boolean
TRUE or FALSE. Used to check if something has happened.
Character
A single alphanumeric character.
Casting
Converting a variable from one data type to another.
x = int(x)
Variables
A value stored in memory that can change while the program is running.
Constants
A value that does not change while the program is running and is assigned when the program is designed.
Assignment
Giving a variable or constant a value.
Subprograms
small programs that are written within a larger, main program to perform a specific task.
Types of Subprograms
- Functions
- Procedures
Functions
Manipulates data and returns a result back to the main program.
Example of Functions
function f_to_c(temperature_in_f)
temperature_in_f= (temperature_in_f –32) * 5/9
return temperature_in_c
endfunction
Calling a Function
celsius = f_to_c(32)
Procedures
A subprogram that performs a specific task. When the task is complete, the subprogram ends and the main program continues from where it left off.
Example of Procedures
procedure clear_screen(x)
for i = 1 to x:
print(“ “)
endprocedure
Calling a Procedure
clear_screen(5)
Advantages of Subprograms
- Easy to write, test and debug as they are very small.
- Easy to understand.
- Can be reused throughout the program.
Input
A value that is read from an input device.
Output
Data generated by the computer and displayed to the user.
==
Equal to
!=
Not equal to
//
Absolute division.
50 // 4 = 12
%
MODULUS
50 % 4 = 2
To the power
String Manipulation
The use of programming techniques to modify, analyse or extract information from a string.
Concatenation
Joining two strings together
Example of Concatenation
word1 = “Computer”
word2 = “Science”
sentence = word1 + word2
= ComputerScience
Length
word1 = “Computer”
len(word1)
= 8
Character Position
word1 = “Computer”
word1[2]
= m
Choosing certain letters
word1 = “Computer”
word1[0,3]
= Com
word1[3,3]
= put
Uppercase
word1 = “Computer”
word1.upper
= COMPUTER
Lowercase
word1 - “Computer”
word1.lower
= computer
Arrays
A set of data values of the same type, stored in a sequence in a computer program. Also known as a list.
Example of Arrays
score = [“1”, “2”, “3”, “4”, “5”, “6”, “7”]
print(score[2])
= 3
2D Arrays
A two-dimensional array can hold more than one set of data. It is like a table, with data held in rows and columns.
Example of 2D Arrays
score [1,9]
Would give the value in the 2nd row and 10th column.
Up the stairs then down the corridor.
SQL
Structured Query Language. Programming language used for databases.
SQL Commands
SELECT
FROM
WHERE
Example of SQL
SELECT “Age”
FROM studentTbl
WHERE “Name” = “Rob” OR “Mia”