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

Programming (3)

Uploaded by

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

Programming (3)

Uploaded by

Supun Thenuwara
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Programming

By Mr. Supun Chamara (Bsc Hons, Teacher, Former Lecturer)


Variables and Constants

● A variable in a computer program is a named data store than contains a value that
may change during the execution of a program.
● A constant in a computer program is a named data store than contains a value that
does not change during the execution of a program.
Data Types
Input form the Keyboard and Output to the Display
Input and Output form the Keyboard
Arithmetic Operators
Selection
● Selecting a block of code to execute Start

Yes Output “You


age> 17?
are Adult”

No

End
Selection
● If and else Start

Yes Output “You


age> 17?
are Adult”

No

Output “You
are a Child”

End
Selection Start

Output “Enter Marks”

● If else if
Input Mark1, Mark2, Mark3

Mark1>Maek2 Yes Output


and
mark1>mark3 ? Mark1

No

Mark2>Mark1
Yes
and Output
mark2>mark3 ? Mark2

No

Mark3>Maek1 Yes Output


and
mark3>mark2 ? Mark3

No

End
Selection Start

Output “Enter Marks”

● If elif (case statement)


Input Mark1, Mark2, Mark3

Mark1>Maek2 Yes Output


and
mark1>mark3 ? Mark1

No

Mark2>Mark1
Yes
and Output
mark2>mark3 ? Mark2

No

Mark3>Maek1 Yes Output


and
mark3>mark2 ? Mark3

No

End
Selection
● Nested If
Iteration(loops)
● Type of iterations
○ Count-controlled loops (for a set number of iterations)
○ Pre-condition loops – may have no iterations
○ Post-condition loops – always has at least one iteration.
Count-controlled loops
● Used when the number of iterations are known.

# iterate from i = 0 to i = 3
for i in range(4):
print(i) language = 'Python'

# iterate over each character in language


for x in language:
print(x)
Condition-controlled loops
When the number of iterations is not known and iterate until the condition get fales.

number = 1

while number <= 3:


print(number)
number = number + 1
Totalling and Counting
Procedures, functions and parameters
● A 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.
● A 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. In contrast to a
procedure, a function will return a value back to the main program.
● Parameters are the variables that store the values of the arguments passed to a
procedure or function. Some but not all procedures and functions will have
parameters.
Procedures without parameters
● Here is an example of a procedure without parameters in pseudocode:
Procedures with parameters
It is often useful to pass a value to a procedure that can be used to modify the
action(s) taken. For example, to decide how many stars would be output. This is
done by passing an argument when the procedure is called to be used as a
parameter by the procedure.
DECLARE Counter : INTEGER
FOR Counter FROM 1 TO NUMBER
SEND "*" TO DISPLAY
NEXT Counter
ENDPROCEDURE
Functions
A function is just like a procedure except it always returns a value. Just like a
procedure it is defined once and can be called many times within a program. Just
like a procedure it can be defined with or without parameters.

SET MyTemp to Celsius(MyTemp)


Functions and Procedures
When procedures and functions are defined, the first statement in the definition
is a header, which contains:
» the name of the procedure or function
» any parameters passed to the procedure or function, and their data type
» the data type of the return value for a function.
Local and global variables
● A global variable can be used by 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 – its scope is restricted to that part of the program.
DECLARE Number1, Number2, Answer : INTEGER DECLARE Number1, Number2, Answer : INTEGER
PROCEDURE Test PROCEDURE Test
DECLARE Number3, Answer : INTEGER DECLARE Number3, Answer : INTEGER
Number1 ← 10 SET Number1 TO 10
Number2 ← 20 SET Number2 TO 20
Number3 ← 30 SET Number3 TO 30
Answer ← Number1 + Number2 SET Answer TO Number1 + Number2
OUTPUT "Number1 is now ", Number1 SEND "Number1 is now ", Number1 TO DISPLAY
OUTPUT "Number2 is now ", Number2 SEND "Number2 is now ", Number2 TO DISPLAY
OUTPUT "Answer is now ", Answer SEND "Answer is now ", Answer TO DISPLAY
ENDPROCEDURE ENDPROCEDURE

Number1 ← 50 SET Number1 TO 50


Number2 ← 100 SET Number2 TO 100
Answer ← Number1 + Number2 SET Answer TO Number1 + Number2
OUTPUT "Number1 is ", Number1 SEND "Number1 is ", Number1 TO DISPLAY
OUTPUT "Number2 is ", Number2 SEND "Number2 is ", Number2 TO DISPLAY
OUTPUT "Answer is ", Answer SEND "Answer is ", Answer TO DISPLAY
CALL Test CALL Test
OUTPUT "Number1 is still ", Number1 SEND "Number1 is still ", Number1 TO DISPLAY
OUTPUT "Number2 is still ", Number2 SEND "Number2 is still ", Number2 TO DISPLAY
OUTPUT "Answer is still ", Answer SEND "Answer is still ", Answer TO DISPLAY
OUTPUT "Number3 is ", Number3 SEND "Number3 is ", Number3 TO DISPLAY
Library routines
● Many programming language include library routines(predefined functions and
procedures) that are ready to use in our program.
● These routines are fully tested and ready for use.
● A programming language usually includes a standard library of functions and
procedures. e.g. string handling, inpu/output

Value1 =MOD(10,3) returns the remainder of 10 divided by 3


Value2 =DIV(10,3) returns the quotient of 10 divided by 3
Value3 =ROUND(6.97354, 2) returns the value rounded to 2 decimal places
Value4 =RANDOM() returns a random number between 0 and 1 inclusive
Arithmetic operators
Logical operators

All programming languages make use of logical operators to decide which path
to take through a program.
Logical operators

All programming languages make use of Boolean operators to decide whether an


entire expression is true or false.
Declaring a Variable in Python
DECLARE ClassAverage, StudentAverage, SubjectAverage : REAL
DECLARE Student, Subject, Test : INTEGER
DECLARE Name, Class : STRING
DECLARE GENDER: BOOLEAN

ClassAverage=0.0
StudentAverage=0.0
SubjectAverage=0.0
Student=0
Subject=0
Test=0
Name=””
Class=””
GENDER=False
String Handling
● Strings are used to store text. Every string contains a number of characters,
from an empty string, which has no characters stored, to a maximum number
specified by the programming language.
● The characters in a string can be labelled by position number. The first
character in a string can be in position zero or position one, depending on the
language.
String Handling
Creating a maintainable program
» always use meaningful identifier names for:
– variables
– constants
– arrays
– procedures
– functions
» be divided into modules for each task using:
– procedures
– functions
» be fully commented using your programming language’s commenting feature.
Use of nested statements(Cambridge)
NEXT Test
// Declarations of the variables needed SubjectAverage ← SubjectTotal / NumberOfTests
DECLARE ClassAverage, StudentAverage, SubjectAverage : REAL OUTPUT "Average mark for Subject ", Subject, " is ",
DECLARE Student, Subject, Test : INTEGER SubjectAverage
DECLARE ClassHigh, ClassLow, ClassTotal : INTEGER OUTPUT "Highest mark for Subject ", Subject, " is ", SubjectHigh
DECLARE StudentHigh, StudentLow, StudentTotal : INTEGER OUTPUT "Lowest mark for Subject ", Subject, " is ", SubjectLow
DECLARE SubjectHigh, SubjectLow, SubjectTotal : INTEGER IF SubjectHigh > StudentHigh
ClassHigh ← 0 THEN
ClassLow ← 100 StudentHigh ← SubjectHigh
ClassTotal ← 0 ENDIF
// Use of constants enables you to easily change the values for testing IF SubjectLow < StudentLow
CONSTANT NumberOfTests = 5 THEN
CONSTANT NumberOfSubjects = 6 StudentLow ← SubjectLow
CONSTANT ClassSize = 20 ENDIF
FOR Student ← 1 TO ClassSize StudentTotal ← StudentTotal + SubjectTotal
StudentHigh ← 0 NEXT Subject
StudentLow ← 100 StudentAverage ← StudentTotal / (NumberOfTests * NumberOfSubjects
StudentTotal ← 0 OUTPUT "Average mark for Student ", Student, " is ", StudentAverage
FOR Subject ← 1 TO NumberOfSubjects OUTPUT "Highest mark for Student ", Student, " is ", StudentHigh
SubjectHigh ← 0 OUTPUT "Lowest mark for Student ", Student, " is ", StudentLow
SubjectLow ← 100 IF StudentHigh > ClassHigh
SubjectTotal ← 0 THEN
FOR Test ← 1 TO NumberOfTests ClassHigh = StudentHigh
OUTPUT "Please enter mark " ENDIF
INPUT Mark IF StudentLow < ClassLow
IF Mark > SubjectHigh THEN
THEN ClassLow ← StudentLow
SubjectHigh ← Mark ENDIF
ENDIF ClassTotal ← ClassTotal + StudentTotal
IF Mark < SubjectLow NEXT Student
THEN ClassAverage ← ClassTotal / (NumberOfTests * NumberOfSubjects * ClassSize)
SubjectLow ← Mark OUTPUT "Average mark for Class is ", ClassAverage
ENDIF OUTPUT "Highest mark for Class is ", ClassHigh
SubjectTotal ← SubjectTotal + Mark OUTPUT "Lowest mark for Class is ", ClassLow
NEXT Test
Use of nested statements(edexcel)
NEXT Test
// Declarations of the variables needed SET SubjectAverage TO SubjectTotal / NumberOfTests
DECLARE ClassAverage, StudentAverage, SubjectAverage : REAL SEND "Average mark for Subject ", Subject, " is ", SubjectAverage TO DISPLAY
DECLARE Student, Subject, Test : INTEGER SEND "Highest mark for Subject ", Subject, " is ", SubjectHigh TO DISPLAY
DECLARE ClassHigh, ClassLow, ClassTotal : INTEGER SEND "Lowest mark for Subject ", Subject, " is ", SubjectLow TO DISPLAY
DECLARE StudentHigh, StudentLow, StudentTotal : INTEGER IF SubjectHigh > StudentHigh
DECLARE SubjectHigh, SubjectLow, SubjectTotal : INTEGER THEN
SET ClassHigh TO 0 SET StudentHigh TO SubjectHigh
SET ClassLow TO 100 ENDIF
SET ClassTotal TO 0 IF SubjectLow < StudentLow
// Use of constants enables you to easily change the values for testing THEN
SET CONSTANT NumberOfTests TO 5 SET StudentLow TO SubjectLow
SET CONSTANT NumberOfSubjects TO 6 ENDIF
SET CONSTANT ClassSize TO 20 SET StudentTotal TO StudentTotal + SubjectTotal
FOR Student 1 TO ClassSize NEXT Subject
SET StudentHigh TO 0 SET StudentAverage TO StudentTotal / (NumberOfTests * NumberOfSubjects
SET StudentLow TO 100 SEND "Average mark for Student ", Student, " is ", StudentAverage TO DISPLAY
SET StudentTotal TO 0 SEND "Highest mark for Student ", Student, " is ", StudentHigh TO DISPLAY
FOR Subject 1 TO NumberOfSubjects SEND "Lowest mark for Student ", Student, " is ", StudentLow TO DISPLAY
SET SubjectHigh TO 0 IF StudentHigh > ClassHigh
SET SubjectLow TO 100 THEN
SET SubjectTotal TO 0 SERT ClassHigh TO StudentHigh
FOR Test 1 TO NumberOfTests ENDIF
SEND "Please enter mark " TO DISPLAY IF StudentLow < ClassLow
RECEIVE Mark FROM KEYBOARD THEN
IF Mark > SubjectHigh SET ClassLow TO StudentLow
THEN ENDIF
SET SubjectHigh TO Mark SET ClassTotal TO ClassTotal + StudentTotal
ENDIF NEXT Student
IF Mark < SubjectLow SET ClassAverage TO ClassTotal / (NumberOfTests * NumberOfSubjects * ClassSize)
THEN SEND "Average mark for Class is ", ClassAverage TO DISPLAY
SET SubjectLow TO Mark SEND "Highest mark for Class is ", ClassHigh TO DISPLAY
ENDIF SEND "Lowest mark for Class is ", ClassLow TO DISPLAY
SET SubjectTotal TO SubjectTotal + Mark
NEXT Test
Arrays
● An array is a data structure containing several elements of the same data type.
● these elements can be accessed using the same identifier name.
● The position of each element in an array is identified using the array’s index.
One-dimensional arrays
● A one-dimensional array can be referred to as a list. Here is an example of a list with 10
elements in it where the first element has an index of zero.
● When a one-dimensional array is declared in pseudocode:
■ the name of the array
■ the first index value
■ the last index value
■ and the data type
○ DECLARE MyList : ARRAY[0:9] OF INTEGER
■ we can add the number 27 to the fourth position in the array MyList as follows:
○ MyList[4] ← 27 or MyList[4] = 27
■ display the data that lies in a particular location in an array
○ OUTPUT MyList[1]
OUTPUT "Enter these 10 values in order 27, 19, 36, 42, 16, 89, 21, 16, 55, 72"
FOR Counter ← 0 TO 9
OUTPUT "Enter next value " SEND "Enter these 10 values in order 27, 19, 36, 42, 16, 89, 21, 16, 55, 72" TO DISPLAY
INPUT MyList[Counter] FOR Counter is 0 TO 9
NEXT Counter SEND "Enter next value " TO DISPLAY
RECEIVE MyList[Counter] FROM KEYBOARD
NEXT Counter
Two-dimensional arrays
● A two-dimensional array can be referred to as a table, with rows and columns.
● Here is an example of a table with 10 rows and 3 columns, which contains 30 elements.
● The first element is located at position 0,0.
● When a two-dimensional array is declared in pseudocode:
» the first index value for rows
» the last index value for rows
» the first index value for columns
» the last index value for columns
» and the data type
● DECLARE MyTable : ARRAY[0:9,0:2] OF INTEGER
Two-dimensional arrays
● this time there need to be two nested loops
OUTPUT "Enter these values in order 27, 19, 36, 42, 16, 89, 21, 16, 55, 34"
OUTPUT "Enter these values in order 31, 67, 98, 22, 35, 46, 71, 23, 11, 76"
OUTPUT "Enter these values in order 17, 48, 29, 95, 61, 47, 28, 13, 77, 21"
FOR ColumnCounter ← 0 TO 2
FOR RowCounter ← 0 TO 9
OUTPUT "Enter next value "
INPUT MyTable[RowCounter, ColumnCounter]
NEXT RowCounter
NEXT ColumnCounter

SEND "Enter these values in order 27, 19, 36, 42, 16, 89, 21, 16, 55, 34" TO DISPLAY
SEND "Enter these values in order 31, 67, 98, 22, 35, 46, 71, 23, 11, 76" TO DISPLAY
SEND "Enter these values in order 17, 48, 29, 95, 61, 47, 28, 13, 77, 21" TO DISPLAY
FOR ColumnCounter FROM 0 TO 2
FOR RowCounter FROM 0 TO 9
SEND "Enter next value " TO DISPLAY
RETRIEVE MyTable[RowCounter, ColumnCounter] FROM KEYBOARD
NEXT RowCounter
NEXT ColumnCounter

● display the data that lies in a particular location in a Two-dimensional array


OUTPUT MyList[2,1] OR SEND MyList[2,1] TO DISPLAY
File Handling
● when data is saved to a file it is stored permanently.
● Data stored in a file can thus be accessed by the same program at a later date or accessed by another program.
● Data stored in a file can also be sent to be used on other computer(s).
● The storage of data in files is one of the most used features of programming.
● Every file is identified by its filename.
● Here are examples of writing a line of text to a file and reading the line of text back from the file.
DECLARE TextLine : STRING // variables are declared as normal DECLARE TextLine : STRING // variables are declared as normal
DECLARE MyFile : STRING DECLARE MyFile : STRING
// writing the line of text to the file // writing the line of text to the file
MyFile ← "MyText.txt" SET MyFile TO "MyText.txt"
OPEN MyFile FOR WRITE // opens file for writing OPEN MyFile FOR WRITE // opens file for writing
OUTPUT "Please enter a line of text" SEND "Please enter a line of text" TO DISPLAY
INPUT TextLine INPUT TextLine
WRITEFILE, TextLine // writes a line of text to the file WRITEFILE, TextLine // writes a line of text to the file
CLOSEFILE(MyFile) // closes the file CLOSEFILE(MyFile) // closes the file
// reading the line of text from the file // reading the line of text from the file
OUTPUT "The file contains this line of text:" SEND "The file contains this line of text:" TO DISPLAY
OPEN MyFile FOR READ // opens file for reading OPEN MyFile FOR READ // opens file for reading
READFILE, TextLine // reads a line of text from the file READFILE, TextLine // reads a line of text from the file
OUTPUT TextLine SEND TextLine TO DISPLAY
CLOSEFILE(MyFile) // closes the file CLOSEFILE(MyFile) // closes the file
File Handling

You might also like