IPRG5111_Lecture 3S_2024
IPRG5111_Lecture 3S_2024
png)
HMAW – IPRG5111
Introduction to Programming Logic
Lecture 3
Lecturer: Brendan van der Merwe Email times: 8am – 5pm,
Email: [email protected] Monday to Friday
Objectives
In your first Lecture, you will learn about:
• Computer systems
• Simple program logic Lecture 1
• The steps involved in the program development cycle
• Pseudocode statements and flowchart symbols
• Draw flowcharts and pseudocode that demonstrate the solution to a
problem
Chapter 2:
• Apply the rules for selecting and declaring variable or constant names Lecture 2
• Write mathematical expressions in the required format for a program
• Working with Operators
• Functions
Lecture 3
• Comments in Programming
Understanding Simple Program Logic
• Programs with syntax errors cannot execute
• Logical errors
– Errors in program logic produce incorrect output
• Flowchart
– Pictorial representation of the logical steps it takes to solve
a problem
Drawing Flowcharts
Pseudocode Standards
• Programs begin with the word start and end with the
word stop; these two words are always aligned
• Whenever a module name is used, it is followed by a
set of parentheses ()
• Modules begin with the module name and end with
return. The module name and return are always
aligned
• Each program statement performs one action—for
example, input, processing, or output
Pseudocode Standards
• Program statements are indented a few spaces more
than the word start or the module name
• Each program statement appears on a single line if
possible. When this is not possible, continuation lines
are indented
• Program statements begin with lowercase letters
• No punctuation is used to end statements
start
input myNumber
set myAnswer = myNumber * 2
output myAnswer
stop
Drawing Flowcharts
Drawing Flowcharts
Drawing Flowcharts
Drawing Flowcharts
Drawing Flowcharts
Summary
• Hardware and software accomplish input, processing,
and output
• Logic must be developed correctly
• Logical errors are much more difficult to locate than
syntax errors
• Use flowcharts and pseudocode to plan the logic
Exercise 1
1. Draw a flowchart and write pseudocode to represent the logic of a
program that allows the user to enter a value. The program divides
the value by 2 and outputs the result.
start
output “Please enter in a number“
input myNumber
set myAnswer = myNumber / 2
output myAnswer
stop
Exercise 2
2. Draw a flowchart and write pseudocode to represent the logic of a
program that allows the user to enter two values. The program outputs
the product of the two values.
Exercise
3. Draw a flowchart or
3 (You do)
& write pseudocode to represent the logic of a program
that allows the user to enter a value for hours worked in a day. The program
calculates the hours worked in a five-day week and the hours worked in a 252-
day work year. The program outputs all the results.
Pseudocode
start
input hoursWorkedDay
set hoursWorkedWeek = hoursWorkedDay * 5
set hoursWorkedYear = hoursWorkedDay * 25
output hoursWorkedWeek
output hoursWorkedYear
stop
Exercise 4 (You do)
&
Chapter 2
Programming Basics
What is a variable?
• Variable names:
– Must be one word
– Must start with a letter
– Should have some appropriate meaning
Example:
Example:
Example:
The current 3-character prefixes
that will be used with the names
of variables. This could serve as
an extra classification of the
variable.
output semesterMark
stop
Operators
• The type of operator helps us understand the type of data that can be
processed and the value that likely to be returned from the execution
of an expression or equation.
• Three Categories of Operators
- Mathematical: We've dealt with mathematical operators
throughout school, so we understand them. Extra operators we
will use in pseudocode are integer division (\) and modulo division
(MOD).
- Relational: A programmer would use relational operators to do
program decisions.
- Logical: Used to connect relational expressions (decision-
making expressions) and to perform operations on logical data.
Common Mathematical Operations
• Standard mathematical operators:
+ (plus sign)—addition
− (minus sign)—subtraction
* (asterisk)—multiplication
/ (slash)—division
- Mathematical: Operators
Example: Assume that income = 8 and expense = 6. What is the value
of each of the following expressions?
(income + expense) * 2 Answer: 28
The Integer Data Type
• Dividing an integer by another integer is a special case
– Dividing two integers results in an integer, and any fractional
part of the result is lost
– The decimal portion of the result is cut off, or truncated
– 7\3 would result in 2, because 7 divided by 3 is
2.3333333333333335, but in integer division, the fractional
part is discarded, resulting in 2.
• A remainder operator (called the modulo operator or
the modulus operator) contains the remainder of a
division operation
– 24 Mod 10 is 4
– Because when 24 is divided by 10, 4 is the remainder
Activity:
Evaluate the following equations, given the values
A = 12, B = 3, C = 6, D = 2
a. F = A + B / C - D ^ 2
b. F = (A + B) / C - D ^ 2
c. F = A + B / (C - D ^ 2)
d. F = (A + B) MOD C
e. F = (A + B) \ D ^ 2
Activity:
Evaluate the following equations, given the values
A = 12, B = 3, C = 6, D = 2
a. F = A + B / C - D ^ 2
F = 12 + 3 / 6 - 2^2
F = 12 + 0.5 - 4
F = 12.5 - 4
F = 8.5
Activity:
Evaluate the following equations, given the values
A = 12, B = 3, C = 6, D = 2
d. F = (A + B) MOD C
F = (12 + 3) MOD 6
F = 15 MOD 6
F=3
- Relational: Operators
Activity:
Print the result of the following expressions:
a. 5 < 8
b. “A” > “H”
c. 35 <= 35
d. 35 < 67
a. True OR False
b. NOT True
c. False AND True
d. True AND True
e. False OR False
Activity:
Print the result of the following expressions:
a. True OR False
The OR operator evaluates to True if at least one of the
operands is True. In this case, the first operand is True,
so the result is True.
Activity:
Print the result of the following expressions:
Since a function will return a value, most of the time, we will consider
a variable to accept the value returned. e.g. fltResult = SQRT(10)
// the result of the calculation of the square root of 10, which isis
approximately 3.16227766016838, is assigned to the variable,
fltResult
Features of a Good Program Design
Use program comments where appropriate
• Comments help to document your program and give a brief
explanation to other programmers (including your lecturer) when
reviewing your program, they will be able to understand the logic
you have used in the chosen solution.
• Comments are not recognised as programming statements by the
compiler/interpreter.
• Syntax for comments differs amongst programming languages.
// comment line
• num fltVAT = 0.15 // constant VAT value assigned
/*
Multi line
Comment
*/
Features of a Good Program Design
Identifiers (variables) should be chosen carefully
- Choose a noun that represents the thing.
- Verbs are preferred for module names since they likely perform an
action.
- By using meaningful, descriptive names, your program becomes
self-documenting.
- use pronounceable names.
- use abbreviations wisely.
- avoid digits in a name.
- Follow a standard for naming - calcAnswer(), fltHourlyWage
- With regards to constants - we will be using capital letters, but still
using the prefix idea we are currently using in our lessons – fltVAT.
- A separate document for the program you write will detail the
entire list of all the variables used in a program is called a Data Dictionary.
Features of a Good Program Design
Design clear statements within your program and modules
Avoid confusing line breaks.
For example - if we are meant to output an answer with quite a long
sentence then consider ending a line with quotes or an operator.
Instead of
Features of a Good Program Design
Write clear prompts and output statements
- prompts are messages displayed on a monitor to ask the user for a
response. E.g.
output "Please enter your name"
input strName
- The idea of echoing input is to repeat the input back to the user in
an output and a subsequent prompt to further continue with the
processing of a program.
Activity:
a. R = A AND B OR C AND D
b. R = NOT (A AND B) OR NOT (D AND C)
c. R = (A OR B) AND (D OR C)
d. R = NOT (A AND B OR C) AND (A OR B AND D)
Activity:
Evaluate the following equations, given the values A =
False, B = True, C = False, D = True.
a. R = A AND B OR C AND D
R=False AND True OR False AND True
False AND True = False
False AND True = False
R=False