Free Web Hosting by Netfirms
Web Hosting by Netfirms | Free Domain Names by Netfirms


.

Home to new Visual Basic .NET programmers

Accumulators, Counters & Averages

 

Home ] What is this site? ] myExercises ] myTutorials ] myExam ] myMicrosoft ] myNotes ] Study Advice ] myLinks ]


Search (my)Visual Basic .NET

(Tip: Scroll past sponsored links to see your search results for this website)


Complain to Volkswagen South Africa (VWSA)


New to myExercises:

 

Lynnwood Consultants Program
Computer Calculator Program
Funny Time Program
Creative Minds Login Program
Hatfield Pizza & Pasta Program
  Shutterbug Program

New to myNotes:

 

>>

TOE chart


Order this book today from

kalahari.net: click-click, ding-dong

 

Title: Microsoft Visual Basic 2005:

         RELOADED, Second Edition

Author:  Diane Zak

 

Click the image below to order your copy.

 

Up ] GUI Standards ] Access Files ] [ Accumulators, Counters & Averages ] Arrays ] Collections ] Constants & Enumerators ] Crystal Reports ] Data Types ] Flowchart Symbols ] Functions ] Keywords ] Methods ] Operators ] Repetition Structures ] Selection Structures ] Sub Procedures ] Toolbox Controls ] Variables ] Windows Forms ]


 

Introduction

    Accumulator

    Counter    

    Averages

Syntax

Example

 


Introduction

  • Counters and Accumulators are most often used in Do...Loops to calculate sub-totals, totals and averages. 

  • A counter counts how many there are, e.g. number of employees, or how many times a certain piece of code executed.

  • An accumulator adds values together, e.g. sales amounts

  • Counters and Accumulators are just normal numeric variables that you declare using the Dim statement, which are then used together with operators to do a calculation.

  • Counters and Accumulators have a default value of zero.

  • Counters and accumulators can be initialised; in other words given a starting value other than the default value of zero

  • Incrementing (updating) is coded within the loop - updating Counters and Accumulators before or after the loop will give a wrong result.

  • The value of counters and accumulators can be positive or negative, integer or decimal

  • A counter is updated with a constant value, e.g. + 1

  • Accumulators are updated with values that vary; e.g. the price of an apple is different from the price of an orange

  • Averages are calculated by dividing the value of the accumulator with the value of the counter

Back to Top

 

Syntax

 

Counter

 

 counter = counter + increment_value 

e.g. intNumber = intNumber + 1

 

Accumulator

 

 accumulator = accumulator + variable 

e.g. sngSales = sngSales + sngItem

 

Average

 

 average = accumulator / counter 

e.g. sngAverageSales = sngSales / intNumber

 

Back to Top

 

Example

 

Dim strSales As String

Dim intNumSales As Integer    'counter

Dim sngSumSales As Single    'accumulator

Dim sngAverageSales As Single    'average

 

strSales = InputBox("Enter a sales amount. Click Cancel when finished.", "Sales Entry")

 

Do While strSales <> ""

    intNumSales = intNumSales + 1    'update counter

    sngSumSales = sngSumSales + Val(strSales)    'update accumulator

    strSales = InputBox("Enter a sales amount. Click Cancel when finished.", "Sales Entry")

Loop

 

sngAverageSales = sngSumSales / intNumSales    'calculate average

Me.AvgLabel.Text = Format(sngAverageSales, "currency")

 

Back to Top

 


All content on this site is free for private use only  |  Contributions are encouraged  |  Thank you to Netfirms for the free hosting  |  If copyright content is published here or links to certain content violates some right/law, contact the Webmaster to have it removed immediately.

 

Site launched: October 2005  |  Updated: July 20, 2006  |  Link exchange  |  Site map  |  Contact us