1 / 35

Defining Control Structures

1. Defining Control Structures. All computer programs, no matter how simple or how complex, are written using one or more of three basic structures: Sequence • Selection Repetition

wilbur
Télécharger la présentation

Defining Control Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 1 Defining Control Structures • All computer programs, no matter how simple or how complex, are written using one or more of three basic structures: • Sequence • Selection • Repetition • These structures are called control structures or logic structures, because they control the flow of a program’s logic • In most programs, you will use both the selection and repetition structures

  2. 1 The Sequence Structure • The four instructions shown in Figure 1-1 are called an algorithm • An algorithm is simply the set of step-by-step instructions that accomplish a task

  3. 1 The Repetition Structure • Notice that the instruction to be repeated—in this case, walk—is indented below the repeat50 times:instruction • Indenting in this manner indicates the instructions that are part of the repetition structure, and therefore, are to be repeated

  4. 1 The Repetition Structure But what if you do not know precisely how many steps there are between Rob and the chair? • You simply must change the repetition structure’s condition • The repetition structure shown in the algorithm in Figure 1-3 ends when Rob is standing directly in front of the chair

  5. 1 Rob’s New Instruction Set • Notice that the instructions to be followed when the balloon is red—in this case, drop the balloon in the red box—and the instruction to be followed when the balloon is not red—in this case, drop the balloon in the yellow box—are indented

  6. 1 An Example of the Selection Structure • Notice that the algorithm shown in Figure 1-5 contains all three control structures: sequence, selection, and repetition

  7. 2 Solving Everyday Problems

  8. 2 The Problem-Solving Process for Creating a Computer Program

  9. 2 Analyzing the Problem • A helpful way to identify the output is to search the problem specification for an answer to the following question: What does the user want to see either printed on paper, displayed on the screen, or stored in a file? • The answer to this question typically will be stated as nouns and adjectives in the problem specification

  10. 2 IPO Charts • Programmers use an IPO (Input, Processing, Output) chart to organize and summarize the results of a problem analysis

  11. 2 IPO Charts • A helpful way to identify the input is to search the problem specification for an answer to the following question: What information will the computer need to know in order to either print, display, or store the output items? • As with the output, the input is typically stated as nouns and adjectives in the problem specification • A problem specification that contains too much information can be confusing to analyze

  12. 2 IPO Charts • If you are not sure if a piece of information is important, ask yourself this question: If I didn’t know this information, could I still solve the problem? • If your answer is “Yes”, then the information is superfluous and can be ignored

  13. 2 IPO Charts • When reading a problem specification, it helps to use a pencil to lightly cross out information that you feel is unimportant to the solution, thereby reducing the amount of information you need to consider in your analysis

  14. 2 IPO Charts

  15. 2 Completed IPO Chart

  16. 2 IPO Chart Shown with a Flowchart in the Processing Column

  17. 2 Another Way of Solving Sarah’s Problem

  18. 2 Hints for Writing Algorithms • Before you write an algorithm, consider whether the problem you are solving is similar to one you have already solved • Even if the problem you are trying to solve is not identical to one that you already solved, you may be able to use a portion of a previous solution to solve the current problem

  19. 2 IPO Chart for the Problem Specification Shown in Figure 2-13

  20. 2 Hints for Writing Algorithms

  21. 2 Hints for Writing Algorithms • If you have not solved a similar problem, and you cannot find a portion of an existing algorithm that you can use, try solving the problem manually, writing down on paper every step you take to do so

  22. 2 Desk-Checking the Algorithm • You can desk-check an algorithm using pseudocode or a flowchart

  23. 2 Desk-Checking the Algorithm

  24. 2 Desk-Checking the Algorithm

  25. 2 The Gas Mileage Problem • Figure 2-24 shows the problem specification for the gas mileage problem, which you will solve next • First, analyze the problem, looking for nouns and adjectives that represent both the output and the input

  26. 2 The Gas Mileage Problem

  27. 1 Programming Rob the Mechanical Man • Before you can complete this lesson, the Rob the Mechanical Man files must be installed on your computer’s hard disk; the installation process is described in the following eight steps • Important note: If you are working on a computer in your school’s computer lab, the files may already be installed on the computer • To install the Rob the Mechanical Man files on your computer’s hard disk refer to the procedures listed on pages 25-26 in the handout

  28. 1 Installation Message Shown in the Rob the Mechanical Man Setup Dialog Box

  29. 1 Rob and the Flower Bed

  30. 1 Programming Rob the Mechanical Man • To run the application that you will use to create Rob’s algorithm follow the directions on page 27 of the handout

  31. 1 Programming Rob the Mechanical Man • The application shown in Figure 1-13 contains two list boxes, identified by the labels Instruction Set and Algorithm • The Instruction Set list box displays the set of instructions that Rob can understand • Double-clicking an instruction in the Instruction Set list box copies the instruction to the Algorithm list box, which currently is empty • Clicking the Clear Algorithm button removes all of the instructions from the Algorithm list box, and clicking the Exit button ends the application

  32. 1 Initial Algorithm for Rob

  33. 1 Programming Rob the Mechanical Man • To enter the algorithm shown in Figure 1-15, and then verify that it works correctly, perform the steps on pages 29-30 of the handout

  34. 1 First Instruction Shown in the Algorithm List Box

  35. 1 The Completed Algorithm Shown in the Algorithm List Box

More Related