9.2 - 11.2 Algorithm & Programming Basics (VB,
9.2 - 11.2 Algorithm & Programming Basics (VB,
Syllabus Content:
9.2. Algorithms
show understanding that an algorithm is a solution to a problem expressed as a sequence of
defined steps
use suitable identifier names for the representation of data used by a problem
o summarise identifier names using an identifier table
show understanding that many algorithms are expressed using the four basic constructs of
assignment, sequence, selection and repetition
show understanding that simple algorithms consist of input, process, output at various
stages
document a simple algorithm using:
o Structured English
o pseudocode (on the examination paper, any given pseudocode will be presented
using the Courier New font)
o program flowchart
derive pseudocode or a program flowchart from a structured English description of a
problem
derive pseudocode from a given program flowchart or vice versa
use the process of stepwise refinement to express an algorithm to a level of detail from
which the task may be programmed
decompose a problem into sub-tasks leading to the concept of a program module
(procedure/ function)
show an appreciation of why logic statements are used to define parts of an algorithm
solution
use logic statements to define parts of an algorithm solution
10.2 Arrays
use the technical terms associated with arrays including upper and lower bound
select a suitable data structure (1D or 2D array) to use for a given task
use pseudocode for 1D and 2D arrays (pseudocode will use square brackets to contain the
array subscript, for example a 1D array as A[1:n] and a 2D array as C[1:m, 1:n])
write program code using 1D and 2D arrays
write algorithms/program code to process array data including:
Syllabus Content:
11.1 Programming basics
Implement and write pseudocode from a given design presented as either a program
flowchart or structured English
Notes and guidance
Write pseudocode statements for implement and write a program from a given design
presented as either a program flowchart or pseudocode
write pseudocode for:
o the declaration of variables and constants
o the assignment of values to variables and constants
o expressions involving any of the arithmetic or logical operators
o input from the keyboard and output to the console given pseudocode will use the
following structures:
o DECLARE <identifier> : <data type> // declaration
o CONSTANT <identifier> = <value>
o <identifier> ← <value> or <expression> // assignment
o INPUT <identifier>
o OUTPUT <string> , OUTPUT <identifier(s)>
11.2 Constructs
Write pseudocode to write:
Selection
use an ‘IF’ structure including the ‘ELSE’ clause and nested IF statements
use a ‘CASE’ structure
Iteration
use a ‘count-controlled’ loop:
o FOR <identifier> ← <value1> TO <value2> <statement(s)> ENDFOR
o alternatively: FOR <identifier> ← <value1> TO <value2> STEP <value3>
<statement(s)> ENDFOR
use a ‘post-condition’ loop:
o REPEAT <statement(s)> UNTIL <condition>
use a ‘pre-condition’ loop
o WHILE <condition> <statement(s)> ENDWHILE • justify why one loop structure
may be better suited to a problem than the others
9.2 Algorithms:
An algorithm is a sequence of steps done to perform some task.
The essential aim of an algorithm is to get a specific output,
An algorithm involves with several continuous steps,
The output comes after the algorithm finished the whole process.
So basically, all algorithms perform logically while following the steps to get an output for a
given input.
Types of Algorithms:
Structured English
Flowcharts
Pseudo codes
Program Code
STRUCTURED ENGLISH:
Structured English provides a more formal way of documenting the stages of the algorithm.
Structured English is a subset of English language that consists of command statements used
to describe an algorithm.
FLOWCHARTS:
Flow chart is a graphical representation of a program.
Flowcharts use different symbols containing information about steps or a sequence of events.
Variable:
Variable is a named memory location with DataType where value can be stored.
The content of a variable can change at runtime
Constants:
Just like variables, constants are "dataholders". They can be used to store data that is
needed at runtime.
In contrast to variable, the content of a constant can't change at runtime, it has a
constant value.
Before the program can be executed (or compiled) the value for a constant must be
known.
Arithmetic
Use the arithmetic operators.
Assignment
Assignment is the process of writing a value into a variable (a named memory location).
For example, Count ← 1 can be read as ‘Count is assigned the value 1’, ‘Count is
made equal to 1’ or ‘Count becomes 1’.
Initialization:
If an algorithm needs to read the value of a variable before it assigns input data or a
calculated value to the variable, the algorithm should assign an appropriate initial value
to the variable, known as Initialization.
Input
We indicate input by words such as INPUT, READ or ENTER, followed by the name of
a variable to which we wish to assign the input value.
Output:
We indicate output by words such as OUTPUT, WRITE or PRINT, followed by a
comma-separated list of expressions.
Totaling
To keep a running total, we can use a variable such as Total or Sum to hold the running
total and assignment statements such as:
Counting
It is sometimes necessary to count how many times something happens.To count up or
increment by 1, we can use statements such as:
Count ← Count + 1
INCREMENT Count by 1
Structured statements
In the sequence structure the processing steps are carried out one after the other. The
instructions are carried out in sequence, unless a selection or loop is encountered.
- - - Subtraction
* * * Multiplication
/ / / Division
= = == Equal
The following table shows the Visual Basic data types, their supporting common
language runtime types, their nominal storage allocation, and their value ranges.
BEGIN
DECLARE variable : Datatype
Variable 0 //initialization
OUTPUT (“What is your Email address”)
INPUT variable value\
IF valid email address?
Then ...
END IF
End
www.majidtahir.com WhatsApp +923004003666 Enroll in Online classes at www.fiqar.org 7
9.2, 10.2, 11.1 to 11.2 Algorithm & Programming Basics CS 9618 with Majid Tahir
1 at www.majidtahir.com
VB code PYTHON
Sub main() num1=int(input("enter number1"))
Dim Num1 As Integer num2=int(input("enter number2"))
Dim Num2 As Integer total = num1+num2
Dim Sum As Integer prod = num1*num2
Dim Procduct As Integer print("Total is", total)
Console.Writeline(“Enter number1”) print("Product is", prod)
Num1 = Console.Readline()
Console.Writeline(“Enter number2”)
Num2 = Console.Readline()
Sum = Num1+Num2
Product = Num1*Num2
Console.Writeline(“Sum is” & sum)
Console.Writeline(“Product is” & Product)
End Sub
Pseudocode VB Code
Pseudocode: PYTHON:
BEGIN miles = float(input("enter miles"))
DECLARE miles,km : REAL km = miles*1.61
OUTPUT (“Enter miles”) print("Kilometers are:", km)
INPUT miles
km miles * 1.61
OUTPUT(“Km are : ” & km)
END
PSEUDOCODE FLOWCHART:
BEGIN
START
DECLARE marks : Integer
PRINT ("Enter your grade")
INPUT marks
IF marks > 50 INPUT
THEN marks
PRINT ("You have passed")
ELSE
PRINT (“You’ve failed”) Yes OUTPUT
END IF IF marks>50 (“Pass”)
END No
OUTPUT
(“Fail”)
STOP
VB Code
PYTHON Code:
Sub main()
marks = int(input(" Enter your marks ")) Dim marks As Integer
if marks>=50:
Console.Writeline(“Enter marks”)
print("Pass")
else: print("Fail") Marks = Console.Readline()
If marks >= 50 Then
Console.Writeline(“pass”)
Else
Console.Writeline(“fail”)
End If
End Sub
FLOWCHART:
START
INPUT
marks
Yes
OUTPUT
IF marks >=
(“Grade A”)
No
Yes OUTPUT
IF marks >= 60
(“Grade B”)
No
No Yes
STOP
The IF statement is useful, but can get clumsy if you want to consider “multi-way
selections
INPUT num1,
num2, Opvalue
Yes
Ans = num1+num2
Opvalue = “+”
No Yes
Opvalue = “-” Ans = num1-num2
No
Yes Ans = num1 * num2
Opvalue = “ * ”
No
Opvalue = “/” Ans = num1 / num2
No Yes
STOP
START
Pseudo code
INPUT marks
BEGIN
DECLARE marks : Integer
OUTPUT
PRINT ("Enter your marks") marks>=80? Yes (“Grade A”)
INPUT marks
CASE OF marks no
80 >= :PRINT("Grade A") marks>=70? Yes OUTPUT
70 >= :PRINT("Grade B") (“Grade B”)
60 >= :PRINT("Grade C") no
60 >= :PRINT("Grade D") OUTPUT
40 >= :PRINT ("Grade E") marks>=60? Yes (“Grade C”)
OTHERWISE
PRINT("Grade U, Repeat Exam") no
marks>=50? OUTPUT
END CASE Yes (“Grade D”)
END no
yes
OUTPUT
marks>=40? (“Grade D”)
no
OUTPUT
(“Grade U)
STOP
Exit conditions consist of logical expressions whose truth can be tested, such as
Count = 10 or Score < 0.
FOR count = 1 to 10
INPUT number
total = total + number
NEXT count
WHILE … Do LOOP
This loop is used when we don’t know how many times the loop is to be performed. The Loop is
ended when a certain condition is true.
This condition is checked before starting the loop.
REPEAT
Input NUMBER
TOTAL = TOTAL + NUMBER
COUNT = COUNT + 1
Until COUNT = 10
Output Total
It uses a variable to count how many time it goes round the loop and stops when it reaches its limit.
BEGIN
DECLARE count, number : Integer
OUTPUT (“Input a number for its times table")
INPUT number
FOR count = 1 To 20
PRINT (number , “times" , count , “ = ” number * Count”)
NEXT
Output PYTHON
The wile loop is known as a test before loop. The condition is tested before entering the loop, but tested
each time it goes round the loop. The number of times the statements within the loop are executed
varies. The test before loop goes round 0 or more times.
This method is useful when processing files and using “read ahead” data
The repeat loop is similar to the while loop, but it tests the condition after the statements have been
executed once. This means that this test after loop goes round 1 or more times.
VB Code PSEUDOCODE
Sub main()
BEGIN
DECLARE name : String
Dim marks As Integer
REPEAT
Do
PRINT (“Enter marks 0 to 100”)
Console.Writeline(“Enter marks 0 to100”)
INPUT marks
Marks = Console.Readline() UNTIL marks>=0 AND marks <=100
Loop Until marks>=100 AND <=100
if marks >= 50 Then IF marks>=50
Console.Writeline(“ Pass ”) THEN
Else OUTPUT(“Pass”)
Console.Writeline(“ Fail ”) ELSE
End If OUTPUT(“Fail”)
End Sub END IF
END
PYTHON Does not have REPEAT LOOP so WHILE Loop is used.
FLOWCHART…WHILE-ENDWHILE
START
marks
(“Grade A”)
INPUT marks (“ ”)
(“ ”)
(“ ”)
LOOP
(“ ”)
(“
WHILE
marks >100 OR < 0 OUTPUT
(“ ERROR, Re Input”)
Yes
No
STOP
marks
(“Grade A”)
(“ ”)
9.2, 10.2, 11.1 to 11.2 Algorithm & Programming Basics CS 9618 with Majid Tahir
1 at www.majidtahir.com
FLOWCHART…REPEAT-UNTIL
START
marks
(“Grade A”)
(“ ”)
LOOP (“ ”)
(“ ”)
(“ ”)
(“
UNTIL
INPUT marks marks>=0 AND <=100
Yes
STOP
marks
(“ ”)
(“ ”)
An array is a special variable that has one name, but can store multiple values. Each value is stored in an
(“ ”)
(“
Index Name
1 Fred
2 James
3 Tom
4 Robert
5 Jonah
6 Chris
7 John
8 Matthew
9 Mikey
10 Jack
PYTHON Code:
name = []
marks = []
for count in range(5):
name.append (str(input("Enter name: ")))
marks.append (int(input("Enter marks ")))
print("Name ", name, " scored ", marks)
OUTPUT screen
Python Code
row, col = 3, 4
table = [[0 for x in range(row)] for y in range(col)]
# Creates a list containing 5 lists, each of 8 items, all set to 0
For row = 0 To 2
For column = 0 To 3
Console.WriteLine("Row = " & row & "column = " & column & “has Value”)
Console.WriteLine(matrix(row, column))
Next
Next
Console.ReadKey()
End Sub
Multi-Dimensional Arrays:
A multi-dimensional array can be thought of as a table, each element has a row and
column index.
Following example declares a two-dimensional array called matrix and would be
declared by
Refrences:
Computer Science by David Watson & Helen Williams
Visual Basic Console Cook Book
Computer Science AS and A level by Sylvia Langfield and Dave Duddell
https://www.sitesbay.com/javascript/javascript-looping-statement
http://wiki.jikexueyuan.com/project/lua/if-else-if-statement.html