By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,099 Members | 1,364 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,099 IT Pros & Developers. It's quick & easy.

Summing Odds and Evens

P: 4
Not to step on anyone, but in reference to this thread on summing odds and evens, why not just use the 'continue' statement? So adding up the odds would look something like this:

-on some event-
Expand|Select|Wrap|Line Numbers
  1.         Dim counter, sum As Short
  2.         sum = 0
  3.         For counter = 1 To 99
  4.             If counter Mod 2 = 0 Then Continue For
  5.             sum += counter
  6.         Next
  7.         lblOdd.Text = sum
  8. end sub
You would just have to define the array from an input.
Apr 5 '07 #1
Share this Question
Share on Google+
8 Replies


Expert 5K+
P: 8,434
Not to step on anyone, but in reference to this thread on summing odds and evens, why not just use the 'continue' statement?
Visual Basic has a continue statement? That's news to me. What version of VB?
Apr 6 '07 #2

P: 4
Visual Basic has a continue statement? That's news to me. What version of VB?
VB 2005 is the first ver. to incorporate it (that I know of).
Apr 9 '07 #3

Expert 5K+
P: 8,434
VB 2005 is the first ver. to incorporate it (that I know of).
So, I'm guessing that it skips to the next iteration of the loop?

The language I use on the mainframe, called Natural, has such a statement (ESCAPE TOP). This has always been one of its best features, and one which I have greatly missed in other languages.
Apr 9 '07 #4

Expert 5K+
P: 8,434
To get back to the original question (not that the poster will see any of this, I suppose)...

If anyone's interested, it seems to me that just about the most efficient method (assuming there isn't a way to do it in a single calculation) would be to use an IF/ELSE construct. For example:
Expand|Select|Wrap|Line Numbers
  1. Dim Counter As Long, SumOdd As Long, SumEven As Long
  2. For Counter = 1 To 99
  3.   If Counter Mod 2 Then
  4.     SumOdd = SumOdd + Counter
  5.   Else
  6.     SumEven = SumEven + Counter
  7.   End If
  8. Next
(I'm assuming that the real issue wasn't about a simple 1 to 99 loop.)
Apr 9 '07 #5

P: 4
Here's the list of new commands.

From M$:
"Continue Statement

Visual Basic now supplies a Continue statement, which immediately skips to the next iteration of a Do, For, or While loop. "
Apr 12 '07 #6

P: 4
And no, my problem was much more difficult. I just don't expect more experienced people to do my homework for me :) (though, answering a question here and there has been invaluable).
Apr 12 '07 #7

Expert 5K+
P: 8,434
Here's the list of new commands.
Thanks, I'll bookmark that one for (probably a lot of) future reference.
Apr 13 '07 #8

Expert 5K+
P: 8,434
And no, my problem was much more difficult. I just don't expect more experienced people to do my homework for me :) (though, answering a question here and there has been invaluable).
Uh oh... you said the "H word". Now I'll have to close down the thread and kill you. ;)





In case this is too cryptic, I should point out that there has been considerable (and sometimes heated) heated debate over the handling of homework & coursework questions on TheScripts.
Apr 13 '07 #9

Post your reply

Sign in to post your reply or Sign up for a free account.