Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

OPTIMUM

INSTITUTE

IGCSE(EdExcel)

Chapter 18- Logic


&
Understanding Algorithm(Chapter 1)

U Nyein Oo
Founder
Optimum Institute
Optimum Institute of Business & Technology Studies
of Business & Technology Studies

Program Development Steps

Stages of program development process


 Defining the Problem
 Designing the Program(Developing Algorithm)
 Developing the program (Coding or Writing the program)
 Testing and Debugging

Optimum Institute of Business & Technology Studies 2

1
Program Development Steps(cont.)

Designing the Program(Developing Algorithm-Ref 181)


 Algorithm is a step-by-step procedure, which defines a set of instructions
to be executed in a certain order to get the desired output.
 Algorithms are generally created independent of underlying languages, i.e.
an algorithm can be implemented in more than one programming
language.

Optimum Institute of Business & Technology Studies 3

Program Development Steps(cont.)

Algorithm Tools:
– Flowchart
– Pseudo code
– Decision table
– Trace Table(Dry Run/Desk Checking)
– Structured Diagram..etc

Optimum Institute of Business & Technology Studies 4

2
Flowchart Symbols

Optimum Institute of Business & Technology Studies 5

Advantages of Pseudo Code

 English like language design


 No rules
 Very close to program code
 Easy to learn, quite to procedure
 It has block features(eg… BEGIN…END/START…STOP)
 It has JSP features (sequence, selection, iteration)
 Combination of action oriented verb and operand (variable)

Optimum Institute of Business & Technology Studies 6

3
Pseudo Keywords

• INPUT(ACCEPT) Input value from Console


• OUTPUT(DISPLAY) Output value to Console
• READ Input data from data storage
• WRITE Output data to data storage
• PRINT Output data to printer
• IF ….THEN…END IF One way Selection
• IF..THEN..ELSE..END IF Two Ways Selection
• IF…THEN..ELSE..IF…END IF Multi ways Selection
• FOR…END FOR For Loop(Iteration)
• WHILE…END WHILE While Loop(Iteration)…et

Optimum Institute of Business & Technology Studies 7

Operator

• Operator are mathematical symbol that perform some sort of operator


upon data. Operator acts upon alphanumeric, pure numeric or string
data.
– Assignment Operators( = )
– Arithmetic Operators ( + , - , 8, / , %)
– Compound Assignment Operators ( +=, -= …etc)
– Increment/Decrement Operators (++, -- ..etc)
– Relational or Comparison Operators ( >, <, ==, >= , <=, !=…etc)
– Logical Operators(AND, OR, NOT)

Optimum Institute of Business & Technology Studies 8

4
Logical operators

• Logical operators can use that combine results of several comparisons


to present a single answer.
• The logical operators can only result in either true or false values.

Optimum Institute of Business & Technology Studies 9

Logical operators(cont.)
Operator Meaning Example
AND or && Logical And x > 0 AND x < 10
x > 0 && x < 10

OR or || Logical Or x > 0 OR x < 10


x > 0 || x < 10

NOT or ! Logical Not NOT (x <10)


! (x <10)

Optimum Institute of Business & Technology Studies 10

5
Structured Programming
• Structured programming is a program written with only the structured
programming constructions:
– (1) sequence,
– (2) Selection,
– and (3) Iteration.

Optimum Institute of Business & Technology Studies 11

Sequence
• Sequence is the first programming structure.

• Sequences are the main logical structure of algorithms or programs.

Sequence Syntax
Statement 1
Statement 2
Statement 3
-
-
-
Statement n

Optimum Institute of Business & Technology Studies 12

6
Selection(Conditional Statement)
• Selection is a programming structure where a section of code is run only if a
condition is met.
• There are three sort of selection.
One way selection
Two ways selection
Multi ways selection

Optimum Institute of Business & Technology Studies 13

One way selection

One way selection Syntax Exercise Question


IF expression=True THEN
Display the “Result is Pass” message if
Statement (s)
given input number is at least 40
END IF

Optimum Institute of Business & Technology Studies 14

7
Sample Exercise for One way Selection

 IF age is ad least 18 THEN Display message is “ You are an adult “

Optimum Institute of Business & Technology Studies 15

Using IF with Logical Operator

AND = && (sub1>=40 AND …….sub6>=40)

OR = ||

NOT = !

Optimum Institute of Business & Technology Studies 16

8
Sample Exercise for Logical AND
– IF Marks of subject One >=40 and Marks of Subject Two >=40 then Display

Message is “ Result is Pass”

Optimum Institute of Business & Technology Studies 17

Sample Exercise for Logical OR


– IF Marks of subject One <40 or Marks of Subject Two <40 then Display

Message is “ Result is Fail ”

Optimum Institute of Business & Technology Studies 18

9
Sample Exercise for Logical NOT
– IF given gender type is not “Male” THEN Display message is “ Gender is

Female ”

Optimum Institute of Business & Technology Studies 19

Reference Page 181-182-183

One Way Selection

One Way Selection with


Logical AND

One Way Selection with


Logical OR

One Way Selection with


Logical NOT

Optimum Institute of Business & Technology Studies 20

10
Two way selection
• Based upon the value of the condition (Boolean T or F) choose what to
do next.
• There is another form of IF statement which is IF..THEN …ELSE
statement.
• IF...THEN …ELSE statement helps to reduce the complexity of a
program to some extent and also improves the readability.

Optimum Institute of Business & Technology Studies 21

IF Condition THEN
Statement 1
Statement 2 True branch
This is executed if then condition is true
------
Statement n
ELSE
Statement 1
False branch
Statement 2
This is executed if then condition is false
-------
Statement n
END IF

Optimum Institute of Business & Technology Studies 22

11
Pseudocode Example for Two way Selection

Exercise Question
Display the “Result is Pass” message if given
input mark is at least 40 else Display “ Result is
Fail” message

IF mark >= 40 THEN


DISPLAY “Result is Pass”
ELSE
DISPLAY “Result is Fail”
END IF

Optimum Institute of Business & Technology Studies 23

Pseudocode Example for Two way Selection with Logical AND

Exercise Question
Display the “Distinction ” message if given input
two subject marks is at least 80 else Display “
Regular Pass ” message

Optimum Institute of Business & Technology Studies 24

12
Pseudocode Example for Two way Selection with Logical OR

Exercise Question
Display the “Distinction ” message if given input
two subject marks is at least 80 else Display “
Regular Pass ” message

Optimum Institute of Business & Technology Studies 25

Thank You!

U Nyein Oo
Founder
Optimum Institute of Business & Technology Studies
Optimum Institute of Business & Technology Studies

13

You might also like