Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "BACS 287 Programming Logic 1. BACS 287 Programming Basics There are 3 general approaches to writing programs – Unstructured – Structured – Object-oriented."— Presentation transcript:

1 BACS 287 Programming Logic 1

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

3 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

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

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

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

7 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

8 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

9 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

10 Pseudocode Example Start Get temperature If temperature < 40 then Wear a coat Else Don’t wear a coat Endif Stop BACS 287

11 Flowchart Symbols

12 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

13 BACS 287 Flowchart Example

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

15 BACS 287 Sequence Construct Start X = 1 X = X + 1 Write X to screen Stop

16 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”.

17 BACS 287 Selection (If-Then-Else) Start Read X IF X > 4.0 then Print “Error” Else Print “OK” EndIF Stop

18 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”.

19 BACS 287 Selection (Case) Start Read X Select Case X Case “red” Print “Value is red” Case “blue” Print “Value is blue” Case Else Print “Error” EndCase Stop

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

21 BACS 287 Iteration (Do While) Start X = 1 Sum = 0 Do While X < 11 Sum = Sum + X X = X + 1 EndDo Print Sum Stop

22 BACS 287 Iteration (Do Until) Start X = 1 Sum = 0 Do Until X > 10 Sum = Sum + X X = X + 1 EndDo Print Sum Stop

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

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

25 BACS 287 You Try It – Example 1 Solution Start Read GPA If GPA >= 4.0 then Print President’s letter ElseIf GPA >= 3.5 then Print Dean’s letter ElseIf GPA >= 3.25 then Print Director’s letter ElseIf GPA >= 3.0 then Print Honor Roll letter EndIf Stop

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

27 BACS 287 You Try It – Example 2 Solution Start Get Loop Count X = 1 Do While X <= Loop Count Print “This is a loop” X = X + 1 EndDo Stop


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

Similar presentations


Ads by Google