380 likes | 757 Vues
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
E N D
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
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
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
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
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
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
2 Solving Everyday Problems
2 The Problem-Solving Process for Creating a Computer Program
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
2 IPO Charts • Programmers use an IPO (Input, Processing, Output) chart to organize and summarize the results of a problem analysis
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
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
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
2 IPO Charts
2 Completed IPO Chart
2 IPO Chart Shown with a Flowchart in the Processing Column
2 Another Way of Solving Sarah’s Problem
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
2 IPO Chart for the Problem Specification Shown in Figure 2-13
2 Hints for Writing Algorithms
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
2 Desk-Checking the Algorithm • You can desk-check an algorithm using pseudocode or a flowchart
2 Desk-Checking the Algorithm
2 Desk-Checking the Algorithm
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
2 The Gas Mileage Problem
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
1 Installation Message Shown in the Rob the Mechanical Man Setup Dialog Box
1 Rob and the Flower Bed
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
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
1 Initial Algorithm for Rob
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
1 First Instruction Shown in the Algorithm List Box
1 The Completed Algorithm Shown in the Algorithm List Box