Wrox Programmer Forums
|
VB How-To Ask your "How do I do this with VB?" questions in this forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 12th, 2006, 08:48 PM
Authorized User
 
Join Date: Dec 2004
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Default Why Does This Work?

To All,

I have found this nested For/Next loop designed to generate the prime numbers up to a certain number, in this case 13.

My question is why does the following code work as it is written? I have highlighted the portion in red that I don't understand. I could have generated this code myself but I would have written it with the red highlighted portion deleted, and by the way I find that the code works well with the red hightlighted portion present or deleted.

From my perspective, I don't understand why it would work with the hightlighted portion included, that is I don't understand why the final value of 13 would be included if the most that j could be is j-1 or 12. It would seem that you could never reach 13/13 or mod =0 for the final value of 13.

Any thoughts?

         For i = 3 To 13
            For j = 2 To i - 1
                If (i Mod j) = 0 Then
                    Exit For
                End If
            Next
            If (i = j) Then
                Console.Out.Write("{0} ", j)
            End If
        Next
        Console.Out.WriteLine()


Thank you for any insight,

Little Shell

 
Old October 13th, 2006, 12:59 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 627
Thanks: 0
Thanked 0 Times in 0 Posts
Default

every number is divisible by itself (result = 1) so there no need to test the number against itself. If you test it against itself, the condition (i mod j) is zero, therefore exiting the loop with value i=j (same as if you did not test against itself, in the case the loop never exits)
 
Old October 13th, 2006, 05:26 PM
Authorized User
 
Join Date: Dec 2004
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Marcostraf,

Thank you for your comments. You have been very helpful.

Little Shell






Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 1 Ctrl+F5 don't work, F5 does work? jimboak BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 7 June 29th, 2008 03:46 AM
why it doesn't work sangfroid C# 2005 2 January 21st, 2008 02:38 PM
Able to Get it work !! rsrika C# 2005 0 March 8th, 2006 09:16 AM
Why this example doesn't work DietCoke ASP.NET 1.0 and 1.1 Basics 3 November 29th, 2004 01:17 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.