Wednesday, February 9, 2011

Visual Basic 6.0 - Session 9

In this tutorial, we look at writing code containing the 5 basic control structures.
•  sequence
•  binary selection
•  multiway selection
•  pre-test iteration
•  post-test iteration

The IPO chart and pseudocode algorithm for finding the average of some numbers entered from the keyboard, using a post-test loop, might look like this:


Write this algorithm as a flowchart.
Open a new form and create a link to the program from the menu form (Main) under Iterations … Post-test Loops … Averages.  Write an application to display the average of 10 numbers entered by the user using a post-test loop with the syntax Do…..Loop Until to ask for each number.  Remember to use the Val() function to convert strings to numbers.  Each number might be asked for with an input box using the syntax:

strNumber=InputBox(“Enterthenextnumber.”,“Enteryournumbers.”)
intNumber=Val(strNumber)

Don’t forget to declare all your variables with Dim statements.  
e.g. Dim strNumber As String

Initialise your variables to 0 at the beginning of the procedure.
intNumber=0
intCounter=0
Can you be sure that the counter will never cause division by zero and crash your program?  Explain your answer.

No comments:

Post a Comment