Programming Logic and Design: Sixth Edition
Programming Logic and Design: Sixth Edition
Chapter 1
2
Programming Logic & Design, Sixth Edition
Objectives
In this chapter, you will learn about: Computer systems Simple program logic The steps involved in the program development cycle Pseudocode statements and flowchart symbols Using a sentinel value to end a program Programming and user environments The evolution of programming models
3
Programming Logic & Design, Sixth Edition
Hardware
Equipment associated with a computer
Software
Computer instructions Tell the hardware what to do Programs
Instructions written by programmers
4
Programming Logic & Design, Sixth Edition
Processing
By central processing unit (CPU)
Output
Display the results
Input
Processing
Output
5
Programming Logic & Design, Sixth Edition
Syntax
Rules governing its word usage and punctuation
Computer memory
Volatile, Computers temporary, internal storage
6
Programming Logic & Design, Sixth Edition
Compiler or an interpreter
Translates program code into machine language (binary language) Checks for syntax errors
7
Programming Logic & Design, Sixth Edition
3. Variable
Named memory location whose value can vary
8
Programming Logic & Design, Sixth Edition
9
Programming Logic & Design, Sixth Edition
10
Programming Logic & Design, Sixth Edition
Documentation Supporting paperwork for a program to help the programmer for understanding the problem.
11
Programming Logic & Design, Sixth Edition
Programmer focus on figuring out what sequence of events will lead from the available input to the desired output. Planning the logic includes thinking carefully about all the possible data values a program might encounter and how you want the program to handle each scenario. The process of walking through a programs logic on paper before you actually write the program is called desk-checking.
12
Programming Logic & Design, Sixth Edition
13
Programming Logic & Design, Sixth Edition
Syntax error
Called Compiler or interpreter Changes the programmers English-like high-level programming language into the low-level machine language Produces the executable program only when the code is free of syntax errors. Misuse of a languages grammar rules Programmer corrects listed syntax errors Might need to recompile the code several times to get the executable program
14
Programming Logic & Design, Sixth Edition
15
Programming Logic & Design, Sixth Edition
Testing
Execute/run the program with some sample data to see whether the results are logically correct
16
Programming Logic & Design, Sixth Edition
Conversion
Entire set of actions an organization must take to switch over to using a new program or set of programs For example,
data-entry people must be trained to prepare the input for the new program; users must be trained to understand the output; or existing data in the company must be changed to an entirely new format to accommodate this program.
17
Programming Logic & Design, Sixth Edition
18
MCTS Guide to Microsoft Windows Server 2008 Network Infrastructure Configuration
19
MCTS Guide to Microsoft Windows Server 2008 Network Infrastructure Configuration
20
MCTS Guide to Microsoft Windows Server 2008 Network Infrastructure Configuration
21
MCTS Guide to Microsoft Windows Server 2008 Network Infrastructure Configuration
22
Programming Logic & Design, Sixth Edition
2. Flowchart
Pictorial representation of the logical steps it takes to solve a problem
23
Programming Logic & Design, Sixth Edition
Writing Pseudocode
Pseudocode representation involves
writing down all the steps you will use in a program. Pseudocode representation of a number-doubling problem
start input myNumber set myAnswer = myNumber * 2 output myAnswer stop
beginning statement like start and end it with a terminating statement like stop
24
Programming Logic & Design, Sixth Edition
Drawing Flowcharts
Flowcharts allow programmers
to visualize more easily how the program statements will connect.
Create a flowchart
Draw geometric shapes that contain the individual statements Connect shapes with arrows
25
MCTS Guide to Microsoft Windows Server 2008 Network Infrastructure Configuration
Drawing Flowcharts
Input symbol
Indicates input operation Parallelogram Processing statements such as arithmetic Rectangle Represents output statements Parallelogram Because the parallelogram is used for both input and output, it is often called the input/output symbol or I/O symbol.
Processing symbol
Output symbol
26
Programming Logic & Design, Sixth Edition
Terminal symbols
Start/stop symbols Shaped like a racetrack Also called lozenge
27
MCTS Guide to Microsoft Windows Server 2008 Network Infrastructure Configuration
28
Programming Logic & Design, Sixth Edition
29
MCTS Guide to Microsoft Windows Server 2008 Network Infrastructure Configuration
30
Programming Logic & Design, Sixth Edition
31
Programming Logic & Design, Sixth Edition
Declaration
Statement that provides a data type and an identifier for a variable Identifier is a variables name A data items data type is a classification that describes the following:
What values can be held by the item How the item is stored in computer memory What operations can be performed on the data item
32
MCTS Guide to Microsoft Windows Server 2008 Network Infrastructure Configuration
Declaration
Computers deal with two basic types of datatext and numeric. Two data types will be used:
1. 2.
num string.
Identifier (variable name) Value (a starting value) initializing
num mySalary num yourSalary = 14.55 string myName string yourName = "Juanita"
The process of naming program variables and assigning a type to them is called making declarations, or declaring variables. Declaring a starting value is known as initializing the variable.
33
Programming Logic & Design, Sixth Edition
Garbage
Variables unknown value before initialization declare a variable and do not initialize a value, the variable contains an unknown value until it is assigned a value. A variables unknown value is commonly called garbage.
Variables must be declared before they are used in a program for the first time.
34
Programming Logic & Design, Sixth Edition
35
Programming Logic & Design, Sixth Edition
Figure 2-2 Flowchart and pseudocode of number-doubling program with variable declarations
36
Programming Logic & Design, Sixth Edition
Naming Variables
Programmer chooses reasonable and descriptive names for variables Programming languages have rules for creating identifiers
Most languages allow letters and digits
for example, j89
37
Programming Logic & Design, Sixth Edition
Pascal casing
When the first letter of a variable name is uppercase, as in HourlyWage, the format is known as Pascal casing.
38
Programming Logic & Design, Sixth Edition
Unnamed constants
Do not have identifiers like variables do
39
Programming Logic & Design, Sixth Edition
2. String variable
Can hold text Letters of the alphabet Special characters such as punctuation marks
40
Programming Logic & Design, Sixth Edition
Magic number
41
Programming Logic & Design, Sixth Edition
Assignment operator
Equal sign Always operates from right to left
Valid
set someNumber = 2 set someNumber = someOtherNumber set totalScore = test1 + test2
Not valid
set 2 + 4 = someNumber
42
Programming Logic & Design, Sixth Edition
43
Programming Logic & Design, Sixth Edition
For example
firstAnswer = 2 + 3 * 4 14 secondAnswer = (2 + 3) * 4 20 average = score1 + score2 / 2 average = (score1 + score2) / 2 totalPriceWithTax = price + price * TAX_RATE totalPriceWithTax = price + (price * TAX_RATE)
44
Programming Logic & Design, Sixth Edition
Therefore, the statement becomes: answer = a + b + (temporary result just calculated) f Then, addition and subtraction are carried out from left to right as follows:
a and b are
Another way to say this is that the following two statements are equivalent: answer = a + b + c * d / e f answer = a + b + ((c * d) / e) f
added, the temporary result is added, and then f is subtracted. The final result is then assigned to answer.
45
Programming Logic & Design, Sixth Edition
46
Programming Logic & Design, Sixth Edition
Summary
Computer programming
Programmers job
Requires specific syntax Must develop correct logic Understanding the problem, planning the logic, coding the program, translating the program into machine language, testing the program, putting the program into production, and maintaining it
47
Programming Logic & Design, Sixth Edition
Exercises
(1) Draw a flowchart and write pseudo code to represent the logic of a program which will calculate area of the circle using the formula area=pi * radius * radius (pi = 3.142) (2) Draw a flowchart and write pseudo code to represent the logic of a program for the square and cube of any number input. (3) Draw a flowchart and write pseudo code to represent the logic of a program to input two sides of a rectangle. Calculate the area of the rectangle and print out the answer. (4) Draw a flowchart and write pseudo code to represent the logic of a program that allows the user to enter a value. The program multiplies the value by 10 and outputs the result.
48
MCTS Guide to Microsoft Windows Server 2008 Network Infrastructure Configuration
Exercises
(5) Draw a flowchart or write pseudocode to represent the logic of a program that allows the user to enter two values. The program outputs the sum of the two values. (6) Draw a flowchart or write pseudocode to represent the logic of a program that allows the user to enter three values. The values represent hourly pay rate, the number of hours worked this pay period, and percentage of gross salary that is withheld. The program multiplies the hourly pay rate by the number of hours worked, giving the gross pay. Then, it multiplies the gross pay by the withholding percentage, giving the withholding amount. Finally, it subtracts the withholding amount from the gross pay, giving the net pay after taxes. The program outputs the net pay.