Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > BOOK: Ivor Horton's Beginning Java, Java 7 Edition
|
BOOK: Ivor Horton's Beginning Java, Java 7 Edition
This is the forum to discuss the Wrox book Ivor Horton's Beginning Java, Java 7 Edition by Ivor Horton ; ISBN: 978-0-470-40414-0
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Ivor Horton's Beginning Java, Java 7 Edition 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:35 PM
Registered User
 
Join Date: Jun 2012
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default Chapter 3, Loops and Logic. 'Calculating Primes' nest loop example

Hello,
I am having difficulties understanding the loop example that user 'kao2110' had trouble with back in January. My difficulty is with either the Primes.java or Primes2.java examples found in Chapter 3, seeing as they are both the same. 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(from Primes2):

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:11 PM
Registered User
 
Join Date: Jun 2012
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

ok so turns out that I was mixing the equality operator signs up, where < is really less-than. So now my only problem is understanding when j iterates
 
Old July 29th, 2012, 05:32 PM
Authorized User
 
Join Date: Apr 2012
Posts: 11
Thanks: 2
Thanked 0 Times in 0 Posts
Default Value of j increases whenever value of i isn't an even number

After the first run-through of the outerloop, each time it iterates after that, the value of i is one more than it was the previous iteration. And with each iteration of the outerloop, the innerloop starts from the beginning - being newly initialized - and j starts with a value of 2 each time. This is because, in order to determine that a number isn't prime, it must be divisible by 2 with no remainder. Each time the outerloop iterates (and the innerloop executes anew) if i is an even number, j doesn't increase beyond its initialized value of 2 becasue the code of the if-statement gets executed.

Each time the innerloop iterates, and when i isn't an even number, j (which starts each innerloop iteration with a value of 2) is increased by one and divided into i. If the remainder, or modulus, of each division operation is ever zero, the innerloop is abandoned and the System.out.println(i) line of code, which is part of the outerloop, isn't executed. As long as i isn't an even number (and obviously divisible by 2), j will increase during iterations of the innerloop until it either divides into i with the modulus being zero (as in the case of when i has value of 9 and j increases to 3) or until j increases to the same value as i (as in when i is 5 and j increases from 2 to 3 to 4 then to 5, ending the current series of iterations of innerloop, with the modulus never being zero).

Last edited by skf001; August 4th, 2012 at 09:51 AM..





Similar Threads
Thread Thread Starter Forum Replies Last Post
Question on Chap 3: Loops and Logic - "Calculating Primes" examples kao2110 BOOK: Ivor Horton's Beginning Java, Java 7 Edition 3 January 28th, 2012 04:15 AM
changing a loop so it loops through certain files within a folder jeskit Excel VBA 5 May 3rd, 2011 08:26 AM
break for-each loops, or limit amount of loops warhero XSLT 2 July 4th, 2007 02:18 AM
Problem with loop logic holdmykidney XSLT 5 July 16th, 2004 07:11 AM
Mixing Data access logic and business logic polrtex BOOK: Professional Jakarta Struts 0 December 15th, 2003 07:19 PM





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