0% found this document useful (0 votes)
20 views76 pages

Farrell PLD 10e ch02 PowerPoint

Chapter 2 of 'Programming Logic and Design' focuses on the elements of high-quality programs, including the declaration and use of variables and constants, arithmetic operations, and the advantages of modularization. It describes different data types, such as numeric and string, and emphasizes the importance of naming conventions for variables to enhance readability and maintainability. The chapter also covers the declaration process, type-safety, and the significance of using descriptive identifiers in programming.

Uploaded by

Paul Oberholzer
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)
20 views76 pages

Farrell PLD 10e ch02 PowerPoint

Chapter 2 of 'Programming Logic and Design' focuses on the elements of high-quality programs, including the declaration and use of variables and constants, arithmetic operations, and the advantages of modularization. It describes different data types, such as numeric and string, and emphasizes the importance of naming conventions for variables to enhance readability and maintainability. The chapter also covers the declaration process, type-safety, and the significance of using descriptive identifiers in programming.

Uploaded by

Paul Oberholzer
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/ 76

Programming Logic

and Design, 10e


Chapter 2: Elements of High-Quality
Programs

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or
duplicated, or posted to a publicly accessible website, in whole or in part.

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
1
Chapter Objectives

When you complete this chapter, you will be able to:


• Declare and use variables and constants
• Perform arithmetic operations
• Describe the advantages of modularization
• Modularize a program
• Describe the features of good program design

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
2
Introduction

When you write programs, you work with data of 2 different types:
• numeric
• string

You also work with data in 3 different forms:


• literals(or unnamed constants)
• variables
• named constants

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
3
Variables and Constants
• Variables
− 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.
• Constants
A constant is also a space in memory that is assigned to hold a value that is assigned at
declaration that never changes during the processing of all the instructions in a program.
They can be any data type
- Example - PI
const float PI = 3.14; // This is an example using JAVA
Const PI = 3.1415926535; // This is an example using JavaScript
The above-mentioned variable can be used as required and will never change. If there is
code that attempts to change the value, the compiler/interpreter will present an error
stating that this is not allowed.

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
4
Constants
There are 2 types of constants
• Named constants

It is a variable, which is declared with a name and assigned a value.

Example - fltNewPrice = fltPrice + (fltPrice * fltVAT)

The variable is used in various instructions as required.


• Literal constants (Unnamed)

This would be a value that is used in various instructions. These are hard-coded values.

Example - fltNewPrice = fltPrice + (fltPrice * 0.15)

-0.15 is an unnamed constant, whose purpose is not immediately apparent.


• When you declare a named constant, program maintenance becomes easier. Eg if the value of VAT
changes from 0.15 to 0.20 in future, you only need to change the value assigned to the named constant
at the beginning of the program, then during execution all references to VAT are automatically updated.

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
5
Understanding Unnamed, Literal Constants
• two types of unnamed/literal constants
− Numeric constant (or literal numeric constant)
 Number that does not change e.g 52.
 When you store a numeric value in computer memory, additional characters such as
rand signs, $, , are not input or stored.
 Those characters might be added to output for readability, but there are not part of the
number.
− String constant (or literal string constant)
 Appears within quotation marks
 Known as alphanumeric values
 Can contain alphabetic characters, numbers, and other characters. e.g “Thabang”, “56”,
“348 Thabo Sehume Street/- 7 $..”
 52 is an unnamed numeric constant because it does not have an identifier.
 “Thabang” is an unnamed string constant because it does not have an identifier.
Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
6
Declaring and Using Variables and
• Data type describes: Constants
− 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: Numeric data that can be used in arithmetic operations

These types of variables can hold numbers.

1. Integers - Represent all whole numbers, both positive and negative (1024, -256)

2. floating-point - Represent all real numbers, both positive and negative (-9876.55, 34522.0,
0.0003453)
− String / Character: Nonnumeric data. Can hold alphanumeric characters. Characters and Strings
cannot be used for mathematical operations.
− they can be compared and arranged in alphabetical order, if needed.

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
7
Declaring and Using Variables and

Constants
Joining characters forms a string. A string is an array of characters.

In programming, when we need to join more than one string or character to form a new string is known as concatenation, using the +
symbol in coding.

This is used mostly to output information for the user.

• Example - "5" + "5" will result in "55" and not 10

- character

All letters (CAPITAL and small), numbers, and special symbols ('A', 'h', '5', '&')

- string

• Combination of more than one character ("More than one word", "ST99999", "7676", "76 and 32")

• Boolean - Represents a logical value: True, or False

// Strings:
• colour = "Yellow"; // can be enclosed in double-inverted commas
• lastName = 'Johnson'; // or single-inverted commas
// Character
• agree = 'Y';

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
8
Working with Variables
• Variables are named memory locations whose contents can vary or differ over time.

• Remember that when a variable or constant is declared, then the computer will store the data internally in a
named memory location.

• you can access this memory location by stating the name of the variable or constant when needed.

• In the case of a variable, there is only one value that is stored in the memory location, but this value may be
overwritten by another, since a variable can change during the execution of a program.

• for a constant there is only 1 value stored in the memory location, but this value may not change at any time
during execution of a program.
• Declaration: Statement that provides a variable's:
− Data type
− Identifier
− Optionally, an initial value

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
9
Understanding a Variable’s Data Type

• Numeric variable
− Can hold digits and have mathematical operations performed on it
− Integers values, floating-point values (also called real numbers)
• String variable
− Can hold text and other special characters
 Letters of the alphabet and punctuation marks
• Type-safety
− Prevents assigning values of an incorrect data type

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
10
Declarations / Identifiers
The declaration statement appears at the beginning of the program, immediately after the start instruction.

• Declaration statement of a variable

- Declaration statement of a variable

[type] and [identifier]

num intNumber

let intNumber; // declaration statement

- Initialising a variable (assigning a value)

This can be done in 2 ways.

1, at declaration

num intCount = 0

let intCount = 0; // initialising a variable at declaration

2 . or after declaration on a different line

num intCount

intCount = 0

let intCount2;

intCount2 = 0; // variable is initialised on a different line

Choosing to initialise a variable with a value will be your choice, unless specifically requested.

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
11
Figure 2-1: Flowchart and Pseudocode for the
Number-Doubling Program

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
12
Figure 2-2: Flowchart and Pseudocode of Number-Doubling
Program with Variable Declarations

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
13
Understanding a Variable’s Identifier

• Identifier: Program component’s name or name of the variable.


• Programmers choose reasonable and descriptive names for variables
• Programming languages have rules for creating identifiers
− Most languages allow letters and digits within identifiers
− Some languages allow hyphens, underscores dollar signs, or other special characters in
variable names
− Each programming language has a few reserved keywords that are not allowed as
variable names as they are part of the language’s syntax

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
14
Understanding a Variable’s Identifier
• ** Please note that the following naming convention (known as camel case and a
mix of Hungarian case) will be used in the presentation of all programs shared.
num intNumber
num fltPrice
• // num is the classification (type) of a numerical variable

string chrOneCharacter
string strCompanyName
• // string is the classification (type) of an alphanumeric variable

boolean blnCheck
• // boolean is the classification (type) of a boolean variable that // only could have a value of true
or false
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.
- integer int
- float flt
- character chr
- string str
- boolean bln

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
15
Understanding a Declaration’s Identifier
• Identifiers are case sensitive
• Variable names:
− Must be one word
− Must start with a letter
− Should have some appropriate meaning
• you will need to follow the naming conventions of the company for which you are working. It is important for
all programmers within a company to follow the convention so that there is a consistency in all the
instructions coded.
• In cases when there are many programmers working on one solution, following the naming conventions will
eliminate conflicting names. It will also be readable amongst the team and the company. Also allows for
easy maintenance of the program. Naming conventions produce clean, well-written programs.

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
16
Understanding a Declaration’s Identifier
• Name a variable according to what it represents.

- Ask yourself, Is it descriptive enough?


- using one word is acceptable - fltPrice - because we know that the value being stored is the price of an
item.
- Variable must be one word, but if there is more than one word, each word must start with a capital letter.
- e.g., fltHourlyWage, intRandomNumber
- Variable names are case sensitive throughout the source code of a program.
** RESERVED words are not allowed to be used as names of variables
• RESERVED words would be stop, input, output.

- Do not start a name with a digit/number - always start with a letter


- Do not name variables with spaces in between words. If you do require a space, then use the underscore '_'
character to represent a space strFirst_Name
- For names of constants - use capital letters fltVAT, fltPI, strCOMPANYNAME

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
17
Table 2-1: Variable Naming Conventions
(1 of 2)

Convention for naming variables Examples Languages in


which commonly
used
Camel casing is the convention in which the hourlyWage Java, C#
variable starts with a lowercase letter and any lastName
subsequent word begins with an uppercase
letter. It is sometimes called lower camel casing
to emphasize the difference from Pascal casing.
Pascal casing is a convention in which the first HourlyWage Visual Basic
letter of a variable name is uppercase. It is LastName
sometimes called upper camel casing to
distinguish it from lower camel casing.
Hungarian notation is a form of camel casing in numHourlyWage C for Windows API
which a variable’s data type is part of the stringLastName programming
identifier.

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
18
Table 2-1: Variable Naming Conventions
(2 of 2)

Convention for naming variables Examples Languages in


which commonly
used
Snake casing is a convention in which parts of a hourly_wage C, C++, Python,
variable name are separated by underscores. last_name Ruby

Mixed case with underscores is a variable Hourly_Wage Ada


naming convention similar to snake casing, but Last_Name
new words start with an uppercase letter.
Kebob case is sometimes used as the name for hourly-wage Lisp (with
the style that uses dashes to separate parts of last-name lowercase letters),
a variable name. The name derives from the COBOL (with
fact that the words look like pieces of food on a uppercase letters)
skewer.

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
19
Assigning Values to Variables

• Assignment statement
− myAnswer = myNumber * 2
• Assignment operator (=)
− Example of 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
 lvalue: Result to the left of an assignment operator

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
20
Initializing a Variable

• Initializing the variable: Declaring a starting value when a variable is declared


− num yourPayRate = 24.55
− string yourName = "Juanita“
• Garbage: Variable’s unknown value
• Variables must be declared before they are used in the program

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
21
Declaring Named Constants
• Named constant
− Similar to a variable, except it can be assigned a value only once
− Assign a useful name to a value that will never be changed during a program’s execution
num SALES_TAX_RATE = 0.06
− Example: Both taxAmount = price * SALES_TAX_RATE and
taxAmount = price * 0.06 have identical meaning
• Magic number
− Unnamed constant whose purpose is not immediately apparent e.g 0,06
• When you declare a named constant, program maintenance becomes easier. Eg if the value of
VAT changes from 0.06 to 0.12 in future, you only need to change the value assigned to the
named constant at the beginning of the program, then during execution all references to VAT
are automatically updated.

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
22
Knowledge Check Activity

Identify the rule pertaining to variable names.


a. Variable names should have some appropriate meaning.
b. Variable names must start with a number.
c. Variable names must be two words.
d. Variable names must be written using all uppercase letters.

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
23
Knowledge Check Activity: Answer

Identify the rule pertaining to variable names.


Answer: a. Variable names should have some appropriate meaning.
Variable names must be one word, must start with a letter, should have some
appropriate meaning, and are case sensitive.

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
24
Programming
• Input (Programming)

Input is accepting a value from the user and assigning the value entered to a variable named in the
instruction.
- e.g., input strName
• Output (Programming)

Output is used to display statements to the user. The statements are always enclosed in double
inverted commas ("").
It is used in 2 instances.
1, - Asking the user to input a value
- e.g., output "Please enter the price of the product“
2, -Displaying a result to the user after the execution of certain instructions
- e.g., output "The final price with VAT is R" + fltNewPrice

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
25
Comments in Programming
• Comments are an important factor that programmers need to consider for another
programmer to read the code, or the same programmer needs to be reminded of a
reason for implementing such code.
Comments are preceded by 2 forward slash characters
- They can be placed at the end of an instruction or on a new line.
e.g., fltNewPrice = fltNewPrice + (fltPrice * fltVAT) //calculating

e.g. output "Please enter your name"


input strName

// The above statement reads in a value into the variable, strName


- If you need to place a comment on multiple lines, away from the code, then please use the following
structure, a forward slash followed by an asterisk, and at the end of the comment, an asterisk followed by a
forward slash.
- e.g.
/* This is comment on
multiple
lines
*/
Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
26
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 is
//assigned to the variable, fltResult

e.g
num fltAnswer
fltAnswer = Math.sqrt(64)
Output "The square root of 64 is " + fltAnswer)

- The (data - 64) shown above is referred to as a "parameter" and some others refer to it as the
"argument that is passed", in this case to the function.

- There are many pre-loaded functions that are available to use, even within the scope of the
pseudocode we are learning, and we will learn them as we come across them.
Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
27
Types of built-in functions
Mathematical functions
• used in science, mathematics, or business, calculating.an absolute value (e.g., square root), random number generation
and much more
• The following link contains an extensive list of mathematical functions:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math

String functions
• these are used to manipulate string variables. We can copy parts of a string to another variable, we can find the length of
a string or a specific character, locate a single character within a string, join two strings together etc.
• List of string functions: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String

Conversion functions
• Are used to convert data from one data type to another. Not used much in pseudocode, but in programming languages,
we will see a need for them.
• Example, in JavaScript, there is function/window that we can use to input/read a value, prompt(), but when a value is
entered and returned from the function, it is always a value of a String data type that is returned from the function and if
we asked for a number, we would need to use a conversion function to change the string value (a list of characters) into a
number.
• Conversion functions available at: https://javascript.info/type-conversions

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
28
Types of built-in functions
• Statistical functions
• - Calculates maximum and minimum values, mean and median values, and so forth.
• Utility functions
• - When we need to produce reports, for example, utility functions will be used. These functions can access
information outside of the program and the language of the PC.
• - For example, we can retrieve the data and time and be able to display it in a report when required.

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
29
Operators
• Operators are data connectors within expressions and equations. They help us tell how a
program processes the data. The type of operator will also help us understand the type of
data that can be processed and the value that likely to be returned from the execution of the
expression or the equation.
Three Categories of Operators
• Mathematical / Arithmetic
Used for performing arithmetic computations.
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.
• Standard mathematical/arithmetic operators:
− + (plus sign)—addition
− − (minus sign)—subtraction
− * (asterisk)—multiplication
− / (slash)—division

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
30
Operators

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
31
Operators

• Rules of precedence, or 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

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
32
Order of Precedence (Operators)

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
33
Operands and Results
• The operand and the result are two ideas related to operators.
• Operands are the data that the operator connects and processes.
• The result is the answer that is produced when the operation is complete.

Example
• fltPrice = fltPrice + (fltPrice * fltVAT)
• fltWages = (40 * fltPayRate) + ((fltHours - 40) * fltPayRate * 1.5))
• F = 6 * (2 \ 6 (6 + 2))

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
34
Expressions and Equations
-expressions and equations are developed to perform the required tasks to arrive at the result.
-they make up part of the instructions.
Expressions
- processes data, the operands, using operators
e.g., length * width
- expressions do not use the equal to "=" sign, except as a relational operator
The result is not stored in memory and, therefore, unavailable for use at a later stage.
Equations
- store the result of an expression in a memory location in the computer (the variable being used to accept the
answer)
area = length * width
-in the assignment statement - the variable on the left is assigned (given) the value of the result of the expression on
the right-hand side

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
35
Expressions and Equations
- The assignment statement intCount = intCount + 1 is quite interesting
- we wouldn't learn this during school days, but here in programming and based on how we read an assignment
statement
- the variable intCount is assigned the result of the expression on the right which is, intCount + 1
Examples:
• Expression Equation
•a + b c = a + b
•a < b c = a < b
• a AND b c = a AND b

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
36
Mixing Data Types

• 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
• Remainder operator, called the modulo operator or the modulus operator, is the value that
remains after division
− 24 Mod 10 is 4
 Because when 24 is divided by 10, 4 is the remainder

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
37
Understanding the Advantages of Modularization

• Modules
− Subunit of programming problem
− Called subroutines, procedures, functions, or methods
− To call a module is to use its name to invoke the module, causing it to execute
• Modularization
− Breaking down a large program into modules
− Called functional decomposition

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
38
Modularization Provides Abstraction

• Abstraction: Paying attention to important properties while ignoring nonessential details


− Selective ignorance
• Newer high-level programming languages use English-like vocabulary
− One broad statement corresponds to dozens of machine instructions
• Modules provide another way to achieve abstraction

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
39
Modularization Allows Multiple Programmers to
Work on a Problem

• Dividing any large task into modules makes it easier to divide the task among various people
• Rarely does a single programmer write a commercial program
− Professional software developers can write new programs quickly by:
 Dividing large programs into modules
 Assigning each module to an individual programmer or team

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
40
Modularization Allows to Reuse Work

• Reusability
− Feature of modular programs that allows individual modules to be used in a variety of
applications
− Many real-world examples found
• Reliability
− Assures that a module has been tested and proven to function correctly

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
41
Discussion Activity

Discuss the reasons why programmers should consider modularizing programs.

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
42
Discussion Activity: Answer

Modularized programs are easier to understand in that they enable a programmer to see the “big picture.”
Abstraction is the process of paying attention to important properties while ignoring nonessential details.
Abstraction makes complex tasks look simple. When any large task is divided into modules, programmers gain the
ability to more easily divide the task among various people. Professional software developers can write new
programs in weeks or months, instead of years, by dividing large programs into modules and assigning each
module to an individual programmer or team. The reusability of modular programs allows individual modules to be
used in a variety of applications. Reliability is the feature of programs that assures that a module has been proven
to function correctly. Reliable software saves time and money. If programmers create the functional components of
their programs as stand-alone modules and test them in their current programs, much of the work already will be
done when they use the modules in future applications.

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
43
Modularizing a Program (1 of 3)

• Main program: Basic steps, or mainline logic, of the program


• When a module is created, it includes
− Module header
− Module body
− Module return statement
• Naming a module is similar to naming a variable
− Module names must start with a letter and cannot contain spaces, should have meaning,
and are followed by a set of parentheses

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
44
Modularizing a Program (2 of 3)

• When a main program wants to use a module, it calls the module, or invokes it
• Flowchart symbol used to call a module is a rectangle with a bar across the top
− Name of the module being called is placed inside the rectangle
− Each module is drawn separately with its own sentinel symbols
 Each module begins with its name and ends with a return statement

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
45
Figure 2-3: Program That Produces A Bill Using
Only Main Program

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
46
Figure 2-4: Program That Produces a bill Using Main Program
That Calls displayAddressInfo() Module

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
47
Modularizing a Program (3 of 3)

• Encapsulation: Technique of grouping program elements within a module


• Modularized version of the program is preferred as:
− Main program remains short and easy to follow because it contains just one statement to
call the module
− Module is easy to reuse
• When statements contribute to the same job, it results in greater functional cohesion

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
48
Declaring Variables and Constants within Modules
(1 of 2)

• Any statements can be placed within modules


− Includes input, processing, output statements, and variable and constant declarations
• Variables and constants are usable only in the module in which they are declared
− Programmers say the data items are visible, in scope, or local
• Modules are portable
− Portable: Self-contained units that are easily transported

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
49
Declaring Variables and Constants within Modules
(2 of 2)

• Global variables and constants


− Known to the entire program
− Declared at the program level
− Visible to and usable in all the modules called by the program
− No approved by many programmers

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
50
Figure 2-5: The Billing Program with Constants Declared
within the Module

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
51
Understanding the Most Common
Configuration for Mainline Logic

• Mainline logic of almost every procedural computer program follows a general structure
− Housekeeping tasks: Steps one must perform at the beginning of a program to get ready
for the rest of the program
− Detail loop tasks: Do the core work of the program
− End-of-job tasks: Steps one takes at the end of the program to finish the application

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
52
Figure 2-6: Flowchart and Pseudocode of Mainline Logic
for a Typical Procedural Program

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
53
Figure 2-7: Sample Payroll Report

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
54
Figure 2-8: Logic for Payroll Report

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
55
Creating Hierarchy Charts

• Program hierarchy chart


− Shows the overall picture of how modules are related to one another
− Tells which modules exist within a program and which modules call others
− Can be a planning tool for developing the overall relationship of program modules before
one writes them
− Can be documentation tool to help others see how modules are related after a program is
written

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
56
Figure 2-10: Hierarchy Chart of Payroll Program in
Figure 2-8

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
57
Figure 2-11: Billing Program Hierarchy Chart

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
58
Think, Pair, Share

• Suppose you own a cookie factory, and you want to represent the activities in the factory
using mainline logic. Take a few minutes to think about the three parts of a typical procedural
program and create a flowchart illustrating the activities.
• Pair up with a partner and discuss your thought process and approach to the problem. Ask
each other questions and provide feedback on each other’s solutions.
• Share your approach and solution with the rest of the class.

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
59
Features of Good Program Design

• Providing program comments where appropriate


• Choosing identifiers thoughtfully
• Striving to design clear statements within your programs and modules
• Writing clear prompts and echo input
• Continuing to maintain good programming habits as you develop your programming skills

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
60
Using Program Comments (1 of 2)

• Program comments
− Written explanations of programming statements that are not part of the program logic
− Serve as internal documentation for readers of the program
• Syntax used differs among programming languages
• In a flowchart, one can use an annotation symbol to hold information that expands on what
is stored within another flowchart symbol

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
61
Using Program Comments (2 of 2)

Declarations

num sqFeet

// sqFeet is an estimate provided by the seller of the property

num pricePerFoot

// pricePerFoot is determined by current market conditions

num lotPremium

// lotPremium depends on amenities such as whether lot is waterfront

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
62
Figure 2-13: Flowchart That Includes Annotation
Symbols

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
63
Choosing Identifiers (1 of 2)

• General guidelines
− Give a variable or a constant a name that is a noun because it represents a thing
− Give a module an identifier that is a verb because it performs an action
− Use meaningful names
 Self-documenting: Programs that contain meaningful names
− Use pronounceable names
− Be judicious in the use of abbreviations

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
64
Choosing Identifiers (2 of 2)

− Avoid digits in a name


− Use the rules allowed by the language to separate words in long, multiword variable names
− Consider including a form of the verb to be
− Name constants using all uppercase letters separated by underscores (_)
• Programmers sometimes create a data dictionary, which is a list of every variable name
used in a program, along with its type, size, and description

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
65
Designing Clear Statements

• Avoiding confusing line breaks


• Using temporary variables to clarify long statements

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
66
Avoiding Confusing Line Breaks

• Most modern programming languages are free-form


− One must make sure the meaning is clear
− Programmers are allowed to place two or three statements on a line or to spread a single
statement across multiple lines
 Both make programs harder to read

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
67
Using Temporary Variables to Clarify Long
Statements (1 of 2)

• Temporary variable, or work variable


− Not used for input or output
− Working variable that is used during a program’s execution
• Series of temporary variables to hold intermediate results can be used when several
mathematical operations are needed to determine a result

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
68
Using Temporary Variables to Clarify Long
Statements (2 of 2)

//Using a single statement to compute commission

salesCommission = (sqFeet * pricePerFoot + lotPremium) * commissionRate

// Using multiple statements to compute commission

basePropertyPrice = sqFeet * pricePerFoot

totalSalesPrice = basePropertyPrice + lotPremium

salesCommission = totalSalesPrice * commissionRate

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
69
Writing Clear Prompts and Echoing Input

• Prompt
− Message displayed on a screen to ask the user for a response
− Used both in command-line and GUI interactive programs
• Echoing input
− Repeating input back to a user either in a subsequent prompt or in output

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
70
Figure 2-15: Beginning of a Program That Accepts
a Name and a Balance as Input

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
71
Figure 2-16: Beginning of a Program That Accepts a Name and a Balance
as Input and Uses a Separate Prompt for Each Item

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
72
Figure 2-17: Beginning of a Program That Accepts a Customer’s
Name and Uses It in the Second Prompt

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
73
Maintaining Good Programming Habits

• Every program written will be better if programmers:


− Plan before they code
− Maintain the habit of first drawing flowcharts or writing pseudocode
− Desk-check their program logic on paper
− Think carefully about the variable and module names they use
− Design their program statements to be easy to read and use

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
74
Self-Assessment

• Define a variable and a constant in programming.


• What is the order of arithmetic operations in a programming language?
• What are the advantages of modularization?
• How can one modularize a program?
• What are the features of good program design?

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
75
Summary

Click the link to review the objectives for this presentation.


Link to Objectives

Farrell, Programming Logic and Design, 10 th Edition. © 2024 Cengage. All Rights Reserved. May not be scanned, copied or duplicated, or posted to a publicly
accessible website, in whole or in part.
76

You might also like