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 June 28th, 2012, 01:44 PM
Registered User
 
Join Date: Jun 2012
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default problem with loops

Hello,
I am having difficulties understanding a loop example. Please look at the code below. I understand how the nested 'for' loop does not execute in the beginning because its conditional statement is "false" and therefore the first value of 'i,' which is 2, is output. What I dont understand is whether 'j' is iterated even though the conditional expression is false and cancels the entire loop, or does the conditional statement being false only affect the nested if statement and still maintains ++j alive to iterate to the next integer value? If 'j' is not iterated, then how is the conditional statement, i < j, of the nested for loop ever deemed "true" to not permit i as a prime when 'i' reaches the value of 4? I might be thinking this too far. Please help. Thank you.

Here is the code:

public class Primes2 {
public static void main(String[] args){
int nValues = 50;
int j;

Outerloop:
for(int i = 2; i <= nValues; ++i){

for(j = 2; j < i; ++j){
if(i % j == 0){
continue Outerloop;
}
}
System.out.println(i);
}

}
}
 
Old June 28th, 2012, 02:05 PM
Registered User
 
Join Date: Jun 2012
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

ok so it turns out that I had messed up with the equality operator. < is less than, which would make sense. What I am having difficulty with then is when does j iterate?
 
Old June 28th, 2012, 03:35 PM
Friend of Wrox
 
Join Date: May 2005
Posts: 227
Thanks: 1
Thanked 7 Times in 7 Posts
Default

p0stalDude3,
To assist with your problem, more information is needed: The definition of a prime number is not in your solution algorithm or your coding. A prime number is a number greater than 0 that is divisible by 1 and itself, such as 1,2, 3, 5, 7, 11, 13, 17,19, etc. In your code you are not testing for prime numbers. It is not clear from your code as to what to do with the prime number when you find it, that is to say do you display the prime, build a list (data structure), or place the prime number into a data structure like an array. Instead of stacking you for loop, first lay it out, step by step first. Hint: I would use the While-repetition control (While nValues <50, because the incrementing the loop is at the bottom of the loop and not at the top of the loop .

Hope ths helps.
__________________
Disclaimer: The above comments are solely the opinion of one person and not to be construed as a directive or an incentive to commit fraudulent acts.
 
Old July 31st, 2012, 09:07 PM
Authorized User
 
Join Date: Apr 2012
Posts: 11
Thanks: 2
Thanked 0 Times in 0 Posts
Default Answer is on "BOOK: Ivor Horton's Beginning Java, Java 7 Edition" forum

Go to the following:

Wrox Programmer Forums > Java > Java and JDK > BOOK: Ivor Horton's Beginning Java, Java 7 Edition





Similar Threads
Thread Thread Starter Forum Replies Last Post
break for-each loops, or limit amount of loops warhero XSLT 2 July 4th, 2007 02:18 AM
problem with arrays/loops jfern Javascript How-To 4 November 7th, 2006 10:29 AM
While loops and For loops in XSLT spencer.clark XSLT 1 August 5th, 2005 09:50 AM
problem with nested loops ptaylor2005 Beginning PHP 4 April 27th, 2005 07:05 PM
loops Hudson40 Access VBA 1 February 4th, 2005 12:58 PM





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