bacs 287 programming logic 1. bacs 287 programming basics there are 3 general approaches to writing...

27
BACS 287 BACS 287 Programming Logic 1

Upload: chester-elliott

Post on 03-Jan-2016

219 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: BACS 287 Programming Logic 1. BACS 287 Programming Basics There are 3 general approaches to writing programs – Unstructured – Structured – Object-oriented

BACS 287

BACS 287

Programming Logic 1

Page 2: BACS 287 Programming Logic 1. BACS 287 Programming Basics There are 3 general approaches to writing programs – Unstructured – Structured – Object-oriented

BACS 287

Programming Basics

There are 3 general approaches to writing programs– Unstructured– Structured– Object-oriented

Unstructured is unacceptable. Structured programming is a good place to start

learning. Object-oriented is the most modern and is used in

more advanced courses.

Page 3: BACS 287 Programming Logic 1. BACS 287 Programming Basics There are 3 general approaches to writing programs – Unstructured – Structured – Object-oriented

BACS 287

Structured Programs

Structured programs traditionally break the problem up into modules

A module:– Performs a single, well-defined task– Is relatively small– Has a single entry and exit point– Does not interact with other modules

Structured programs tend to not use the ‘GOTO’ statement

Page 4: BACS 287 Programming Logic 1. BACS 287 Programming Basics There are 3 general approaches to writing programs – Unstructured – Structured – Object-oriented

BACS 287

Structured Programs and VB

Relatively easy to implement structured programs in Visual Basic.– Event-driven nature makes modules natural– All major structured programming constructs are

supported Since the user interface is easy to define, we

can concentrate on structuring the procedures.

Page 5: BACS 287 Programming Logic 1. BACS 287 Programming Basics There are 3 general approaches to writing programs – Unstructured – Structured – Object-oriented

BACS 287

Program Design

Good programming design dictates that modules be written using the 3 basic programming constructs.

Any procedural logic can be represented using strictly these 3 structures.

Your Visual Basic code modules should be written using this style.

Page 6: BACS 287 Programming Logic 1. BACS 287 Programming Basics There are 3 general approaches to writing programs – Unstructured – Structured – Object-oriented

BACS 287

Structured Coding Basics

Any program can be written using 3 basic constructs.– Sequence– Selection– Iteration

Visual Basic has many ways to implement these constructs.

Page 7: BACS 287 Programming Logic 1. BACS 287 Programming Basics There are 3 general approaches to writing programs – Unstructured – Structured – Object-oriented

BACS 287

Basic Programming Constructs

Sequence - Perform instructions one after the other

Selection - Make a choice and follow a logic path

– IF-THEN-ELSE– CASE

Iteration - Repeat a code segment several times

– DO WHILE– DO UNTIL

Page 8: BACS 287 Programming Logic 1. BACS 287 Programming Basics There are 3 general approaches to writing programs – Unstructured – Structured – Object-oriented

BACS 287

Procedural Logic Design Tools

Many tools exist to plan procedural logic. Two common ones will be used in this class

Pseudocode– English-like– Free form– Easy to convert to VB code

Flowchart– Graphic representation – Better for high-level overview than pseudocode

Page 9: BACS 287 Programming Logic 1. BACS 287 Programming Basics There are 3 general approaches to writing programs – Unstructured – Structured – Object-oriented

BACS 287

Pseudocode

The “Rules”– One statement per line– Indent nested structures– End all structures with appropriate terminator

‘IF’ statements with ‘ENDIF’ ‘DO’ statements with ‘ENDDO’ ‘CASE’ statements end with ENDCASE

– Use structured programming constructs

Page 10: BACS 287 Programming Logic 1. BACS 287 Programming Basics There are 3 general approaches to writing programs – Unstructured – Structured – Object-oriented

BACS 287

Pseudocode Example

Start

Get temperature

If temperature < 40 thenWear a coat

ElseDon’t wear a coat

Endif

Stop

Page 11: BACS 287 Programming Logic 1. BACS 287 Programming Basics There are 3 general approaches to writing programs – Unstructured – Structured – Object-oriented

BACS 287

Flowchart Symbols

DECISION

PRINTED OUTPUTGENERIC

INPUT / OUTPUT

PROCESS

START / STOP

TAPESTORAGE

DISKSTORAGE

FLOW LINES

Page 12: BACS 287 Programming Logic 1. BACS 287 Programming Basics There are 3 general approaches to writing programs – Unstructured – Structured – Object-oriented

BACS 287

Flowcharts

The “Rules”– Use the basic flowchart symbols (boxes)– All boxes are connected by flow lines– Flow lines do not cross other flow lines– Flow lines are vertical and horizontal, not curved or

diagonal– General flow is from top to bottom, left to right– Start with a single ‘Start’ box and end with a single ‘Stop’

box– Draw the entire chart at a consistent logical level

Page 13: BACS 287 Programming Logic 1. BACS 287 Programming Basics There are 3 general approaches to writing programs – Unstructured – Structured – Object-oriented

BACS 287

Flowchart Example

Page 14: BACS 287 Programming Logic 1. BACS 287 Programming Basics There are 3 general approaches to writing programs – Unstructured – Structured – Object-oriented

BACS 287

Sequence Construct - Example

The Problem: Initialize a variable called ‘X’ to a value of 1.

Add 1 to this value. Display the result on the screen.

Page 15: BACS 287 Programming Logic 1. BACS 287 Programming Basics There are 3 general approaches to writing programs – Unstructured – Structured – Object-oriented

BACS 287

Sequence Construct

Start

X = 1

X = X + 1

Write X to screen

Stop

START

X = 1

STOP

Display X

X = X + 1

Page 16: BACS 287 Programming Logic 1. BACS 287 Programming Basics There are 3 general approaches to writing programs – Unstructured – Structured – Object-oriented

BACS 287

Selection Construct – Example 1

The Problem: Read the value of a variable ‘X’. When the

value of ‘X’ is greater than 4, display “error”. Otherwise, display “ok”.

Page 17: BACS 287 Programming Logic 1. BACS 287 Programming Basics There are 3 general approaches to writing programs – Unstructured – Structured – Object-oriented

BACS 287

Selection (If-Then-Else)

Start

Read X

IF X > 4.0 thenPrint “Error”

ElsePrint “OK”

EndIF

Stop

START

STOP

Print "OK"

Read X

X > 4.0 Print "Error"Yes

No

Page 18: BACS 287 Programming Logic 1. BACS 287 Programming Basics There are 3 general approaches to writing programs – Unstructured – Structured – Object-oriented

BACS 287

Selection Construct – Example 2

The Problem: Read the value of a variable ‘X’. When the

value of ‘X’ is “red”, print “value is red”. When the value of ‘X’ is “blue”, print “value is blue”. When it is neither, print “error”.

Page 19: BACS 287 Programming Logic 1. BACS 287 Programming Basics There are 3 general approaches to writing programs – Unstructured – Structured – Object-oriented

BACS 287

Selection (Case)

StartRead XSelect Case X

Case “red”Print “Value is red”

Case “blue”Print “Value is blue”

Case ElsePrint “Error”

EndCaseStop

START

STOP

Print "Value is

red”

Read X

CASE X

X = “red”

Print "Value is

blue"

Print "Error"

OtherwiseX + “blue”

Page 20: BACS 287 Programming Logic 1. BACS 287 Programming Basics There are 3 general approaches to writing programs – Unstructured – Structured – Object-oriented

BACS 287

Iteration Construct – Example

The Problem: Calculate the sum of the first 10 digits (that

is, 1 through 10). When you finish, print this value.

Perform these calculations by performing an IF-THEN test at the top of the loop. Next, repeat where the IF-THEN is at the bottom of the loop.

Page 21: BACS 287 Programming Logic 1. BACS 287 Programming Basics There are 3 general approaches to writing programs – Unstructured – Structured – Object-oriented

BACS 287

Iteration (Do While)

StartX = 1Sum = 0Do While X < 11

Sum = Sum + XX = X + 1

EndDoPrint SumStop

START

STOP

X = 1Sum = 0

X < 11

Sum = Sum + X Print Sum

X = X + 1

Yes

NO

Page 22: BACS 287 Programming Logic 1. BACS 287 Programming Basics There are 3 general approaches to writing programs – Unstructured – Structured – Object-oriented

BACS 287

Iteration (Do Until)

StartX = 1Sum = 0Do Until X > 10

Sum = Sum + XX = X + 1

EndDoPrint SumStop

START

STOP

X = 1Sum = 0

X > 10

Sum = Sum + X

Print Sum

X = X + 1

YES

NO

Page 23: BACS 287 Programming Logic 1. BACS 287 Programming Basics There are 3 general approaches to writing programs – Unstructured – Structured – Object-oriented

BACS 287

Putting it All Together

Note that the 3 programming constructs are combined to solve problems.

Also note that they can be “nested” within one another.

The key is to think logically and plan out a strategy for solution that takes into account all possibilities.

Page 24: BACS 287 Programming Logic 1. BACS 287 Programming Basics There are 3 general approaches to writing programs – Unstructured – Structured – Object-oriented

BACS 287

You Try It – Example 1

The Problem: Read a students GPA. If the value is equal

to 4.0, print a “President’s letter.” If the GPA is between 3.5 and 3.99, print a “Dean’s letter.” If it is between 3.25 and 3.499, print a “Director’s letter.” If it is between 3.0 and 3.249, print a “Honor role letter.” If it is below 3.0, don’t print anything.

Page 25: BACS 287 Programming Logic 1. BACS 287 Programming Basics There are 3 general approaches to writing programs – Unstructured – Structured – Object-oriented

BACS 287

You Try It – Example 1 Solution

StartRead GPAIf GPA >= 4.0 then

Print President’s letterElseIf GPA >= 3.5 then

Print Dean’s letterElseIf GPA >= 3.25 then

Print Director’s letterElseIf GPA >= 3.0 then

Print Honor Roll letterEndIfStop

START

STOP

GPA >= 4.0Print President

Letter

Read GPA

NO

GPA >= 3.5Print President

Letter

Yes

Yes

GPA >= 3.25Print Director

LetterYes

GPA >= 3.0Print Honor Roll

LetterYes

NO

NO

NO

Page 26: BACS 287 Programming Logic 1. BACS 287 Programming Basics There are 3 general approaches to writing programs – Unstructured – Structured – Object-oriented

BACS 287

You Try It – Example 2

The Problem: Read a number. Write code to print “This is a

loop” the number of times indicated by the number. The test should be at the top of the loop. Next, do the same by putting the test at the bottom of the loop.

Page 27: BACS 287 Programming Logic 1. BACS 287 Programming Basics There are 3 general approaches to writing programs – Unstructured – Structured – Object-oriented

BACS 287

You Try It – Example 2 Solution

StartGet Loop CountX = 1Do While X <= Loop Count

Print “This is a loop”X = X + 1

EndDoStop

START

STOP

Get Loop Cnt

X <= Loop Cnt?

Print “This is a loop”

YES

NO

Initialize X to 1

Add 1 to X