Docstoc

PROGRAMMING – REPETITION

Document Sample
PROGRAMMING – REPETITION Powered By Docstoc
					                     PROGRAMMING – REPETITION

   To demonstrate the different means of implementing repetition we shall use a
   number of examples.

   Programming Example 1 – Table of Squares:
   This program will produce a tabular display of the first 10 square numbers and
   also provide the option of producing a hard copy on a printer.

   Create the following in VB:



                                                                        cmdDisplay

picDisplay                                                              cmdClear


                                                                        cmdPrint




                                                                        cmdExit



   Write the code and add relevant comments:
Points to Note:
   • the For Next loop is referred to as a counted loop because the actions
       are executed a set number of times (in this case 10). The syntax for this
       loop is:
              For counter = start to end Step increment
                    Statements
              Next counter
   • as with if..then..else statements it is desirable to use indentation to aid
       understanding
   • Print is a method that allows us to send the specified object/expression to
       the printer
   • EndDoc terminates a document sent to a printer
   • Cls is a method which clears all text and graphics on a picture box or form
   • MsgBox displays a message in a dialogue box and waits for the user to
       press a button.
   Programming Example 2 – Word Count:
   This program will count the number of words in a sentence entered by the user.

   Create the following in VB:



                                                                            cmdCount


txtInput                                                                    cmdClear




                                                                            cmdExit

lblCount

   Write the code and add comments:




   Save and run the program. Try to “trick” the program.
      Some points to note:
        • this example uses some string functions to process text data. As with all
           functions they return a value. The Len function returns an integer which is
           the number of characters in a string. The Mid function returns a string
           which is part of the source string eg Mid(“Awareness”, 2, 3) returns ‘war’ –
           it starts at the second character and copies three characters.
        • Left and Right will return a specified number of characters counting either
           from left or right.



      Programming Example 3 – Hit Songs:
      This application will search a list for a recording artist/group and provide the
      name of a hit song.

      Create the following in VB




                                                                               cmdClear


                                                                               lstSongs

lstNames
                                                                               cmdSong


                                                                               lblSong


                                                                              cmdExit
Now add the code and comments:




Save and run your program, typing in the name of an artist in the search box.
(Make sure you have copied hits.dat form directory into your h drive and change
path in code to reflect this!!!!!)

				
DOCUMENT INFO
Shared By:
Categories:
Stats:
views:42
posted:5/4/2010
language:English
pages:5
Description: PROGRAMMING – REPETITION