0% found this document useful (0 votes)
6 views45 pages

IPRG5111_Lecture 2S_2024

The document provides an overview of programming logic, covering topics such as computer systems, program development cycles, pseudocode, and flowcharting. It emphasizes the importance of correct logic in programming, the use of variables and constants, and the standards for writing pseudocode. Exercises are included to practice drawing flowcharts and writing pseudocode for various programming scenarios.

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)
6 views45 pages

IPRG5111_Lecture 2S_2024

The document provides an overview of programming logic, covering topics such as computer systems, program development cycles, pseudocode, and flowcharting. It emphasizes the importance of correct logic in programming, the use of variables and constants, and the standards for writing pseudocode. Exercises are included to practice drawing flowcharts and writing pseudocode for various programming scenarios.

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/ 45

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

png)

HMAW – IPRG5111
Introduction to Programming Logic
Lecture 2
Lecturer: Brendan van der Merwe Email times: 8am – 5pm,
Email: [email protected] Monday to Friday
Objectives
- An overview of IPRG5111

In your first Lecture, you will learn about:


• Computer systems Lecture 1
• Simple program logic
• 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:
Lecture 2
• Apply the rules for selecting and declaring variable or constant names
• Write mathematical expressions in the required format for a program
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.
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
Performing Mathematical Operations
• Standard mathematical operators:
+ (plus sign)—addition
− (minus sign)—subtraction
* (asterisk)—multiplication
/ (slash)—division
- Mathematical: Operators
Performing Arithmetic Operations
• Rules of precedence
– Also called the order of operations
– Dictate the order in which operations in the same
statement are carried out
– Expressions within parentheses () are evaluated first
– All the arithmetic operators have left-to-right associativity
– Multiplication and division are evaluated next
• From left to right
– Addition and subtraction are evaluated next
• From left to right
Example: Assume that income = 8 and expense = 6. What is the value
of each of the following expressions?
(income + expense) * 2 Answer: 28
Test Your Understanding
And create a flowchart.

You might also like