7 and 8 Programming Concepts
7 and 8 Programming Concepts
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:
Structure Diagrams
Flowcharts
Pseudo codes
Program Code
FLOWCHARTS:
Flow chart is a graphical representation of a program.
Flowcharts use different symbols containing information about steps or a sequence of events.
(2210) Algorithm & Programming Basics
PSEUDOCODE:
Pseudo code is an outline of a program, written as a series of instruction using simple
English sentences. Pseudo code uses keywords commonly found in high-level languages and
mathematical notation.
Variable:
Variable is named memory location or data holder whose value can change during
running of a program. Variable must have a meaningful name and a Data Type
Variables must be DECLARED or created before they are used in the program.
Constants:
Just like variables, constants are "dataholders".
In contrast to variable, the content of a constant can't change at runtime, it has a
constant value. Constants are also declared with a value before they are used in the
program.
Operator Comparison
Addition
+
Subtraction
-
Multiplication
*
Division
/
Equals to
=
Not equal
<>
(2210) Algorithm & Programming Basics
Logical operations:
Operator Comparison
> Greater than
< Less than
>= Greater than equal to
<= Less than equal to
= Equals to
<> Not equal
() Group
AND And
OR Or
NOT Not
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
Initialization:
The value of variable can be initialized before user’s input 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.
Data types
A variable can store one type of data. The data types are:
Input
In Pseudocodes we indicate input by keywords such as INPUT, READ or ENTER,
followed by the name of a variable to which we wish to assign the input value.
Output:
In Pseudocodes 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:
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.
Pseudo code
BEGIN
DECLARE variable : Datatype
Variable 0 //initialization
Then ...
END
Constants
Creating Constants in Pseudocode is just writing costant name and value with it. In contrast to variable,
the content of a constant can't change at runtime, it has a constant value.
Type of Programs:
Sequence
Selection
Repetitions/Loops
Computer Science
(2210) Algorithm & Programming Basics 2210
Sequence
Statements are followed in sequence so the order of the statements in a program is
important.
Assignment statements rely on the variables used in the expression on the right-hand
side of the statement all having been given values. Input statements often provide
values for assignment statements. Output statements often use the results from
assignment statements.
VB code example PSEUDOCODE
BEGIN
DECLARE number1 : Integer
DECLARE number2 : Integer
DECLARE sum : Integer
DECLARE product : Integer
Flowchart Pseudocode
BEGIN
DECLARE miles,km : REAL
IF…THEN…ELSE…ENDIF
CASE…OF…OTHERWISE…ENDCASE
IF…THEN…ELSE…ENDIF
For an IF condition the THEN path is followed if the condition is true and the ELSE path
is followed if the condition is false.
There may or may not be an ELSE path. The end of the statement is shown by ENDIF.
IF ((Height > 1) OR (Weight > 20) OR (Age > 5)) AND (Age < 70)
THEN
PRINT ("You can ride")
ELSE
PRINT ("Too small, too young or too old")
ENDIF
CASE Choice OF
FLOWCHART:
START
INPUT
marks
Yes
OUTPUT
IF marks>50 (“Pass”)
No
OUTPUT
(“Fail”)
STOP
PSEUDOCODE VB Code
BEGIN
DECLARE marks : Integer
END
Computer Science
(2210) Algorithm & Programming Basics 2210
BEGIN
DECLARE marks : INTEGER
PRINT ("Enter marks")
INPUT marks
IF marks >= 80
THEN PRINT ("Grade A")
ELSE IF marks >= 60
THEN PRINT ("Grade B")
ELSE IF marks >= 60
THEN PRINT ("Grade C")
ELSE PRINT ("Grade U")
END IF
END IF
END IF
The IF statement is useful, but can get clumsy if you want to consider “multi-way
END
selections
FLOWCHART: START
INPUT
marks
Yes OUTPUT
IF marks >= 80
(“Grade A”)
No
Yes OUTPUT
IF marks >=
(“Grade B”)
70
No
Yes OUTPUT (“Grade C”)
IF marks >=60
No
OUTPUT (“Grade U”)
STOP
Computer Science
(2210) Algorithm & Programming Basics 2210
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
Computer Science
(2210) Algorithm & Programming Basics 2210
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
The fore loop repeats statements a set number of time. It uses a variable to count how many time it goes
round the loop and stops when it reaches its limit.
Computer Science
(2210) Algorithm & Programming Basics 2210
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
VB code example:
Sub Main(args As String())
Console.WriteLine("Times Table Program")
Dim count, num As Integer
Console.WriteLine("please Input a number for its TimesTable")
num = Console.ReadLine()
For count = 1 To 20
Console.WriteLine(num & " Times " & count & " = " & num * count)
Next
End Sub
OUTPUT of Code
Computer Science
(2210) Algorithm & Programming Basics 2210
INPUT BiggestSoFar
FOR Counter 1 TO 5
INPUT NextNumber
IF NextNumber > BiggestSoFar
THEN
BiggestSoFar NextNumber
ENDIF
END FOR
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
VB Code example
BEGIN
DECLARE name : String
INPUT name
END
VB code example
BEGIN
DECLARE name : String
REPEAT
INPUT name
PRINT (“Your name is:” name)
UNTIL name = "x"
END
Keeps inputting name and keeps printing name until user enters “X”
Computer Science
(2210) Algorithm & Programming Basics 2210
FLOWCHART…WHILE-ENDWHILE
START
marks
(“Grade A”)
(“ ”)
INPUT name (“ ”)
LOOP
(“ ”)
(“ ”)
(“
No
STOP
marks
(“Grade A”)
FLOWCHART…REPEAT-UNTIL
(“ ”)
(“ ”)
(“ ”)
(“ ”) START
(“
marks
(“Grade A”)
(“ ”)
INPUT name (“ ”)
LOOP
(“ ”)
(“ ”)
is”)
OUTPUT (name)
UNTIL
name= “x” No
Yes
STOP
marks
(“ ”)
(“ ”)
Computer Science
(2210) Algorithm & Programming Basics 2210
Array Data Type
An array is a special variable that has one name, but can store multiple values. Each value is stored in an
element pointed to by an index.
The first element in the array has index value 0, the second has index 1, etc
• What the array is going to be used for, so it can be given a meaningful name
• How many items are going to be stored, so the size of the array can be determined.
• What sort of data is to be stored, so that the array can be the appropriate data type.
PRINT (names(1))
BEGIN
DECLARE table(3, 4) : Integer
FOR row = 1 To 3
FOR column = 1 To 4
PRINT("Please Input Value in Row: ",row, "column : ", column)
INPUT table(row, column)
NEXT
NEXT
FOR row = 1 To 3
FOR column = 1 To 4
PRINT ("Row = " & row & "column = " & column & “has Value”)
PRINT (table(row, column))
NEXT
NEXT
END
Sub Main()
Dim table(2, 3) As Integer
For row = 0 To 2
For column = 0 To 3
Console.WriteLine("Please Input Value in Row: " & row & "column : " & column)
table(row, column) = Console.ReadLine()
Next
Next
Console.Clear()
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