+ All Categories
Home > Documents > Programming

Programming

Date post: 01-Nov-2014
Category:
Upload: annt-phyo-thwin
View: 109 times
Download: 3 times
Share this document with a friend
Description:
Programming Logic and DesignPseudocode and Flow Chart
Popular Tags:
48
Programming Logic and Design Sixth Edition Chapter 1 An Overview of Computers and Programming
Transcript
Page 1: Programming

Programming Logic and DesignSixth Edition

Chapter 1An Overview of Computers andProgramming

Page 2: Programming

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

Programming Logic & Design, Sixth Edition

2

Page 3: Programming

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

Programming Logic & Design, Sixth Edition

3

Page 4: Programming

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

Programming Logic & Design, Sixth Edition

4

InputProcessin

gOutput

Page 5: Programming

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, Computer’s temporary, internal

storage

Programming Logic & Design, Sixth Edition

5

Page 6: Programming

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

Programming Logic & Design, Sixth Edition

6

Page 7: Programming

Understanding Simple Program Logic•Program with syntax errors cannot

execute1.Logical errors

▫Errors in program logic ▫Produce incorrect output as a result

2.Logic of the computer program▫Sequence of specific instructions in specific

order3.Variable

▫Named memory location whose value can vary

Programming Logic & Design, Sixth Edition

7

Page 8: Programming

Understanding the ProgramDevelopment Cycle•Program development cycle

1. Understand the problem2. Plan the logic3. Code the program4. Use software (a compiler or interpreter)

to translate the program into machine language

5. Test the program6. Put the program into production7. Maintain the program

Programming Logic & Design, Sixth Edition

8

Page 9: Programming

Understanding the ProgramDevelopment Cycle (continued)

Programming Logic & Design, Sixth Edition

9

Figure 1-1 The program development cycle

Page 10: Programming

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 input-

processing-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.

Programming Logic & Design, Sixth Edition

10

Page 11: Programming

2. Planning the Logic• Heart of the programming process (Programming Logic and Design)• During this phase of the process, the programmer

▫ plans the steps of the program, deciding what steps to include and how to order them.

• Most common planning tools 1.Flowcharts 2.Pseudocode

• Desk-checking▫ 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 program’s logic on paper before you actually write the program is called desk-checking.

Programming Logic & Design, Sixth Edition

11

Page 12: Programming

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 commands—in other words, using the correct syntax.

Programming Logic & Design, Sixth Edition

12

Page 13: Programming

4. Using Software to Translate the Program into Machine Language• Why we need software (program coding) to

translate?▫ Each computer knows only one language: its

machine language, which consists of 1s and 0s.• Translator program

▫ Called “Compiler” or “interpreter”▫ Changes the programmer’s 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.

• Syntax error▫ Misuse of a language’s grammar rules▫ Programmer corrects listed syntax errors▫ Might need to recompile the code several times to

get the executable program

Programming Logic & Design, Sixth Edition

13

Page 14: Programming

4. Using Software to Translate the Program into Machine Language (continued)

Figure 1-2 Creating an executable program

Programming Logic & Design, Sixth Edition

14

Page 15: Programming

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

Programming Logic & Design, Sixth Edition

15

Page 16: Programming

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 user’s request for a special list.

• Process depends on program’s 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.

Programming Logic & Design, Sixth Edition

16

Page 17: Programming

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

Programming Logic & Design, Sixth Edition

17

Page 18: Programming

Which statements are true?

MCTS Guide to Microsoft Windows Server 2008 Network Infrastructure Configuration

18

Page 19: Programming

Which statements are true?

MCTS Guide to Microsoft Windows Server 2008 Network Infrastructure Configuration

19

Page 20: Programming

Which statements are true?

MCTS Guide to Microsoft Windows Server 2008 Network Infrastructure Configuration

20

Page 21: Programming

Step 1 : Understanding the ProblemStep 2 : Planning the Logic

MCTS Guide to Microsoft Windows Server 2008 Network Infrastructure Configuration

21

Page 22: Programming

Using Pseudocode Statementsand 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

Programming Logic & Design, Sixth Edition

22

Page 23: Programming

Writing Pseudocode• Pseudocode representation involves

▫ writing down all the steps you will use in a program.▫ Pseudocode representation of a number-doubling

problem

▫ beginning statement like start and end it with a terminating statement like stop

▫Flexible because it is a planning tool

Programming Logic & Design, Sixth Edition

23

startinput myNumberset myAnswer = myNumber * 2output myAnswer

stop

Page 24: Programming

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

Programming Logic & Design, Sixth Edition

24

Page 25: Programming

Drawing Flowcharts• Input symbol

▫ Indicates input operation▫Parallelogram

• Processing symbol▫Processing statements such as

arithmetic▫Rectangle

• Output symbol▫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.

MCTS Guide to Microsoft Windows Server 2008 Network Infrastructure Configuration

25

Page 26: Programming

Drawing Flowcharts (continued)•Flowlines

▫Arrows that connect steps

•Terminal symbols▫Start/stop symbols▫Shaped like a

racetrack▫Also called lozenge

Programming Logic & Design, Sixth Edition

26

Page 27: Programming

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.

MCTS Guide to Microsoft Windows Server 2008 Network Infrastructure Configuration

27

Page 28: Programming

Drawing Flowcharts (continued)

Figure 1-6 Flowchart and pseudocode of program that doubles a number

Programming Logic & Design, Sixth Edition

28

Page 29: Programming

Which statements are true?

MCTS Guide to Microsoft Windows Server 2008 Network Infrastructure Configuration

29

Page 30: Programming

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

memory1.Variables2.Literals, or unnamed constants3.Named constants

Programming Logic & Design, Sixth Edition

30

Page 31: Programming

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 variable’s name▫A data item’s 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

Programming Logic & Design, Sixth Edition

31

Page 32: Programming

Declaration• Computers deal with two basic types of data—text and numeric.

Two data types will be used: 1. num2. string.

• For example, a valid declaration:

• 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.

MCTS Guide to Microsoft Windows Server 2008 Network Infrastructure Configuration

32

num mySalarynum yourSalary = 14.55string myNamestring yourName = "Juanita"

Data Type

Identifier (variable

name)Value

(a starting value)

initializing

Page 33: Programming

Working with Variables (continued)•Initializing a variable

▫Declare a starting value for any variable•Garbage

▫Variable’s 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 variable’s unknown value is commonly called garbage.

•Variables must be declared before they are used in a program for the first time.

Programming Logic & Design, Sixth Edition

33

Page 34: Programming

Working with Variables (continued)

Figure 2-1 Flowchart and pseudocode for the number-doubling program

Programming Logic & Design, Sixth Edition

34

Page 35: Programming

Programming Logic & Design, Sixth Edition

35

Figure 2-2 Flowchart and pseudocode of number-doubling program with variable declarations

Variable contains

garbage value

Declare Variable before they are used in the

program

Page 36: Programming

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

Programming Logic & Design, Sixth Edition

36

Page 37: Programming

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

Programming Logic & Design, Sixth Edition

37

Page 38: Programming

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

Programming Logic & Design, Sixth Edition

38

Page 39: Programming

Understanding the Data Types of Variables1. 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

Programming Logic & Design, Sixth Edition

39

Page 40: Programming

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 program’s 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

• Magic number ▫ Unnamed constant▫ Purpose is not immediately apparent▫ Avoid this

Programming Logic & Design, Sixth Edition

40

Page 41: Programming

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

Programming Logic & Design, Sixth Edition

41

Page 42: Programming

Performing Arithmetic Operations•Standard arithmetic operators:

▫+ (plus sign)—addition▫− (minus sign)—subtraction▫* (asterisk)—multiplication▫/ (slash)—division

Programming Logic & Design, Sixth Edition

42

Page 43: Programming

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 out1.Expressions within parentheses are evaluated first2.Multiplication and division are evaluated next

From left to right3.Addition and subtraction are evaluated next

From left to right• 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)

Programming Logic & Design, Sixth Edition

43

Page 44: Programming

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 multiplied by d, and the result is divided by e, giving a new

result.▫ 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 added, the temporary result is added, and then f is

subtracted. The final result is then assigned to answer.▫ 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

Programming Logic & Design, Sixth Edition

44

Page 45: Programming

Performing Arithmetic Operations (continued)

Table 2-1 Precedence and associativity of five common operators

Programming Logic & Design, Sixth Edition

45

Page 46: Programming

Summary• Computer programming

▫Requires specific syntax▫Must develop correct logic

• Programmer’s job▫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

Programming Logic & Design, Sixth Edition

46

Page 47: Programming

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.

Programming Logic & Design, Sixth Edition

47

Page 48: Programming

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.

MCTS Guide to Microsoft Windows Server 2008 Network Infrastructure Configuration

48


Recommended