Farrell PLD 10e ch02 PowerPoint
Farrell PLD 10e ch02 PowerPoint
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
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
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
This would be a value that is used in various instructions. These are hard-coded values.
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
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.
- 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")
// 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.
num intNumber
1, at declaration
num intCount = 0
num intCount
intCount = 0
let intCount2;
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
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.
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)
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)
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
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
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
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
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
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
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
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
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)
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)
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)
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)
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
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
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
num pricePerFoot
num lotPremium
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)
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
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
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)
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)
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
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
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
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