Wrox Programmer Forums > Java nested loop
 | Search | Today's Posts | Mark Forums Read
 Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
 Welcome to the p2p.wrox.com Forums. You are currently viewing the Java Basics 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

June 28th, 2007, 08:42 AM
 Registered User Join Date: Jun 2007 Location: , , . Posts: 1 Thanks: 0 Thanked 0 Times in 0 Posts
nested loop

I’m having some difficulty in understanding nested loops so please explain as easy as possible. This is for finding 50 primes. Here’s the code.
public class FindPrimes
{
public static void main(String[] args)
{
int nPrimes = 50; // The maxium number of primes requried

OuterLoop:
for (int i = 2; ; i++) // This loop runs forever
{
//Try dividing all integers from 2 to i-1
for (int j = 2; j < i; j++)
{
if (i % j == 0) // This is true if j divides exactly
continue OuterLoop; // so exit the loop
}
// We only get here if we have a prime
System.out.println(i); // So output the value
if (--nPrimes == 0) // Decrement the count
break;
}
}
}

Tell me I’m right or not. First OuterLoop runs and we have i = 2; in outerloop. Then inner loop executes where j = 2; now we come to if where we check (i%j == 0) in other words (2%2 == 0) so it is true it executes ****inue OuterLoop; now we have i = 3; here need some explanation down to inner loop perhaps j again initialized to 2. so in if statement this happens (i%j == 0) in other words (3%2 == 0) it is false we have a prime. I want to know this when i becomes i = 10; will it happen in inner loop (7%2 == 0) , (7%3 ==), (7%4 == ), (7%5 == 0), (7%6 == 0) . It will be checked that 7 is prime or not by dividing 7 with all numbers less then 7 in inner loop. Then again OuterLoop. If I’m wrong please explain this

July 13th, 2007, 04:21 PM
 Authorized User Join Date: Apr 2005 Location: , , India. Posts: 71 Thanks: 0 Thanked 0 Times in 0 Posts

Yes, since the variable j is being initialized in the for loop, it would start from 2 and scale up. The 7%2,7%3,7%4 would happen. However for the first time when i=2, it will not enter the inner loop as j!<i that is 2 is not <2. Hence 2 would be printed directly.

-eNJay

 Similar Threads Thread Thread Starter Forum Replies Last Post Nested For-Each Loop In XSLT dghosh XSLT 10 July 2nd, 2008 04:34 AM QueryTables nested loop Sherman McCoy Excel VBA 0 June 4th, 2008 02:37 PM Nested Do Until Loop goels Access VBA 2 September 30th, 2004 08:44 AM nested while loop doesn't loop hosefo81 PHP Databases 5 November 12th, 2003 08:46 AM

 Contact Us - Wrox - Privacy Statement - Top