Logic 102 and 103

You might also like

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

ISTN 102/3

PROGRAMMING LOGIC

Dr Upasana G Singh
1
COMPILERS • « accueillir à la programmation » -
• A computer cannot understand
our language
Source code
(Programming
• 01010101 01001011 01011010
language: 01001110 any guesses?
human • We cannot understand binary
readable) language
• Instructions in a programming
Executable code language (e.g. JavaScript,
(Machine VBScript, Pascal, C++) which we
readable)
learn, then the compiler converts
it into structured language that
Programming Logic (2018) - Dr UG Singh
the computer can understand.
3
INTERPRETER • An interpreter interprets
instructions and converts them
to an intermediate form for
immediate execution.
• Main advantages over a compiler:
High level
instructions – It can be tailored to a specific
programming language
– It allows program
implementation to be
Intermediate independent of the
form characteristics of the host CPU
• The main disadvantage is speed

Programming Logic (2018) - Dr UG Singh


4
• Machine Language – The language
that a computer uses and
understands.
COMPUTER
LANGUAGES – E.g. 01010101 01001011 01011010
01001110
• Natural Language – The language
that humans understand and use for
communication.
– E.g. UKZN
• Programming Language – A
structured, precise, unambiguous
language that can be understood by
both the computer and humans.
– E.g. University: String (16)
Programming Logic (2018) - Dr UG Singh
5
• Describes what kind of data will
DATA TYPES be used (e.g., letters, numbers)
when writing a program.
• In some programming
languages, you don’t need to
specify the data type
• Only be studying the most
commonly adopted data types at
this level

Programming Logic (2018) - Dr UG Singh


6
Data Type Explanation and Examples
STRING • most widely used data types
• consists of one or more characters
• alphanumeric data
• E.g. print 'Hello World!‘
 address = '123 Central Avenue'
NUMBER • integer - numeric value without a decimal.
• whole numbers and can be positive or negative.
• E.g. result = 3 * 117.89 -------- 353

• decimal, a float or a double.


• E.g. result = 3 * 117.89 -------- 353.67
BOOLEAN • can only represent two values: true (1) or false (0)
• Eg. x = 8; y = 7; x>y
• the primary results of conditional statements
• to control workflow in program.
• E.g. if condition is7 true, then do this – else do that
Programming Logic (2018) - Dr UG Singh
PROGRAM/ • Program – set of instructions for solving a problem.
SOFTWARE • Software – another term for program
RELATED
TERMS – can consist of a group of programs:
• Systems Software – allows a person to take advantage of the
hardware capabilities of a computer. E.g. Windows
• Application Software – is a set of programs that can be used to
solve problems. E.g. MS Excel
• Syntax- spelling , grammar and punctuation of a
programming language. (VB lectures)
– Computers understand what you type only if you
type it in the exact form that the computer
expects - syntax.
– Syntactical rules unique to each programming
language.
• Pseudocode - depicts the logical steps that carry out
a task in the natural language (class examples)
Programming Logic (2018) - Dr UG Singh
8
CLASS • Write down the steps you would follow if you
EXAMPLE
wanted to go to the cafeteria from the classroom

Algorithm is a set of
steps that are
performed to solve a
problem. The example
describes an algorithm
Programming Logic (2018) - Dr UG Singh
9
THE
FLOWCHART
AND ITS
SYMBOLS

Example: Create a flowchart to add two numbers

Programming Logic (2018) - Dr UG Singh


10
FLOWCHART TO ADD TWO NUMBERS

Programming Logic (2018) - Dr UG Singh


11
• A program is a set of instructions that
enables a computer to perform some task
or tasks. These tasks could be :
PROGRAM
– adding and subtracting numbers,
– determining a company’s monthly
expenses,
– solving scientific theories or
– even playing video games.
• A process is usually performed to allow
you to create your program easily and
according to the specifications given to
you.
• program design does not perform the
tasks required???
Programming Logic (2018) - Dr UG Singh
13
SOLVING A
PROBLEM

In order to solve a problem

Understand the problem clearly

Gather the relevant information

Process the information

Arrive at the solution

14 Programming Logic (2018) - Dr UG Singh


THE PROCESS

• Define a problem - should be


broken into 3 processes:
– Input
– Output
– Process
• Outline the solution (pseudocode)
• Develop outline into algorithm or
flowchart
• Test algorithm or flowchart
• Code
• Document and maintain

Programming Logic (2018) - Dr UG Singh


15
Define the problem
Input Number, integer
Process Divide number by 2
Obtain Remainder
If remainder is zero
Output Even
Odd

Programming Logic (2018) - Dr UG Singh


16
THE PROCESS

• Define a problem - should be


broken into 3 processes:
– Input
– Output
– Process
• Outline the solution (pseudocode)
• Develop outline into algorithm or
flowchart
• Test algorithm or flowchart
• Code
• Document and maintain

Programming Logic (2018) - Dr UG Singh


17
Define the problem
Input Number, integer
Process Divide number by 2
Obtain Remainder
If remainder is zero
Output Even
Odd
Pseudocode is not actual code. It uses a standard set of words which
makes it resemble code

BEGIN
INPUT NUMBER
R = NUMBER/2
IF R = 0
THEN DISPLAY “NUMBER IS EVEN”
ELSE
DISPLAY “NUMBER IS ODD”
END

Programming Logic (2018) - Dr UG Singh


18
THE PROCESS

• Define a problem - should be


broken into 3 processes:
– Input
– Output
– Process
• Outline the solution (pseudocode)
• Develop outline into algorithm or
flowchart
• Test algorithm or flowchart
• Code
• Document and maintain

Programming Logic (2018) - Dr UG Singh


19
S TAR T

IN P U T n u m
FLOWCHART:
THE ‘IF’
CONSTRUCT
r = n u m M OD 2

No
r =0

Yes

D IS P L AY "N u m b e r i s E ve n "

Programming Logic (2018) - Dr UG Singh


S TOP
20
THE PROCESS

• Define a problem - should be


broken into 3 processes:
– Input
– Output
– Process
• Outline the solution (pseudocode)
• Develop outline into algorithm or
flowchart
• Test algorithm or flowchart
• Code
TRACE TABLES
• Document and maintain

Programming Logic (2018) - Dr UG Singh


21
Pseudocode is not actual code. It uses a standard set of
words which makes it resemble code

BEGIN
INPUT NUMBER
R = NUMBER/2
IF R = 0
THEN DISPLAY “NUMBER IS EVEN”
ELSE
DISPLAY “NUMBER IS ODD”
ENDIF
END

Programming Logic (2018) - Dr UG Singh


22
THE ‘IF-ELSE’
CONSTRUCT
S TA R T

IN P U T n u m

r = n um M O D 2

Yes No
r = 0

D IS P L AY "N u m b e r i s E ve n " D IS PL A Y " N u m b er is O d d "

S TOP

Programming Logic (2018) - Dr UG Singh


23
Programming Logic (2018) - Dr UG Singh
24
PROBLEM
• Customers, in our business, are
granted “GOLD STATUS” if they
have been our loyal customer for
at least 10 years and have
completed R 5 000 000 worth of
transactions.
– Define the problem
– Outline the solution (pseudocode)
– Develop the flowchart
– Test the flowchart

Programming Logic (2018) - Dr UG Singh


26
Define the problem
Input Years, Transactions
Process Years >= 10
Transactions >= 5 000 000
Output GOLD Status
Target not met

BEGIN
INPUT Years
INPUT Transactions
IF Years >= 10 AND Transactions >=5000000
DISPLAY “Classified as an Gold status customer”
ELSE
DISPLAY “Target not met”
END IF Multiple
END Criteria
AND/OR
Programming Logic (2018) - Dr UG Singh
27
START

INPUT Years

INPUT Transactions

Years >= 10
AND
YES NO
Transactions
> 5000000

DISPLAY “Classifiy for DISPLAY “Target not met”


GOLD status”

STOP

Programming Logic (2018) - Dr UG Singh


28
BEGIN
INPUT Years
INPUT Transactions
IF Years >= 10
IF Transactions >=5000000
DISPLAY “Classified for Gold status”
ELSE
DISPLAY “You have not reached your 5 000 000 target”
END IF
ELSE
DISPLAY “You have not reached your 10 year target”
END IF
END
NESTED
IFs
Programming Logic (2018) - Dr UG Singh
29
NESTED IFS

START

INPUT Years

INPUT Transactions

YES Years >= 10 NO

Transactions DISPLAY “You have not reached


> 5000000 NO your 10 year target”

YES DISPLAY “You have not


reached your 5 000 000 target”

DISPLAY “Classifiy for


GOLD status”

STOP
Programming Logic (2018) - Dr UG Singh
30
S TA R T
LOOPS

cn t=0

BEGIN No
cnt=0 cn t < 1 0 0 0

WHILE (cnt < 1000) Yes


DO
DISPLAY “Scooby” D IS PL A Y " Sc o ob y "

cnt=cnt+1
END DO cn t=c n t+1

END

S TOP

Programming Logic (2018) - Dr UG Singh


31
Programming Logic (2018) - Dr UG Singh
32

You might also like