Programming
When algorithms are designed using flowcharts and pseudo codes, it will be
easy for the programmer to develop the code using a High Level
programming Language such as Python.
Python IDLE allows us to write and run the code.
There are 5 basic constructs in programming.
Data use – constants,variables,arrays
Sequence- order of steps in a task
Selection – choosing a path through a program
Iteration – repeating a sequence of steps in a program
Operator use –arithmetic and logical
What are variables?
Variables are named temporary storage locations that contains values
which are changing during the program execution.
What are constants?
Constants are named fixed storage locations that contains a value which
does not change during program execution.
Cambridge - Computer Science Chapter 8
Basic data types
Integer – Positive/negative whole numbers
Real – positive /negative fractional part
Char – variable/constant with a single character
String - variable/constant with several characters
Boolean - variable/constant that have ONLY 2 values
(true / false)
Input and Output
All inputs default as strings, so if the input should be an integer / real
number , commands are used to change the data type.
Ex: In python – int( ) for integer
Float ( ) for real
When displaying results as the output, several parts will be displayed with a
comma in between in python.
When writing an algorithm following concepts should be used and
understood
- Sequence
- Selection
- Iteration
- Counting and totaling
- String handling
- Use of operators
Cambridge - Computer Science Chapter 8
Sequence
*correct order of steps are very important in an algorithm.
Trace table is used to view all outputs of an algorithm in each execution of
statement manually. This is called a dry run.
Selection
Very useful technique.
Allows to follow different routes through a choice.
IF statement- for single choice with an alternative
CASE statement – for multiple choices
Iteration
Following loop structures are used for repetitive tasks in the algorithm.
Count – controlled loops (FOR loop)
Condition-controlled loops
1) pre-condition (while loop)
2) post – condition (Do..while loop / Repeat.. until)
*Python ONLY uses - FOR (count-controlled)
WHILE loops (pre-condition).
Cambridge - Computer Science Chapter 8
Totalling and counting
These are standard methods.
In python –Ex:
Totalling :- Totalweight = Totalweight + weight
Counting :- Numberofitems +=1
Numberofitems=Numberofitems+1
String handling
Strings are used to store text.
Characters in a string are positioned starting from zero.
String manipulation methods
Length – finding the number of characters
Substring – extracting a part of the string
Upper – converting all letters to capitals/upper case
Lower – converting all letters to simple letters / lowercase
*These methods are provided by library routines.
Cambridge - Computer Science Chapter 8
Arithmetic , logical and Boolean operators
Arithmetic
+ Addition
- Subtraction
* Multiplication
/ Division
^ Power
MOD Remainder
DIV Division-Quotient
Logical
> Greater than
< Less than
= Equal
>= Greater than/equal
<= Less than/equal
<> Not equal
Boolean
AND Both true
OR Either true
NOT Not true
Cambridge - Computer Science Chapter 8
Subroutines (Procedures and functions)
Instead of repeating statements and writing new code every time they are
required, it is essential to make use of subroutines.
They are procedures and functions.
Procedure is a set of programming statements grouped together under a single
name that can be called to perform a task at any point in a program.
Function is a set of programming statements grouped together under a single
name that can be called to perform a task at any point in a program.
Comparatively, a function will return a value to the main program (procedure
does not).
What are parameters?
They are variables that store the values of arguments passed to a procedure or
function.
*When procedures or functions are identified, the 1 st statement in the definition
is a header.
Types of variables
Global variables can be used by any part of a program. It’sTheir scope covers
the entire program.
Local variables can only be used by the part of the program they have been
declared in. Their scope is restricted to that part of the program.
Library Routines
Each programming language has many library routines for standard tasks that
are commonly required by programs.
Ex:- MOD,DIV,ROUND,RANDOM
Cambridge - Computer Science Chapter 8
*A maintainable program should
1) use meaningful identifier names for variables, constants, arrays ,
procedures and functions
2) be divided into modules for each task using procedures or functions.
3) use comments
An array is a data structure containing several elements of the same data
type, where it can be accessed by the same identifier name.
*One dimensional array – list
*Two dimensional array – table
What is the purpose of storing data in a file?
Data stored in RAM will be lost when the computer is switched off. Therefore ,
its important to keep the data permanently in files.
Cambridge - Computer Science Chapter 8