270 likes | 469 Views
BACS 287. Programming Logic 1. 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.
E N D
BACS 287 Programming Logic 1 BACS 287
Programming Basics • There are 3general 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. 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 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. 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. 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. 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 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 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 BACS 287
Pseudocode Example Start Get temperature If temperature < 40 then Wear a coat Else Don’t wear a coat Endif Stop BACS 287
Flowchart Symbols 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 BACS 287
Flowchart Example 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. BACS 287
Sequence Construct Start X = 1 X = X + 1 Write X to screen Stop 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”. BACS 287
Selection (If-Then-Else) Start Read X IF X > 4.0 then Print “Error” Else Print “OK” EndIF Stop 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”. 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 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. BACS 287
Iteration (Do While) Start X = 1 Sum = 0 Do While X < 11 Sum = Sum + X X = X + 1 EndDo Print Sum Stop BACS 287
Iteration (Do Until) Start X = 1 Sum = 0 Do Until X > 10 Sum = Sum + X X = X + 1 EndDo Print Sum Stop 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. 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. 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 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. 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 BACS 287