pseudo code basics

29
PLANNING TOOLS

description

This will help you to learn the basics about pseudo code

Transcript of pseudo code basics

Page 1: pseudo code basics

PLANNING TOOLS

Page 2: pseudo code basics

Planning Tools

• ALGORITHMS

• FLOW CHARTS

• PSEUDO CODE

• DECISION TABLES

Page 3: pseudo code basics

Pseudocode

• It Means:

• IMITATION or FALSE CODE

• It is an imitation of the computer instruction

• Using this programmer can concentrate on developing logic without worrying about syntax

• Easy to convert into programming language

Page 4: pseudo code basics

Writing PseudocodeBasic computer operations

There are six basic computer operations1. computer can receive information2. computer can put out information3. computer can perform arithmetic4. computer can assign a value to a variable or

memory location5. computer can compare two variables and select

one of two alternate actions6. computer can repeat a group of actions

Page 5: pseudo code basics

5

Six Basic Computer Operations

1 A computer can receive information– When a computer is required to receive

information or input from a particular source, whether it is a terminal, a disk or any other device, the verbs Read and Get are used in pseudocode

Read => Input from a record

Get => Input from keyboard

Example pseudocode

1.Read student name2.Get system data3.Read number1, number24.Get tax_code

Page 6: pseudo code basics

6

Six Basic Computer Operations

2 A computer can put out information– When a computer is required to supply

information or output to a device, the verbs Print, Write, Put, Output, or Display are used in pseudocode

– Print => send output to printer– Write => send out to file– Put, Output, Display => send

to screen

Example pseudocode1.Print ‘Program Completed’2.Write customer record to master file3.Output total tax4.Display ‘End of data’

Page 7: pseudo code basics

7

Six Basic Computer Operations

3 A computer can perform arithmetic– Most programs require the computer to perform some sort of

mathematical calculation, or formula, and for these, a programmer may use either actual mathematical symbols or the words for those symbols

– To be consistent with high-level programming languages, the following symbols can be written in pseudocode:+ for Add - for Subtract* for Multiply / for Divide ( ) for Parentheses

– When writing mathematical calculations for the computer, standard mathematical ‘order of operations’ applies to pseudocode and most computer languages

Page 8: pseudo code basics

8

Six Basic Computer Operations

4 A computer can assign a value to a variable or memory location

– There are three cases where you may write pseudocode to assign a value to a variable or memory location:

1. To give data an initial value in pseudocode, the verbs Initialize or Set are used

2. To assign a value as a result of some processing the symbols ‘=‘ or ‘’ are written

3. To keep a variable for later use, the verbs Save or Store are used

Page 9: pseudo code basics

9

Six Basic Computer Operations

4 A computer can assign a value to a variable or memory location

Example pseudocode

1.Initialize total_price to zero2.Set student_count to zero3.Total_price = cost_price + sales_tax4.Total_price cost_price + sales_tax5.Store customer_num in last_customer_num

Page 10: pseudo code basics

10

Six Basic Computer Operations

5 A computer can compare two variables and select one or two alternate actions

– An important computer operation available to the programmer is the ability to compare two variables and then, as a result of the comparison, select one of two alternate actions

– To represent this operation in pseudocode, special keywords are used: IF and ELSE

Page 11: pseudo code basics

The Selection Structure

amount < 100

interestRate = .06 interestRaate = .10

yes no

1. IF amount < 1001.1 interestRate = .06

2. ELSE2.1 Interest Rate = .10Pseudocode

Page 12: pseudo code basics

12

Six Basic Computer Operations

6 A computer can repeat a group of actions

– When there is a sequence of processing steps that need to be

repeated, a special keyword, WHILE is used in pseudocode

– The condition for the repetition of a group of actions is

established in the WHILE clause, and the actions to be

repeated are listed beneath it

Page 13: pseudo code basics

Repetition using WHILEStart

count = 0

count <10

add 1 tocount

write count

Write“The End”

Stop

1. count = 02. WHILE count < 10

2.1 ADD 1 to count2.2 WRITE count

3. WRITE “The End”

Mainline

1.count = 0

2.DOWHILE count < 10

2.1 DO Process

3.WRITE “The End”

Process

2.1 ADD 1 to count

2.2 WRITE count

Modular

Page 14: pseudo code basics

Rules for Pseudocode

• Write only one statement per line

• Capitalize initial keyword

• Indent to show hierarchy

• End multiline structures

• Keep statements language independent

Page 15: pseudo code basics

One Statement Per Line

Each statement in pseudocode should express just one action for the computer.

PseudocodeREAD name, hoursWorked, payRate

gross = hoursWorked * payRate

WRITE name, hoursWorked, gross

Page 16: pseudo code basics

Capitalize Initial Keyword

In the example below note the words: READ and WRITE. These are just a few of the keywords to use, others include:

READ, WRITE, IF, ELSE, ENDIF, WHILE, ENDWHILE

Pseudocode

READ name, hoursWorked, payRate

gross = hoursWorked * payRate

WRITE name, hoursWorked, gross

Page 17: pseudo code basics

Rules for Variable Names

• Begin with lowercase letter

• Contain no spaces

• Additional words begin with capital

• Unique names within code

• Consistent use of names

Page 18: pseudo code basics

Indent to Show Hierarchy

• Sequence: Keep statements in sequence all starting in the same column

• Selection:Indent statements that fall inside selection structure, but not the

keywords that form the selection

• Loop:Indent statements that fall inside the loop but not keywords that

form the loop

Each design structure uses a particular indentation pattern

READ name, grossPay, taxes

IF taxes > 0

net = grossPay – taxes

ELSE

net = grossPay

ENDIF

WRITE name, net

Page 19: pseudo code basics

End Multiline Structures

See the IF/ELSE/ENDIF as constructed above, the ENDIF is in line with the IF.

The same applies for WHILE/ENDWHILE etc…

READ name, grossPay, taxes

IF taxes > 0

net = grossPay – taxes

ELSE

net = grossPay

ENDIF

WRITE name, net

Page 20: pseudo code basics

Types of Logic Structure

• Sequence

• Selection

• Iteration

Page 21: pseudo code basics

Sequence

• Performing instruction one after another

Page 22: pseudo code basics

The Selection Structure

amount < 100

interestRate = .06 interestRate = .10

yes no

IF amount < 100

interestRate = .06

ELSE

Interest Rate = .10

ENDIF

Pseudocode

Page 23: pseudo code basics

The Looping Structure

In flowcharting one of the more confusing things is to separate selection from looping. This is because each structure use the diamond as their control symbol. In pseudocode we avoid this by using specific keywords to designate loopingWHILE/ENDWHILE

REPEAT/UNTIL

Page 24: pseudo code basics

WHILE / ENDWHILEStart

count = 0

count <10

add 1 tocount

write count

Write“The End”

Stop

count = 0

WHILE count < 10

ADD 1 to count

WRITE count

ENDWHILE

WRITE “The End”

Mainline

count = 0

WHILE count < 10

DO Process

ENDWHILE

WRITE “The End”

Process

ADD 1 to count

WRITE count

Modular

Page 25: pseudo code basics

REPEAT / UNTILStart

count = 0

count <10

add 1 tocount

write count

Write“The End”

Stop

count = 0

REPEAT

ADD 1 to count

WRITE count

UNTIL count >= 10

WRITE “The End”

Mainline

count = 0

REPEAT

DO Process

UNTIL count >= 10

WRITE “The End”

Process

ADD 1 to count

WRITE count

Modular

Page 26: pseudo code basics

Advantages & Disadvantages

Flowchart Advantages: Standardized Visual

Pseudocode Advantages Easily modified Implements structured

concepts Done easily on Word

Processor

Flowchart Disadvantages: Hard to modify Structured design elements not

implemented Special software required Time Consuming

Pseudocode Disadvantages: Not visual No accepted standard, varies from

company to company

Page 27: pseudo code basics

Working with Fields

Calculations

+ add

- subtract

* multiply

/ divide

** or ^ exponentiation

( ) grouping

Selection

> greater than

< less than

= equal to

>= greater than or equal to

<= less than or equal to

!= not equal to

Page 28: pseudo code basics

Any Questions

Page 29: pseudo code basics