looping and adding numbers


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: looping and adding numbers

  1. #1
    Join Date
    Mar 2005
    Posts
    21

    looping and adding numbers

    I am trying to write a method that traverses a for loop

    each time I traverse the loop I am taking a number from an array

    I need to add all these numbers up at the end, or add to a variable each time the loop is traversed so it increases by the number.

    I am struggling with syntax on this one.

    I have tried

    grandtotal = + total;

    I thought this may add total each time the loop is traversed, but it didnt

    The only other way I can think is either always knowing the previous numbers value and then adding that, or being able to find the number sum total an ArrayList object.

    Is there a tried and tested way to do this? I've search everywhere and can find no examples even remotely similar.

    Thanks

    javaStorm

  2. #2
    Join Date
    Mar 2005
    Posts
    21
    whoops I just seem to have answered my own question, it was

    grandtotal += total;

    I knew posting a question up would make it happen one way or another

  3. #3
    Join Date
    Feb 2004
    Posts
    541
    grandTotal += total; is just a simplified way of writing

    grandTotal = grandTotal + total;

    If the shorthand way ever confuses you, just use the longhand way

  4. #4
    Join Date
    Mar 2005
    Posts
    21
    As per usual

    Thanks!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center