Presentation is loading. Please wait.

Presentation is loading. Please wait.

P ROGRAMMING L OGIC GWDA123 Sharon Kaitner, M.Ed. Winter 2015: Week 2.

Similar presentations


Presentation on theme: "P ROGRAMMING L OGIC GWDA123 Sharon Kaitner, M.Ed. Winter 2015: Week 2."— Presentation transcript:

1 P ROGRAMMING L OGIC GWDA123 Sharon Kaitner, M.Ed. Winter 2015: Week 2

2 Understanding structure 1 Sequence, Selection, Loop 2 Unstructured Logic 3 Today’s Overview

3 Learning Objectives Explain structure, sequences, selection and loops

4 L ECTURE : U NDERSTANDING STRUCTURE

5 Structure From your homework week 1, you should have understood how important it is to write all programs logically, and to think of every step that would need to be included, and in what sequence. Imagine a program more complicated than your favorite recipe and all the steps.

6 Structure Think of all the decisions and possibilities involved in a software like PhotoShop Designing the logic would be very time consuming It would be easy to create a mess Logically snarled program statements are called spaghetti code

7 Structure If a program is using spaghetti code logic, we call them unstructured programs; they are not following the rules of logic

8 Structure Think of explaining to someone unfamiliar with the process how to brush their teeth How important is sequence? How important is each step?

9 L ECTURE : S EQUENCE, SELECTION, LOOP

10 Structure Three basic structures: – Sequence – Selection – Loop Any program, no matter how complicated can, be constructed using one or more of the three

11 Structure A structure is a basic unit of programming logic and consists of either a sequence, selection, or loop You can diagram any task with these 3

12 Structure A selection structure asks a question, then depending on the answer, one of two course of actions is performed No matter which path selected, you continue with the next task This is an if-then-else statement

13 Structure A loop structure repeats actions as long as a condition remains true The action(s) occurring within the loop is known as the loop body Some programmers call this a while..do, or a while loop

14 Structure Commonly, a condition is evaluated and if the result is true, you execute the loop body If it is still true, you execute again It continues until the condition is false Programmers call this repetition or iteration

15 Structure These three – sequence, selection, loop – can be combined in any number of ways Each structured program’s segments would be either a sequence, selection or loop Each structure has one entry and one exit

16 Understanding Structure A structured program has the following characteristics: – Only includes a combination of one of the three basic structures – Can be stacked or connected at only one entry or exit point – Any structure can be nested within another

17 Understanding Structure Attaching structures end to end is called stacking structures End-structure statements use endif or endwhile statements Endif shows where the actions will end if condition isn’t meant The instructions occur when if is tested true

18 Structure Any statement that follows the endif statement is outside the decision structure Similarly, endwhile statements show where a loop structure ends Any statements following would be outside of the loop, not part of it

19 Structure When writing pseudocode, the convention is to align an if with its else then indent any dependent statements a few spaces You would also align a while and an endwhile pair

20 Structure Besides stacking structures, you can take individual tasks or steps and replace them with additional structures This is called nesting structures A group of statements that executes as a single unit is called a block

21 Understanding Structure For a program to be structured and work the way you want, you sometimes need to add extra steps A priming input or priming read is the statement that reads the first value in a program It is one kind of added step

22 Understanding Structure Possible combinations of logical structures are endless but each of a structured program’s segments is a sequence, a selection, or a loop Structured, the flow of logic returns to the eof question Asking yourself this can help determine if your logic is structured

23 Understanding Structure Remember chapter one’s doubling problem? The input originalNumber was a case of priming input Priming gets the process going Its purpose is to control the loop that begins with the eof question

24 Understanding Structure Keeping within the sequence, selection, and loop structures creates – Clarity, to make large programs manageable – Professionalism, it is the way it is done – Efficiency, most newer computer languages are structured and use syntax to sequence, select and loop

25 Understanding Structure Keeping within the sequence, selection, and loop structures creates – Maintenance, for you and others to modify and maintain – Modularity, as they can be easily broken down into routines or modules

26 Recognizing structure and unstructured logic – Is it constructed from one of the three basic structures? – Does each task follows to the next? – Either through a flowchart or by writing the pseudocode, you should see alternating sequences, but always one of the three basic

27 Discussion As a group, using the whiteboard, write the pseudocode and flowchart to wash a dog

28 Homework Write the pseudocode for Ex 2 a-e, and read, Chapter 2. Your solutions will be presented to the class next week


Download ppt "P ROGRAMMING L OGIC GWDA123 Sharon Kaitner, M.Ed. Winter 2015: Week 2."

Similar presentations


Ads by Google