4.1 - Repetition Introduction Repetition is one of the three basic programming structures. Simply defined, a repetition structure is a tool to make the computer repeat a single command or series of commands. Repetition is often described as looping and that word will often be used in this text. Once you have mastered the type of structures in Visual Basic you will be able to understand their use in other computer programming languages. Skills Required In order to work through this unit, you will need to be able to: work within the Visual Basic environment define and use variables correctly use the IF and SELECT CASE selection structures use a vertical scroll bar New Skills In this unit you will introduced to three types of repetition structures and some introductory graphics skills. Outcomes By the end of this unit students you should be able to: 1. use the FOR...NEXT structure; 2. use the WHILE...WEND structure; 3. use the DO...LOOP structure 4. use a Timer Control as a repetition structure 5. identify as general ranges the number limits of integer, long, single and double variable types. 6. define the terms twip, pixel and point 7. describe how graphics are positioned on a Visual Basic form 8. import graphics into a program 9. move graphics on the screen using a method and a property 10. use a combo box control 11. use the change event of a control 12. use the form load event 13. assess a programming situation and choose the appropriate programming structure Description The use of repetition or structures that repeat is one of the structures that taps the power of computers. Computers often make the possible impossible by carrying out many operations at an incredible speed and this often involves repetitive calculations and therefore requires a repetition or looping structure. There are three types of looping structures that you will study here: 1. Counter controlled loops 2. Condition controlled loops 3. Timer controlled loops. We use all of these in our everyday lives: The instructions on a shampoo bottle to wash, rinse and then repeat the process is an example of a counter controlled loop. Stirring a can of paint until the colour is mixed in is an example of a condition controlled loop. A grandfather clock that chimes every hour is an example of a timer controlled loop. Loop structures can be very simple or more complex as you will see in the examples that follow. You will develop the skills to use each type of repetition structure and as well will learn how to choose the best structure for a given question. You will as well learn the Visual Basic graphics system. Activity 4.1.1 - Repetition Type Identification In your notebooks, identify each of the following as one of a counter (CTR), condition (CON) or time (T) controlled loop. When starting the car push the gas pedal twice before turning the key. Cut the lawn every weekend Practise at the piano until you get the song right Practise your guitar lessons every day Read from page 50 to page 76 tonight for homework Polish your shoes until they shine Make up an three everyday examples of looping, one for each type of repetition. Activity 4.1.2 - Counter Controlled Repetition If a person was asked to add the numbers from 1 to 10 without access to a calculator they might write 1+2+3+4+5+6+7+8+9 or do the question in their head. This would become more complicated if they were asked to add the numbers from 531 to 1,673. In this section we will look at how to teach a computer to do this kind of task as a counter controlled repetition. In a computer the kind of activity like summing a set number of numbers would require a counter controlled repetition. This simply means repeating an action or actions a set number of times. If you had a old television that only worked when you hit it three times on the right side your instruction to someone else who was going to watch it might be "Hit it 3 times on the right side". The instruction for the computer would be more like: Counting from 1 to 3 Hit the television on the right side Next count The first statement "Counting 1 to 3" indicates that the counting will start at 1 and end at 3. The next line, "Hit the ..." is indented to show that it is the action being repeated and the last line completes the structure. If we go back to the original question, adding the numbers from 1 to 10, the actual Visual Basic program would look like this: Dim sum as integer sum = 0 For ctr=1 to 10 sum=sum + ctr Next ctr After this is run the variable sum would hold the sum of the 10 numbers and it could be output to the screen or used in a program as the programmer wished. The following table explains each line of the For...Next loop. Command Explanation For ctr=1 to 10 The loop will start the variable at 1, and stop when it is 10, it is assumed that 1 will be added to the variable each time through the loop. sum=sum + ctr The current number (ctr) is added to the sum Next ctr The loop ends here, control is passed back to the For command Write a For...Next for each of the following sums:. sum the numbers from 100 to 500 sum the odd numbers from 51 to 100 sum every 7th number from 5 to 1 073 sum the numbers from 200 to 100 (in that order, you can use a negative step) Activity 4.1.3 - Counter Controlled Repetition II Computing Factorials A factorial is the product of a number and the numbers that are smaller than the number. The factorial of 5 would be 5x4x3x2x1. The symbol for factorial is the exclamation mark, 6! would be read "six factorial". Construct a simple program that would accept as input an integer, calculate the factorial of the number and display the factorial in a second text box. A sample screen design is shown below. Dimension all of your local variables as integers. Once you have written the program, test it with 3!, the answer the should 6. Try the numbers 4 to 10 and record your results. Your will get an overflow number for several of these numbers. Overflow simply means that the number cannot be represented with the given variable type. Integer overflow occurs at 32, 768. Once overflow occurs, change the variable type of the result to Long and test to see where overflow occurs. Once you have found the limit, repeat for single and double to determine their overflow limits. HINT: The help for Data Type Summary will be helpful. Save your file as Factorial