Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
|
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
 
Old March 26th, 2013, 04:34 PM
Authorized User
 
Join Date: Sep 2012
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default Trouble Understanding Primes Calculator

/**
I left the original programmer's comments in. I am having trouble understanding how this set of loops works. Both i and j start out as being initialized to 2, so...when the following statement executes:

if(i%j == 0) {
continue OuterLoop;
}

if both values for i and j are 2 then the condition of the if statement would be true and the continue statement would execute, thus not passing 2 to the output. Right??? But program output shows different. So...I am missing something. PLEASE HELP. Thank you.

*/


public class Primes2 {
public static void main(String[] args) {
int nValues = 50; // The maximum value to be checked

// Check all values from 2 to nValues
OuterLoop:
for(int i = 2 ; i <= nValues ; ++i) {
// Try dividing by 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
}
}
}

/**
The output is the following:

2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
*/
 
Old March 27th, 2013, 01:13 AM
Registered User
 
Join Date: Mar 2013
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default The condition in inner loop is not correct

Man, your inner loop condition j<i is not correct. Because first time, both j and i are 2 making the condition (2<2) false, so it will come out of loop with nothing inside the loop being executed. It will go to System.out.println(i) directly and execute.

Last edited by vickyanu; March 27th, 2013 at 01:18 AM..
 
Old March 27th, 2013, 01:24 AM
Authorized User
 
Join Date: Sep 2012
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default All I know is this: The program works!

it spits out all the prime numbers between 2 and 50...so...
 
Old March 27th, 2013, 01:30 AM
Registered User
 
Join Date: Mar 2013
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

check with j<=i instead of j<i
 
Old March 27th, 2013, 01:48 AM
Authorized User
 
Join Date: Sep 2012
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default With your suggestion...

the program compiles just fine but no values whatsoever are outputted





Similar Threads
Thread Thread Starter Forum Replies Last Post
Trouble Understanding for loop. Truck35 Java Basics 1 April 10th, 2014 12:17 PM
Unsure of how code works: Example: Primes alex001 BOOK: Ivor Horton's Beginning Java, Java 7 Edition 1 April 26th, 2013 05:53 PM
Chapter 3, Loops and Logic. 'Calculating Primes' nest loop example p0stalDud3 BOOK: Ivor Horton's Beginning Java, Java 7 Edition 2 July 29th, 2012 05:32 PM
Having trouble understanding Range Variable Notso BOOK: Beginning ASP.NET 4 : in C# and VB 3 March 17th, 2012 04:24 PM
Trouble Understanding the BLL jachin BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 7 March 13th, 2008 12:23 AM





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