Programming Logic and Design Sixth Edition
Chapter 1
An Overview of Computers and Programming
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
Understanding Computer Systems
Computer system
Combination of all the components required to process and store data using a computer
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
Understanding Computer Systems (cont.)
Programming
Writing software instructions
Computer hardware and software accomplish three major operations
Input
Data items enter into computer
Processing
By central processing unit (CPU)
Output
Display the results
Input
Processing
Output
5
Programming Logic & Design, Sixth Edition
Understanding Computer Systems (cont.)
Programming language
Use to write computer instructions Examples
Visual Basic, C#, C++, or Java
Syntax
Rules governing its word usage and punctuation
Computer memory
Volatile, Computers temporary, internal storage
6
Programming Logic & Design, Sixth Edition
Understanding Computer Systems (cont.)
Permanent storage devices
Nonvolatile (Permanent storage)
Compiler or an interpreter
Translates program code into machine language (binary language) Checks for syntax errors
Program executes or runs
Input will be accepted, some processing will occur, and results will be output
7
Programming Logic & Design, Sixth Edition
Understanding Simple Program Logic
Program with syntax errors cannot execute 1. Logical errors
Errors in program logic Produce incorrect output as a result
2. Logic of the computer program
Sequence of specific instructions in specific order
3. Variable
Named memory location whose value can vary
8
Programming Logic & Design, Sixth Edition
Understanding the Program Development Cycle
Program development cycle
1. 2. 3. 4. Understand the problem Plan the logic Code the program Use software (a compiler or interpreter) to translate the program into machine language 5. Test the program 6. Put the program into production 7. Maintain the program
9
Programming Logic & Design, Sixth Edition
Understanding the Program Development Cycle (continued)
Figure 1-1 The program development cycle
10
Programming Logic & Design, Sixth Edition
1. Understanding the Problem
One of the most difficult aspects of programming Users or end users
People for whom program is written Programmers are providing a service to these users Programmers must first understand what the users want. When a program runs, think of the logic as a cycle of inputprocessing-output operations When you plan a program, think of the output first. After understanding what the desired result is, then you can plan what to input and process to achieve it.
Documentation Supporting paperwork for a program to help the programmer for understanding the problem.
11
Programming Logic & Design, Sixth Edition
2. Planning the Logic
Heart of the programming process (Programming Logic and Design) During this phase of the process, the programmer
Most common planning tools Desk-checking
1. 2. Flowcharts Pseudocode plans the steps of the program, deciding what steps to include and how to order them.
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
3. Coding the Program
After the logic is developed, only then the programmer write the program. The logic developed to solve a programming problem can be executed using any number of languages. Hundreds of programming languages are available
Choose based on features Alike in their basic capabilities each language can handle input operations, arithmetic processing, output operations, and other standard functions.
Coding is easier than planning step
Coding concerned with proper punctuation and the correct spelling of commandsin other words, using the correct syntax.
13
Programming Logic & Design, Sixth Edition
4. Using Software to Translate the Program into Machine Language
Why we need software (program coding) to translate?
Translator program
Each computer knows only one language: its machine language, which consists of 1s and 0s.
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
4. Using Software to Translate the Program into Machine Language (continued)
Figure 1-2 Creating an executable program
15
Programming Logic & Design, Sixth Edition
5. Testing the Program
A program that is free of syntax errors is not necessarily free of logical errors. Logical error
Use a syntactically correct statement but use the wrong one for the current context The process of finding and correcting program errors is called debugging.
Testing
Execute/run the program with some sample data to see whether the results are logically correct
Programs should be tested with many sets of data
16
Programming Logic & Design, Sixth Edition
6. Putting the Program into Production
Once the program is tested adequately, it is ready for use. Putting the program into production might mean simply running the program once, if it was written to satisfy a users request for a special list. Process depends on programs purpose
May take several months
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
7. Maintaining the Program
Maintenance
Making changes after program is put into production
Common first programming job
Maintaining previously written programs
Make changes (or) add new functions to existing programs
Repeat the development cycle
18
MCTS Guide to Microsoft Windows Server 2008 Network Infrastructure Configuration
Which statements are true?
19
MCTS Guide to Microsoft Windows Server 2008 Network Infrastructure Configuration
Which statements are true?
20
MCTS Guide to Microsoft Windows Server 2008 Network Infrastructure Configuration
Which statements are true?
21
MCTS Guide to Microsoft Windows Server 2008 Network Infrastructure Configuration
Step 1 : Understanding the Problem Step 2 : Planning the Logic
22
Programming Logic & Design, Sixth Edition
Using Pseudocode Statements and Flowchart Symbols
1. Pseudocode
English-like representation of the logical steps it takes to solve a problem
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
Flexible because it is a planning tool
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
Drawing Flowcharts (continued)
Flowlines
Arrows that connect steps
Terminal symbols
Start/stop symbols Shaped like a racetrack Also called lozenge
27
MCTS Guide to Microsoft Windows Server 2008 Network Infrastructure Configuration
Drawing Flowcharts (continued)
You can draw a flowchart
by hand or use software, such as Microsoft Word and Microsoft PowerPoint, that contains flowcharting tools. can use several other software programs, such as Visio and Visual Logic, specifically to create flowcharts.
28
Programming Logic & Design, Sixth Edition
Drawing Flowcharts (continued)
Figure 1-6 Flowchart and pseudocode of program that doubles a number
29
MCTS Guide to Microsoft Windows Server 2008 Network Infrastructure Configuration
Which statements are true?
30
Programming Logic & Design, Sixth Edition
Declaring and Using Variables and Constants
Data items program has to deal with
All the text, numbers, and other information that are processed by a computer Stored in variables in memory
Three Different forms of data storage in memory
1. Variables 2. Literals, or unnamed constants 3. Named constants
31
Programming Logic & Design, Sixth Edition
Working with Variables
Variablies
Named memory locations to store the user input value or calculated value Contents can vary or differ over time
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
For example, a valid declaration: Data Type
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
Working with Variables (continued)
Initializing a variable
Declare a starting value for any variable
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
Working with Variables (continued)
Figure 2-1 Flowchart and pseudocode for the number-doubling program
35
Programming Logic & Design, Sixth Edition
Declare Variable before they are used in the program
Variable contains garbage value
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
Some languages allow hyphens
for example, hourly-wage
Some languages allow dollar signs or other special characters
for example, hourly$
Different limits on the length of variable names
37
Programming Logic & Design, Sixth Edition
Naming Variables (continued)
Camel casing
the variable starts with a lowercase letter and any subsequent word begins with an uppercase letter, is called camel casing hourlyWage
Pascal casing
When the first letter of a variable name is uppercase, as in HourlyWage, the format is known as Pascal casing.
Variable names used throughout book
Must be one word
for example, finalBalance
Should have some appropriate meaning
38
Programming Logic & Design, Sixth Edition
Understanding Unnamed, Literal Constants and their Data Types
Numeric constant (or literal numeric constant)
Specific numeric value that does not change
Example: 43
String constant (or literal string constant)
String of characters enclosed within quotation marks
Example: Amanda
Unnamed constants
Do not have identifiers like variables do
39
Programming Logic & Design, Sixth Edition
Understanding the Data Types of Variables
1. Numeric variable
Holds digits Can perform mathematical operations on it
2. String variable
Can hold text Letters of the alphabet Special characters such as punctuation marks
Assign data to a variable
Only if it is the correct type
40
Programming Logic & Design, Sixth Edition
Declaring Named Constants
Named constant
Similar to a variable Can be assigned a value only once Assign a useful name to a value that will never be changed during a programs execution For example, if a program uses a sales tax rate of 6%, to declare a named constant as follows: num SALES_TAX = 0.06 Then use SALES_TAX in a program statement again as in the following: set taxAmount = price * SALES_TAX Unnamed constant Purpose is not immediately apparent Avoid this
Magic number
41
Programming Logic & Design, Sixth Edition
Assigning Values to Variables
Assignment statement
set myAnswer = myNumber * 2
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
Performing Arithmetic Operations
Standard arithmetic operators:
+ (plus sign)addition (minus sign)subtraction * (asterisk)multiplication / (slash)division
43
Programming Logic & Design, Sixth Edition
Performing Arithmetic Operations (continued)
Rules of precedence
Also called the order of operations Dictate the order in which operations in the same statement are carried out 1. Expressions within parentheses are evaluated first 2. Multiplication and division are evaluated next 3.
From left to right
From left to right
Addition and subtraction are evaluated next
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
Performing Arithmetic Operations (continued)
Left-to-right associativity
Operations with the same precedence take place from left to right For example, answer = a + b + c * d / e f Multiplication and division have higher precedence than addition or subtraction, so the multiplication and division are carried out from left to right as follows:
c is
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
multiplied by d, and the result is divided by e, giving a new result.
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
Performing Arithmetic Operations (continued)
Table 2-1 Precedence and associativity of five common operators
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
Procedural and object-oriented programmers approach problems differently
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.