week_5_lab_notes

week_5_lab_notes - Week 5 Lab Notes Exercise#1 In this...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Week 5 Lab Notes Exercise #1 In this exercise we prompt the user to enter a series of numbers. We want to count how many numbers are entered and sum those numbers. When the user enters a negative number the program will stop accepting input and display the count and sum. To do this we’ll need three variables and a structured loop. One of the variables will be a counter and another accumulator. Here is the pseudocode: Pseudocode num theCount num theSum num userInput get userInput while userInput > 0 theSum = theSum + userInput theCount = theCount + 1 get userInput endwhile print theCount, theSum We’ll setup a simple script that will prompt a user for a number. To make the scripts logic easier to test I’ll include a loop that will iterate as long as the positive numbers are entered by the user. I’ll start the script with standard comments and variable declarations:
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
‘programmer: John Doe ‘created On: 7/4/1775 ‘purpose: demonstrates how to use counters and accumulators option explicit dim theCount dim theSum dim userInput Now i’ll prompt the user to enter a numeric value and assign that value to the variable userInput. I’ll use the int( ) function to force VBS to convert the user’s input into an integer. If this function is not used the numbers entered by the user will be concatenated (it turns out that plus symbol is used for both addition and concatenation) by the accumulator statement rather than summed. This is a quirk of VBScript that you must deal with when working with user input that are numbers. When you have the solution to this exercise working try a test run without the int function to see what happens. Here is the input statement we will need to use: userInput = int(inputbox(“enter a number (-1 to exit):”)) Now I’ll create a loop control question and fill in the loop’s code block with an accumulator and count statement:
Background image of page 2
do while userInput > 0 theSum = theSum + userInput theCount = theCount + 1 userInput = int(inputbox(“enter a number (-1 to exit):”))
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 7

week_5_lab_notes - Week 5 Lab Notes Exercise#1 In this...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online