0% found this document useful (0 votes)
145 views67 pages

IPRG5111_Lecture 3S_2024

The document outlines the curriculum for an Introduction to Programming Logic course, covering topics such as computer systems, program development cycles, pseudocode, flowcharts, and variable management. It emphasizes the importance of logical and syntax error identification, the use of operators, and the significance of clear program design. Exercises are included to reinforce learning through practical application of concepts like flowchart creation and pseudocode writing.

Uploaded by

shantonjohnson98
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
145 views67 pages

IPRG5111_Lecture 3S_2024

The document outlines the curriculum for an Introduction to Programming Logic course, covering topics such as computer systems, program development cycles, pseudocode, flowcharts, and variable management. It emphasizes the importance of logical and syntax error identification, the use of operators, and the significance of clear program design. Exercises are included to reinforce learning through practical application of concepts like flowchart creation and pseudocode writing.

Uploaded by

shantonjohnson98
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 67

(http://valenciacollege.edu/asdegrees/information-technology/images/it-computer-programming-analysis.

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

• Logic of the computer program


– Sequence of specific instructions in specific order
Understanding the Program
Development Cycle
• Program development cycle
– Understand the problem
– Plan the logic
– Code the program
– Use software (a compiler or interpreter) to translate the
program into machine language
– Test the program
– Put the program into production
– Maintain the program
Understanding the Program
Development Cycle
Using Pseudocode Statements
and Flowchart Symbols
• Pseudocode
– English-like representation of the logical steps it takes to
solve a problem

• 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?

A variable is a space in memory that is assigned to hold a value


that may change during the processing of a program.

Some environments call variables, identifiers.


Declaring and Using Variables
and Constants
• Understanding Data Types
– Data type describes:
• What values can be held by the item
• How the item is stored in memory
• What operations can be performed on the item

– All programming languages support these data types:


• Numeric consists of numbers that can be used in math
• String is anything not used in math
Working with Variables
• Variable are named memory locations

• Contents can vary or differ over time

• Declaration is a statement that provides a variable's:


– Data type
– Identifier (variable’s name)
– Optionally, an initial value
Working with Variables
Understanding a Declaration’s Data
Type
• Numeric variable
– Holds digits
– Can perform mathematical operations on it
• String variable
– Can hold text
– Letters of the alphabet
– Special characters such as punctuation marks
• Type-safety
– Prevents assigning values of an incorrect data type
Understanding a Declaration’s
Identifier
• An identifier is a variable’s name
• Programmer chooses reasonable and descriptive
names for variables
• Programming languages have rules for creating
identifiers
– Most languages allow letters and digits
– Some languages allow hyphens
– Reserved keywords are not allowed
Understanding a Declaration’s
Identifier
• Variable names are case sensitive

• 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.

The prefix will always be written


in small characters, immediately
followed by a capital letter for
the chosen name.
Understanding Constants
• There are two types of constants
– Numeric constant (or literal numeric constant)
• Contains numbers only
• Number does not change
• Example: num VAT = 0.15

– String constant (or literal string constant)


• Also known as Alphanumeric values
• Can contain both alphabetic characters and numbers
• Strings are enclosed in quotation marks
• Example: string REPORT_HEADING = “Payroll Report”
Variable Naming Conventions
• Camel casing
– Variable names have a “hump” in the middle such as
hourlyWage
• Pascal casing
– Variable names have the first letter in each word in
uppercase such as HourlyWage
• Hungarian notation
– A form of camel casing in which the data type is part of the
name such as numHourlyWage
Variable Naming Conventions
• Snake casing
– Parts of variable names are separated by underscores such
as hourly_wage
• Mixed case with underscores
– Similar to snake casing, but new words start with a
uppercase letter such as Hourly_Wage
• Kebob case
– Parts of variable names are separated by dashes such as
hourly-wage
Assigning Values to Variables
• Assignment statement
– set myAnswer = myNumber * 2
• Assignment operator
– Equal sign
– A binary operator, meaning it requires two operands—one
on each side
– Always operates from right to left, which means that it has
right-associativity or right-to-left associativity
– The result to the left of an assignment operator is called an
lvalue
Initializing a Variable
• Initializing the variable - declare a starting value
– num yourSalary = 14.55
– string yourName = “Janita”

• Variables must be declared before they are used in


the program
Test Your Understanding
And create a flowchart.
start
output “Please enter your first test mark”
input testMark1
output “Please enter your second test mark”
input testMark2
output “Please enter your assignment mark”
input assignmentMark

set semesterMark = (testMark1*0.15) + (testMark2*0.15) + (assignmentMark*0.2)

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

b. False ("A" comes before "H" in the ASCII table)


- Logical: Operators
Activity:
Print the result of the following expressions:

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:

d. True AND True


The AND operator evaluates to True only if both
operands are True. In this case, both operands are True,
so the result is True.
Functions (Built in)
These are small sets of instructions that perform a task and return a
value.
They are usually built into the programming language.

The syntax of calling a function - functionName(data) ... an expression

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:

Evaluate the following equations, given the values A =


False, B = True, C = False, D = True.

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

You might also like