.

Saturday, March 9, 2019

Prelude to Programming

As you took your offset printing step you had to figure out how to execute the succeeding(a) process honk unity prat in front of the otherwise At al or so point you did just that, and it was a major accomplishment. But this didnt get you precise far. If you wanted to walk crosswise the room, you use uped to extend this process to the following put the left derriere in front of the mightily theme Put the right nates in front of the left foot Put the left foot in front of the right foot Put the right foot in front of the left foot and so forth This is not a very efficient fashion to describe what you did.A detailed list of your actions as you ambled all over the kinsfolk would be very long. Because you did the alike(p) thing over and over, the following is a much better way to describe your actions Repeat Put the right foot in front of the left foot until you get across the room This way is short, convenient, and just as descriptive. Even if you want to bow out hundred s or thousands of steps, the process spate still be described in four lines. This is the raw material idea off lace. Walking is just wizard of many examples of curves in your daily life.For example, if you have a large family and need to prepare lunches in the morning for everyone, you apprise do the following work out a sandwich Wrap the sandwich Place the sandwich in a lunch dish place an apple in the lunch bag Place a drink in the lunch bag Continue until lunches have been made for everyone in the family Where else do you encounter a intertwineing process? How about eating a sandwich (one sunburn at a time) or brushing your teeth? If you have a de characterming discriminate on Tuesdays at 1 1100 a. M. , you go to class every Tuesday at 1 1 a. M. Until the end of the semester.You do the go to computer programing class looping until a accredited day. After you shew this chapter (one record at a time), youll be ready to place loops in your programs as well. 4. 1 A n Introduction to Repetition Structures Computers Never Get blase 165 4. 1 An Introduction to Repetition Structures Computers Never Get worldly You have already learned that all computing machine programs are created from tierce basic earns sequence, decision, and repetition. This chapter discusses repetition, which in many ways is the most heavy construct of all. We are lucky that calculators dont find repetitious lying-ins boring.Regardless of what lying-in we ask a computer to perform, the computer is virtually useless if it can perform that task completely once. The ability to adopt the same actions over and over is the most basic requirement in programming. When you use any software application, you carry to be able to open the application and do certain tasks. call up if your inter budge processor Was programmed to make your text bold provided once or if your operating system allowed you to use the copy command only once. Each computer task you perform has been coded into the software by a computer programmer and each task must have the ability to be use over and over.In this chapter, we pull up stakes examine how to program a computer to repeat one or more actions many generation. Pop fundamental principle All programming languages provide statements to create a loop. The loop is the basic component of the repetition structure. These statements are a block of code, which under certain conditions, volition be executed repeatedly. In this section, we will introduce both(prenominal) basic ideas about these structures. We will start with a simple congressman of a loop shown in font 4. 1 . This example uses a type of loop called a Repeat Until loop. Other types of loops are discussed passim the chapter. compositors case 4.Simply Writing add togethers This program segment repeatedly inputs a consequence from the exploiter and unwraps that progeny until the exploiter images O. The program then displays the lyric List annihilat eed. 2 3 5 6 7 harbor Number As whole number save Please enter a mo Input Number deliver Number Until Number Write List Ended In the pseudopodia, the loop begins on line 2 with the phrase Repeat and ends on line 6 with Until Number O. The loop remains is contained in lines 3, 4, and 5. These are the statements that will be executed repeatedly. The body of a loop is executed until the analyze condition following the word Until on line 6 becomes true.In this case, the scrutiny condition becomes true when the user types a O. At that point, the loop is exited and the statement on line 7 is executed. 1 66 What Happened? Lets trace the death penalty of this program, assuming that the user enters the poem 1, 3, and O, in that order When execution begins the loop is entered, the number 1 is input, and this number is displayed. These actions make up the first pass by the loop. The test condition, Number = O? is now tested on line 6 and ready to be false because at this point, Number =-? Therefore, the loop is entered again.The program execution returns to line 2 ND the body of the loop is executed again. (Recall that the picture matchs sign, -?2, is a comparison operator and asks the question, Is the lever of the shifting Number the same as O? ) On the second pass by the loop, the number 3 is input (line 4) and displayed (line 5), and once again the condition (line 6), Number = = O is false. So the program returns to line 2. On the third pass through the loop, the number O is input and displayed. This time the condition Number == O is true, so the loop is exited and execution transfers to line 7, the statement after the loop.The quarrel List Ended are displayed and the program is complete. Iterations We have said that the loop is the basic component of the repetition structure. One of the main reasons a computer can perform many tasks efficiently is because it can quickly repeat tasks over and over. The number of times a task is repeated is unend ingly a significant part of any repetition structure, but a programmer must be aware of how many times a loop will be repeated to ensure that the loop performs the task correctly. In computer lingo a single pass through a loop is called a loop iteration.A loop that executes three times goes through three iterations. Example 4. 2 presents the iteration process. Example 4. 2 How Many Iterations? This program segment repeatedly asks the user to input a name until the user enters Done. Declare design As String Write Enter the name Of your brother or baby Input Name Write Name until Name Done and Elizabeth Drake. Published by Addison-Wesley. procure C 2011 by This pseudopodia is almost the same as shown in Example 4. 1 except that the input in this example is draw and quarter entropy instead Of integer data.The loop begins on line 2 with the word Repeat and ends on line 6 with until Name Done. The loop body is contained in lines 3, 4, and 5. How are the iterations counted? Each tim e these statements are executed, the loop is said to have gone through one iteration. 167 Lets assume this program segment is used to enter a list of a users brothers and sisters. If browbeat has two brothers named Joe and Jim and one sister named Ellen, the loop would complete four iterations. Joe would be entered on the first iteration, Jim on the next iteration, Ellen on the third iteration, and the word Done would be entered on the fourth iteration.If Marie, on the other hand, had only one sister named Anne, the program would go through two iterations-?one to enter the name Anne and one to enter the word Done. And if Bobby were an only child, the program would only complete one iteration since Bobby would enter Done on the first iteration. Later in this chapter, We will see how to create a loop that does not require that the test condition count as one of the iterations. Beware of the Infinite Loop In Example 4. 1 , we saw that the user was prompted to enter any number and tha t number would be displayed on the screen.Fifth user started with the number 234789 and worked his way down, entente 234,788, then 234,787, and so forth, the computer would display 234,790 numbers (including the O that terminates the loop). However, after the user entered the dwell number, O, the loop would end. It would be a lot of numbers, but it would end. On the other hand, what would happen if the loop was written as shown in Example 4. 3? Example 4. 3 The Dangerous Infinite Loop In this example, we change the test condition of Example 4. 1 to a condition that is impractical to achieve. The user is asked to enter a number on line 2 and line 3 takes in the users input.Line 4 sets a new variable, Computerized equal to that number plus one. The loop will continue to ask for and display numbers until the look on of Number is greater than Computerized. That condition will neer be met because on each pass through the loop, regardless of what number the user enters, Computerized w ill always be one greater. Thus, the loop will repeat and repeat, continually asking for and displaying numbers. 8 Declare Number, Computerized As Integer Write Please enter a number Computerized = Number + 1 Until Number Computerized Write The End When will it end?Never. The words The End will never be displayed. If, as shown in Example 4. 3, a loops test condition is never satisfied, then the loop will never be exited and it will become an infinite loop. Infinite loops can malodour and Elizabeth Drake. Published by Addison-Wesley. Copyright 2011 by 168 havoc on a program, so when you set up a loop and put in a test condition, be sure that the test condition can be met. Computers dont mind doing a task many times, but forever is simply too many Dont Let the User Get confine in a Loop There is one more important point to mention about Examples 4. And 4. 2. In both of these examples, we have test conditions that can easily be met. As soon as a user enters O for the number in Ex ample 4. 1, the loop ends. As soon as the user enters the word Done in Example 4. 2, the loop ends. But owe would the user know that O or Done is the cue for the program segment to end? It is important for the programmer to make it clear, by means of a fitting prompt, how the user will terminate the action of the loop. In Example 4. 1, the following would be a suitable prompt Write Enter a number enter O to quit. In Example 4. 2, the following would be a suitable prompt Write Enter the name of your brother or sister Write Enter the word Done to quit. In the type of loops we used in these two examples, the loop continues until the user ends it. Other loops end without user input. Regardless of what type f loop you write, you always want to forefend the possibility that the loop will continue without end. Therefore, you must ensure that the test condition can be met and, if the user must enter something superfluous to end the loop, be sure its clear.Relational and Logical Operator s The condition that determines whether a loop is reentered or exited is usually constructed with the help Of relational and logical operators. We will concisely review these operators here. The following are the six standard relational operators and the programming symbols we will use in this book to represent them equal to (or is the same as) to equal to less than greater than or equal to = prelim to Programming Concepts and Design, Fifth Edition, by Stewart Event All six operators can be applied to either numeric or character string data.Note that the double equals sign, the comparison operator (==) is different from the assignment operator speckle the assignment operator assigns the value on the right side of the equals sign to the variable on the left side, the comparison operator compares the values of the variable or expression on the left side of the operator to the value of the variable, expression, number, r text on the right side. It returns only a value of false (if the two values are different) or true (if the two values are the same).

No comments:

Post a Comment